Skip to content

Commit 07ce5ff

Browse files
committed
Handle Falcor style observables from unhandled path datasource.
1 parent ca2c06a commit 07ce5ff

File tree

7 files changed

+100
-18
lines changed

7 files changed

+100
-18
lines changed

src/router/call.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var collapse = pathUtils.collapse;
99
var Observable = require('../RouterRx.js').Observable;
1010
var MaxPathsExceededError = require('../errors/MaxPathsExceededError');
1111
var getPathsCount = require('./getPathsCount');
12+
var outputToObservable = require('../run/conversion/outputToObservable');
1213
var rxNewToRxNewAndOld = require('../run/conversion/rxNewToRxNewAndOld');
1314

1415
/**
@@ -88,8 +89,9 @@ module.exports = function routerCall(callPath, args,
8889
// we will try the next dataSource in the line.
8990
catch(function catchException(e) {
9091
if (e instanceof CallNotFoundError && router._unhandled) {
91-
return router._unhandled.
92-
call(callPath, args, refPaths, thisPaths);
92+
return outputToObservable(
93+
router._unhandled.
94+
call(callPath, args, refPaths, thisPaths));
9395
}
9496
throw e;
9597
});

src/router/get.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Observable = require('../RouterRx.js').Observable;
77
var mCGRI = require('./../run/mergeCacheAndGatherRefsAndInvalidations');
88
var MaxPathsExceededError = require('../errors/MaxPathsExceededError');
99
var getPathsCount = require('./getPathsCount');
10+
var outputToObservable = require('../run/conversion/outputToObservable');
1011
var rxNewToRxNewAndOld = require('../run/conversion/rxNewToRxNewAndOld');
1112

1213
/**
@@ -56,8 +57,8 @@ module.exports = function routerGet(paths) {
5657
// The 3rd argument is the beginning of the actions
5758
// arguments, which for get is the same as the
5859
// unhandledPaths.
59-
return router._unhandled.
60-
get(unhandledPaths).
60+
return outputToObservable(
61+
router._unhandled.get(unhandledPaths)).
6162

6263
// Merge the solution back into the overall message.
6364
map(function(jsonGraphFragment) {

src/router/set.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var collapse = pathUtils.collapse;
1515
var mCGRI = require('./../run/mergeCacheAndGatherRefsAndInvalidations');
1616
var MaxPathsExceededError = require('../errors/MaxPathsExceededError');
1717
var getPathsCount = require('./getPathsCount');
18+
var outputToObservable = require('../run/conversion/outputToObservable');
1819
var rxNewToRxNewAndOld = require('../run/conversion/rxNewToRxNewAndOld');
1920

2021
/**
@@ -141,8 +142,8 @@ module.exports = function routerSet(jsonGraph) {
141142
return pV.path;
142143
}));
143144

144-
return router._unhandled.
145-
set(jsonGraphEnvelope).
145+
return outputToObservable(
146+
router._unhandled.set(jsonGraphEnvelope)).
146147

147148
// Merge the solution back into the overall message.
148149
map(function(unhandledJsonGraphEnv) {

test/FalcorObservable.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"use strict";
2+
3+
function noop() {
4+
// do nothing
5+
}
6+
7+
var disposeNoop = { dispose: noop };
8+
9+
function disposable(dispose) {
10+
if (typeof dispose === "function") {
11+
return { dispose: dispose };
12+
}
13+
if (typeof dispose === "undefined" || dispose === null) {
14+
return disposeNoop;
15+
}
16+
return dispose;
17+
}
18+
19+
function functionsObserver(onNext, onError, onCompleted) {
20+
return {
21+
onNext: typeof onNext === "function" ? onNext : noop,
22+
onError: typeof onError === "function" ? onError : noop,
23+
onCompleted: typeof onCompleted === "function" ? onCompleted : noop
24+
};
25+
}
26+
27+
function partialObserver(partial) {
28+
return {
29+
onNext: typeof partial.onNext === "function"
30+
? partial.onNext.bind(partial)
31+
: noop,
32+
onError: function onError(e) {
33+
if (typeof partial.onError === "function") {
34+
partial.onError(e);
35+
}
36+
},
37+
onCompleted: function onCompleted() {
38+
if (typeof partial.onCompleted === "function") {
39+
partial.onCompleted();
40+
}
41+
}
42+
};
43+
}
44+
45+
function observer(observerOrOnNext, onError, onCompleted) {
46+
return typeof observerOrOnNext === "object" && observerOrOnNext !== null
47+
? partialObserver(observerOrOnNext)
48+
: functionsObserver(observerOrOnNext, onError, onCompleted);
49+
}
50+
51+
function create(subscribe) {
52+
return {
53+
subscribe: function(observerOrOnNext, onError, onCompleted) {
54+
return disposable(
55+
subscribe(observer(observerOrOnNext, onError, onCompleted))
56+
);
57+
}
58+
};
59+
}
60+
61+
function empty() {
62+
return create(function(obs) {
63+
obs.onCompleted();
64+
});
65+
}
66+
67+
function of(value) {
68+
return create(function(obs) {
69+
obs.onNext(value);
70+
obs.onCompleted();
71+
});
72+
}
73+
74+
module.exports = {
75+
create: create,
76+
empty: empty,
77+
of: of
78+
};

test/unit/functional/unhandled.call.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ var noOp = function() {};
33
var chai = require('chai');
44
var expect = chai.expect;
55
var sinon = require('sinon');
6-
var Observable = require('../../../src/RouterRx').Observable;
6+
var FalcorObservable = require('../../FalcorObservable');
77

88
describe('#call', function() {
99
it('should ensure a missing function gets chained.', function(done) {
1010
var router = new R([]);
1111
var onCall = sinon.spy(function() {
12-
return Observable.of({
12+
return FalcorObservable.of({
1313
jsonGraph: {
1414
videos: {
1515
summary: 5
@@ -45,7 +45,7 @@ describe('#call', function() {
4545
it('should ensure a missing function gets chained and will not materialize properly.', function(done) {
4646
var router = new R([]);
4747
var onCall = sinon.spy(function() {
48-
return Observable.of({
48+
return FalcorObservable.of({
4949
jsonGraph: { },
5050
paths: [
5151
['videos', 'summary']

test/unit/functional/unhandled.get.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ var chai = require('chai');
44
var expect = chai.expect;
55
var sinon = require('sinon');
66
var pathValueMerge = require('./../../../src/cache/pathValueMerge');
7-
var Observable = require('../../../src/RouterRx').Observable;
7+
var FalcorObservable = require('../../FalcorObservable');
88
var $atom = require('./../../../src/support/types').$atom;
99

1010
describe('#get', function() {
1111
it('should return an empty Observable and just materialize values.', function(done) {
1212
var router = new R([]);
1313
var onUnhandledPaths = sinon.spy(function convert(paths) {
14-
return Observable.empty();
14+
return FalcorObservable.empty();
1515
});
1616
router.routeUnhandledPathsTo({get: onUnhandledPaths});
1717

@@ -45,7 +45,7 @@ describe('#get', function() {
4545
});
4646
return jsonGraph;
4747
}, {jsonGraph: {}});
48-
return Observable.of(returnValue);
48+
return FalcorObservable.of(returnValue);
4949
});
5050
router.routeUnhandledPathsTo({get: onUnhandledPaths});
5151

@@ -88,7 +88,7 @@ describe('#get', function() {
8888
});
8989
return jsonGraph;
9090
}, {jsonGraph: {}});
91-
return Observable.of(returnValue);
91+
return FalcorObservable.of(returnValue);
9292
});
9393
router.routeUnhandledPathsTo({get: onUnhandledPaths});
9494

test/unit/functional/unhandled.set.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ var chai = require('chai');
44
var expect = chai.expect;
55
var sinon = require('sinon');
66
var pathValueMerge = require('./../../../src/cache/pathValueMerge');
7-
var Observable = require('../../../src/RouterRx').Observable;
7+
var FalcorObservable = require('../../FalcorObservable');
88
var $atom = require('./../../../src/support/types').$atom;
99
var $ref = require('./../../../src/support/types').$ref;
1010

1111
describe('#set', function() {
1212
it('should return an empty Observable and just materialize values.', function(done) {
1313
var router = new R([]);
1414
var onUnhandledPaths = sinon.spy(function convert(paths) {
15-
return Observable.empty();
15+
return FalcorObservable.empty();
1616
});
1717
router.routeUnhandledPathsTo({set: onUnhandledPaths});
1818

@@ -63,7 +63,7 @@ describe('#set', function() {
6363
return jsonGraph;
6464
}, {jsonGraph: {}});
6565

66-
return Observable.of(returnValue);
66+
return FalcorObservable.of(returnValue);
6767
});
6868
router.routeUnhandledPathsTo({set: onUnhandledPaths});
6969

@@ -122,7 +122,7 @@ describe('#set', function() {
122122
});
123123
return jsonGraph;
124124
}, {jsonGraph: {}});
125-
return Observable.of(returnValue);
125+
return FalcorObservable.of(returnValue);
126126
});
127127
router.routeUnhandledPathsTo({set: onUnhandledPaths});
128128

@@ -202,7 +202,7 @@ describe('#set', function() {
202202
path: unicorn,
203203
value: 'missing'
204204
});
205-
return Observable.of(next);
205+
return FalcorObservable.of(next);
206206
});
207207
router.routeUnhandledPathsTo({set: onUnhandledPaths});
208208

0 commit comments

Comments
 (0)