Skip to content

Commit eef4b3a

Browse files
committed
Consider null as defined. Closes #1
1 parent 7dba51d commit eef4b3a

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
[![NPM](https://nodei.co/npm/reactive-function.png)](https://npmjs.org/package/reactive-function)
44

5-
A library for managing data flows and changing state.
5+
* A library for managing data flows graphs and changing state.
6+
* Built on [reactive-property](https://github.com/curran/reactive-property).
7+
* The foundation for [reactive-model](https://github.com/curran/reactive-model).
68

79
# Usage
810

@@ -55,7 +57,7 @@ ReactiveFunction({
5557
});
5658
```
5759

58-
This defines a "reactive function" that will be invoked when its inputs (`firstName` and `lastName`) are both defined and whenever either one changes. The function will be invoked on the next tick of the JavaScript event loop after it is defined and after any dependencies change.
60+
This defines a "reactive function" that will be invoked when its inputs (`firstName` and `lastName`) are both defined and whenever either one changes ([`null` is considered a defined value](https://github.com/curran/reactive-function/issues/1)). The function will be invoked on the next tick of the JavaScript event loop after it is defined and after any dependencies change.
5961

6062
To force a synchronous evaluation of all reactive functions whose dependencies have updated, you can call
6163

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,14 @@ function debounce(fn){
128128

129129
// Returns true if all elements of the given array are defined.
130130
function defined(arr){
131-
return !arr.some(function (d){
132-
return typeof d === "undefined" || d === null;
133-
});
131+
return !arr.some(isUndefined);
132+
}
133+
134+
// Returns true if the given object is undefined.
135+
// Returns false if the given object is some value, including null.
136+
// Inspired by http://ryanmorr.com/exploring-the-eternal-abyss-of-null-and-undefined/
137+
function isUndefined(obj){
138+
return obj === void 0;
134139
}
135140

136141
module.exports = ReactiveFunction;

test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ describe("ReactiveFunction", function() {
271271
assert.equal(numInvocations, 0);
272272
});
273273

274-
it("Should not invoke if an input is null.", function (){
274+
it("Should invoke if an input is null.", function (){
275275
var numInvocations = 0;
276276
var a = ReactiveProperty(null);
277277
var b = ReactiveProperty(10);
@@ -287,7 +287,7 @@ describe("ReactiveFunction", function() {
287287
});
288288

289289
ReactiveFunction.digest();
290-
assert.equal(numInvocations, 0);
290+
assert.equal(numInvocations, 1);
291291
});
292292

293293
it("Should be able to implement unidirectional data binding.", function (){

0 commit comments

Comments
 (0)