Skip to content

Commit 8637d47

Browse files
committed
fix(bindCallback): only call function once even while scheduled
The previous implementation had issues with only calling the source function one time when scheduled. Since bindCallback shares its result with all subscribers, it is multicast, that means that it would need to maintain a list of subscribers internally. In leiu of that, I'm using AsyncSubject (which might need a better name) closes #881
1 parent feea9a1 commit 8637d47

File tree

6 files changed

+330
-123
lines changed

6 files changed

+330
-123
lines changed

spec/observables/bindCallback-spec.js

Lines changed: 220 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,246 @@
1-
/* globals describe, it, expect */
1+
/* globals describe, it, expect, rxTestScheduler */
22
var Rx = require('../../dist/cjs/Rx');
33
var Observable = Rx.Observable;
44

55
describe('Observable.bindCallback', function () {
6-
it('should emit one value from a callback', function (done) {
6+
describe('when not scheduled', function () {
7+
it('should emit one value from a callback', function () {
8+
function callback(datum, cb) {
9+
cb(datum);
10+
}
11+
var boundCallback = Observable.bindCallback(callback);
12+
var results = [];
13+
14+
boundCallback(42)
15+
.subscribe(function (x) {
16+
results.push(x);
17+
}, null, function () {
18+
results.push('done');
19+
});
20+
21+
expect(results).toEqual([42, 'done']);
22+
});
23+
24+
it('should emit one value chosen by a selector', function () {
25+
function callback(datum, cb) {
26+
cb(datum);
27+
}
28+
var boundCallback = Observable.bindCallback(callback, function (datum) { return datum; });
29+
var results = [];
30+
31+
boundCallback(42)
32+
.subscribe(function (x) {
33+
results.push(x);
34+
}, null, function () {
35+
results.push('done');
36+
});
37+
38+
expect(results).toEqual([42, 'done']);
39+
});
40+
41+
it('should emit an error when the selector throws', function () {
42+
function callback(cb) {
43+
cb(42);
44+
}
45+
var boundCallback = Observable.bindCallback(callback, function (err) { throw new Error('Yikes!'); });
46+
var results = [];
47+
48+
boundCallback()
49+
.subscribe(function () {
50+
throw 'should not next';
51+
}, function (err) {
52+
results.push(err);
53+
}, function () {
54+
throw 'should not complete';
55+
});
56+
57+
expect(results).toEqual([new Error('Yikes!')]);
58+
});
59+
60+
it('should not emit, throw or complete if immediately unsubscribed', function (done) {
61+
var nextSpy = jasmine.createSpy('next');
62+
var throwSpy = jasmine.createSpy('throw');
63+
var completeSpy = jasmine.createSpy('complete');
64+
var timeout;
65+
function callback(datum, cb) {
66+
// Need to cb async in order for the unsub to trigger
67+
timeout = setTimeout(function () {
68+
cb(datum);
69+
});
70+
}
71+
var subscription = Observable.bindCallback(callback)(42)
72+
.subscribe(nextSpy, throwSpy, completeSpy);
73+
subscription.unsubscribe();
74+
75+
setTimeout(function () {
76+
expect(nextSpy).not.toHaveBeenCalled();
77+
expect(throwSpy).not.toHaveBeenCalled();
78+
expect(completeSpy).not.toHaveBeenCalled();
79+
80+
clearTimeout(timeout);
81+
done();
82+
});
83+
});
84+
});
85+
86+
describe('when scheduled', function () {
87+
it('should emit one value from a callback', function () {
88+
function callback(datum, cb) {
89+
cb(datum);
90+
}
91+
var boundCallback = Observable.bindCallback(callback, null, rxTestScheduler);
92+
var results = [];
93+
94+
boundCallback(42)
95+
.subscribe(function (x) {
96+
results.push(x);
97+
}, null, function () {
98+
results.push('done');
99+
});
100+
101+
rxTestScheduler.flush();
102+
103+
expect(results).toEqual([42, 'done']);
104+
});
105+
106+
it('should error if callback throws', function () {
107+
function callback(datum, cb) {
108+
throw new Error('haha no callback for you');
109+
}
110+
var boundCallback = Observable.bindCallback(callback, null, rxTestScheduler);
111+
var results = [];
112+
113+
boundCallback(42)
114+
.subscribe(function (x) {
115+
throw 'should not next';
116+
}, function (err) {
117+
results.push(err);
118+
}, function () {
119+
throw 'should not complete';
120+
});
121+
122+
rxTestScheduler.flush();
123+
124+
expect(results).toEqual([new Error('haha no callback for you')]);
125+
});
126+
127+
it('should error if selector throws', function () {
128+
function callback(datum, cb) {
129+
cb(datum);
130+
}
131+
function selector() {
132+
throw new Error('what? a selector? I don\'t think so');
133+
}
134+
var boundCallback = Observable.bindCallback(callback, selector, rxTestScheduler);
135+
var results = [];
136+
137+
boundCallback(42)
138+
.subscribe(function (x) {
139+
throw 'should not next';
140+
}, function (err) {
141+
results.push(err);
142+
}, function () {
143+
throw 'should not complete';
144+
});
145+
146+
rxTestScheduler.flush();
147+
148+
expect(results).toEqual([new Error('what? a selector? I don\'t think so')]);
149+
});
150+
151+
it('should use a selector', function () {
152+
function callback(datum, cb) {
153+
cb(datum);
154+
}
155+
function selector(x) {
156+
return x + '!!!';
157+
}
158+
var boundCallback = Observable.bindCallback(callback, selector, rxTestScheduler);
159+
var results = [];
160+
161+
boundCallback(42)
162+
.subscribe(function (x) {
163+
results.push(x);
164+
}, null, function () {
165+
results.push('done');
166+
});
167+
168+
rxTestScheduler.flush();
169+
170+
expect(results).toEqual(['42!!!', 'done']);
171+
});
172+
});
173+
174+
it('should pass multiple inner arguments as an array', function () {
7175
function callback(datum, cb) {
8-
cb(datum);
176+
cb(datum, 1, 2, 3);
9177
}
10-
var boundCallback = Observable.bindCallback(callback);
178+
var boundCallback = Observable.bindCallback(callback, null, rxTestScheduler);
179+
var results = [];
11180

12181
boundCallback(42)
13182
.subscribe(function (x) {
14-
expect(x).toBe(42);
15-
}, function () {
16-
done.fail('should not be called');
17-
},
18-
done);
183+
results.push(x);
184+
}, null, function () {
185+
results.push('done');
186+
});
187+
188+
rxTestScheduler.flush();
189+
190+
expect(results).toEqual([[42, 1, 2, 3], 'done']);
19191
});
20192

21-
it('should emit one value chosen by a selector', function (done) {
193+
it('should pass multiple inner arguments to the selector if there is one', function () {
22194
function callback(datum, cb) {
23-
cb(null, datum);
195+
cb(datum, 1, 2, 3);
196+
}
197+
function selector(a, b, c, d) {
198+
expect([a, b, c, d]).toEqual([42, 1, 2, 3]);
199+
return a + b + c + d;
24200
}
25-
var boundCallback = Observable.bindCallback(callback, function (err, datum) { return datum; });
201+
var boundCallback = Observable.bindCallback(callback, selector, rxTestScheduler);
202+
var results = [];
26203

27204
boundCallback(42)
28205
.subscribe(function (x) {
29-
expect(x).toBe(42);
30-
}, function () {
31-
done.fail('should not be called');
32-
},
33-
done);
34-
});
206+
results.push(x);
207+
}, null, function () {
208+
results.push('done');
209+
});
35210

36-
it('should emit an error when the selector throws', function (done) {
37-
function callback(cb) {
38-
cb(42);
39-
}
40-
var boundCallback = Observable.bindCallback(callback, function (err) { throw new Error('Yikes!'); });
41-
42-
boundCallback()
43-
.subscribe(function () {
44-
// Considered a failure if we don't go directly to err handler
45-
done.fail('should not be called');
46-
},
47-
function (err) {
48-
expect(err.message).toBe('Yikes!');
49-
done();
50-
},
51-
function () {
52-
// Considered a failure if we don't go directly to err handler
53-
done.fail('should not be called');
54-
}
55-
);
211+
rxTestScheduler.flush();
212+
213+
expect(results).toEqual([48, 'done']);
56214
});
57215

58-
it('should not emit, throw or complete if immediately unsubscribed', function (done) {
59-
var nextSpy = jasmine.createSpy('next');
60-
var throwSpy = jasmine.createSpy('throw');
61-
var completeSpy = jasmine.createSpy('complete');
62-
var timeout;
216+
it('should cache value for next subscription and not call callbackFunc again', function () {
217+
var calls = 0;
63218
function callback(datum, cb) {
64-
// Need to cb async in order for the unsub to trigger
65-
timeout = setTimeout(function () {
66-
cb(datum);
67-
});
219+
calls++;
220+
cb(datum);
68221
}
69-
var subscription = Observable.bindCallback(callback)(42)
70-
.subscribe(nextSpy, throwSpy, completeSpy);
71-
subscription.unsubscribe();
222+
var boundCallback = Observable.bindCallback(callback, null, rxTestScheduler);
223+
var results1 = [];
224+
var results2 = [];
72225

73-
setTimeout(function () {
74-
expect(nextSpy).not.toHaveBeenCalled();
75-
expect(throwSpy).not.toHaveBeenCalled();
76-
expect(completeSpy).not.toHaveBeenCalled();
226+
var source = boundCallback(42);
77227

78-
clearTimeout(timeout);
79-
done();
228+
source.subscribe(function (x) {
229+
results1.push(x);
230+
}, null, function () {
231+
results1.push('done');
80232
});
233+
234+
source.subscribe(function (x) {
235+
results2.push(x);
236+
}, null, function () {
237+
results2.push('done');
238+
});
239+
240+
rxTestScheduler.flush();
241+
242+
expect(calls).toBe(1);
243+
expect(results1).toEqual([42, 'done']);
244+
expect(results2).toEqual([42, 'done']);
81245
});
82246
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* globals describe, it, expect, rxTestScheduler */
2+
var Rx = require('../../dist/cjs/Rx');
3+
var Observable = Rx.Observable;
4+
5+
/**
6+
* I'm starting this file to collect tests that when put in other files break jasmine for
7+
* no apparent reason. It seems like maybe we should move off of jasmine, but moving >1700 tests
8+
* sounds really gross, so I don't want to do that...
9+
*/
10+
describe('jasmine is weird', function () {
11+
describe('bindCallback', function () {
12+
// HACK: If you move this test under the bindCallback-spec.js file, it will arbitrarily
13+
// break one bufferWhen-spec.js test.
14+
it('should not even call the callbackFn if immediately unsubscribed', function () {
15+
var calls = 0;
16+
function callback(datum, cb) {
17+
calls++;
18+
cb(datum);
19+
}
20+
var boundCallback = Observable.bindCallback(callback, null, rxTestScheduler);
21+
var results1 = [];
22+
23+
var source = boundCallback(42);
24+
25+
var subscription = source.subscribe(function (x) {
26+
results1.push(x);
27+
}, null, function () {
28+
results1.push('done');
29+
});
30+
31+
subscription.unsubscribe();
32+
33+
rxTestScheduler.flush();
34+
35+
expect(calls).toBe(0);
36+
});
37+
});
38+
});

src/Rx.KitchenSink.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import './add/operator/combineLatest-static';
2626
import './add/operator/concat-static';
2727
import './add/operator/merge-static';
2828
import './add/observable/bindCallback';
29+
import './add/observable/defer';
2930
import './add/observable/empty';
3031
import './add/observable/forkJoin';
3132
import './add/observable/from';

src/Rx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {Observable} from './Observable';
1111
import './add/operator/combineLatest-static';
1212
import './add/operator/concat-static';
1313
import './add/operator/merge-static';
14-
import './observable/bindCallback';
14+
import './add/observable/bindCallback';
15+
import './add/observable/defer';
1516
import './add/observable/empty';
1617
import './add/observable/forkJoin';
1718
import './add/observable/from';

src/add/observable/bindCallback.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import {Observable} from '../../Observable';
2-
import {BoundCallbackObsevable} from '../../observable/bindCallback';
3-
Observable.bindCallback = BoundCallbackObsevable.create;
2+
import {BoundCallbackObservable} from '../../observable/bindCallback';
3+
Observable.bindCallback = BoundCallbackObservable.create;

0 commit comments

Comments
 (0)