Skip to content

Commit b91da45

Browse files
committed
change callback #5
1 parent 706d755 commit b91da45

File tree

4 files changed

+149
-2
lines changed

4 files changed

+149
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "it-depends",
3-
"version": "3.1.0",
3+
"version": "3.2.0",
44
"description": "Lightweight dependency tracking library for JavaScript",
55
"files": [
66
"src"

specs/generalChangeCallback.js

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
var expect = require('chai').expect;
2+
var itDepends = require('../src/it-depends.js');
3+
4+
describe('general change callback', function () {
5+
var callCount;
6+
var observableValue;
7+
var subscription;
8+
var lastChange;
9+
10+
var expectLastChanges = function(expected) {
11+
expect(lastChange.changed).to.equal(expected.changed);
12+
expect(lastChange.from).to.equal(expected.from);
13+
expect(lastChange.to).to.equal(expected.to);
14+
};
15+
16+
17+
beforeEach(function() {
18+
callCount = 0;
19+
20+
observableValue = itDepends.value('Bob');
21+
22+
subscription = itDepends.onChange(function(changed, from, to) {
23+
callCount++;
24+
lastChange = { changed: changed, from: from, to: to };
25+
});
26+
});
27+
28+
afterEach(function() {
29+
subscription.disable();
30+
});
31+
32+
it('should not be triggered when new subscription is created', function () {
33+
expect(callCount).to.equal(0);
34+
});
35+
36+
context('when when observable is changed', function() {
37+
beforeEach(function() {
38+
observableValue.write('Jack');
39+
});
40+
41+
it('should be triggered once', function () {
42+
expect(callCount).to.equal(1);
43+
expectLastChanges({ changed: observableValue, from: 'Bob', to: 'Jack' });
44+
});
45+
46+
it('should be triggered once when changed back', function () {
47+
observableValue.write('Bob');
48+
expect(callCount).to.equal(2);
49+
expectLastChanges({ changed: observableValue, from: 'Jack', to: 'Bob' });
50+
});
51+
});
52+
53+
context('when other observable value is created', function() {
54+
var otherValue;
55+
56+
beforeEach(function() {
57+
otherValue = itDepends.value('Jack');
58+
});
59+
60+
it('should not be triggered when other observable is created', function () {
61+
expect(callCount).to.equal(0);
62+
});
63+
64+
it('should be triggered once when observable is changed', function () {
65+
observableValue.write('Jack');
66+
67+
expect(callCount).to.equal(1);
68+
expectLastChanges({ changed: observableValue, from: 'Bob', to: 'Jack' });
69+
});
70+
71+
it('should be triggered once when other observable is changed', function () {
72+
otherValue.write('James');
73+
74+
expect(callCount).to.equal(1);
75+
expectLastChanges({ changed: otherValue, from: 'Jack', to: 'James' });
76+
});
77+
});
78+
79+
context('when there is a computed depending on observable value', function() {
80+
var computedValue;
81+
82+
beforeEach(function() {
83+
computedValue = itDepends.computed(function() {
84+
return "Hello, " + observableValue();
85+
});
86+
});
87+
88+
it('should be triggered once when observable is changed', function () {
89+
observableValue.write('Jack');
90+
91+
expect(callCount).to.equal(1);
92+
expectLastChanges({ changed: observableValue, from: 'Bob', to: 'Jack' });
93+
});
94+
});
95+
96+
context('when disabled', function() {
97+
beforeEach(function() {
98+
subscription.disable();
99+
});
100+
101+
it('should not be triggered when observable is changed', function () {
102+
observableValue.write('Jack');
103+
104+
expect(callCount).to.equal(0);
105+
});
106+
107+
context('when enabled back', function() {
108+
beforeEach(function() {
109+
subscription.enable();
110+
});
111+
112+
it('should be triggered when observable is changed', function () {
113+
observableValue.write('Jack');
114+
115+
expect(callCount).to.equal(1);
116+
expectLastChanges({ changed: observableValue, from: 'Bob', to: 'Jack' });
117+
});
118+
});
119+
});
120+
});

specs/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ require('./computedWithComputedDependency.js');
55
require('./unrelatedComputeds.js');
66
require('./computedWith2ValueDependencies.js');
77
require('./promiseValue.js');
8-
require('./parametricComputedWithNoDependencies.js');
8+
require('./parametricComputedWithNoDependencies.js');
9+
require('./generalChangeCallback.js');

src/it-depends.js

+26
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
var nop = function() {};
1212
var trackers = [nop];
13+
var handlers = {};
1314
var nextId = 0;
15+
var nextHandlerId = 0;
1416
var lastWriteVersion = 0;
1517

1618
function notifyCurrentTracker(id, observableValue, currentValue) {
@@ -75,6 +77,22 @@ var computed = function(calculator) {
7577
};
7678

7779
var library = {
80+
onChange: function(handler) {
81+
var handlerId = ++nextHandlerId;
82+
83+
var subscription = {
84+
enable: function() {
85+
handlers[handlerId] = handler;
86+
},
87+
disable: function() {
88+
delete handlers[handlerId];
89+
}
90+
};
91+
92+
subscription.enable();
93+
94+
return subscription;
95+
},
7896
value: function(initialValue) {
7997
var currentValue = initialValue;
8098
var id = ++nextId;
@@ -86,8 +104,16 @@ var library = {
86104

87105
self.write = function(newValue) {
88106
if (currentValue !== newValue) {
107+
var oldValue = currentValue;
89108
currentValue = newValue;
90109
lastWriteVersion++;
110+
111+
for (var handlerId in handlers) {
112+
if (!handlers.hasOwnProperty(handlerId))
113+
continue;
114+
115+
handlers[handlerId](self, oldValue, newValue);
116+
}
91117
}
92118
};
93119

0 commit comments

Comments
 (0)