forked from graphistry/falcor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Request.js
333 lines (287 loc) · 9.78 KB
/
Request.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
var isArray = Array.isArray;
var Subject = require('./Subject');
var Subscriber = require('./Subscriber');
var Subscription = require('./Subscription');
var InvalidSourceError = require('../errors/InvalidSourceError');
var setJSONGraphs = require('../cache/set/setJSONGraphs');
var setPathValues = require('../cache/set/setPathValues');
var invalidatePaths = require('../cache/invalidate/invalidatePathSets');
var toPaths = require('@graphistry/falcor-path-utils/lib/toPaths');
var toCollapseMap = require('@graphistry/falcor-path-utils/lib/toCollapseMap');
var toCollapseTrees = require('@graphistry/falcor-path-utils/lib/toCollapseTrees');
var hasIntersection = require('@graphistry/falcor-path-utils/lib/hasIntersection');
module.exports = Request;
function Request(type, queue, source, scheduler) {
Subject.call(this, [], queue);
this.trees = [];
this.type = type;
this.data = null;
this.active = false;
this.responded = false;
this.paths = null;
this.requested = [];
this.optimized = [];
this.disposable = null;
this.dataSource = source;
this.scheduler = scheduler;
}
Request.prototype = Object.create(Subject.prototype);
Request.prototype.next =
Request.prototype.onNext = function(env) {
var queue = this.parent;
if (!queue) {
return;
}
this.responded = true;
/*
if (this.responded === false) {
this.responded = true;
// Remove this request from the request queue as soon as we get
// at least one response back. This ensures we won't be the target
// of in-flight batch requests.
queue.remove(this);
}
*/
var changed = false;
var jsonGraph = env.jsonGraph;
var boundPath = this.boundPath;
var modelRoot = queue.modelRoot;
var invalidated = env.invalidated;
var paths = env.paths || this.paths;
var requested = this.requested.slice(0);
var observers = this.observers.slice(0);
var rootChangeHandler = modelRoot.onChange;
// Run invalidations first.
if (invalidated && invalidated.length) {
changed = invalidatePaths({ _root: modelRoot, _path: [] }, invalidated, false);
}
if (paths && paths.length && !(!jsonGraph || typeof jsonGraph !== 'object')) {
var results = setJSONGraphs(
{ _root: modelRoot },
[{ paths: paths, jsonGraph: jsonGraph }],
modelRoot.errorSelector, modelRoot.comparator, false
);
paths = results[0];
changed = changed || results[2];
}
if (changed && rootChangeHandler) {
rootChangeHandler.call(modelRoot.topLevelModel);
}
observers.forEach(function(observer, index) {
observer.onNext({
type: 'get', paths: requested[index] ||
filterPathsBoundTo(boundPath, paths)
});
});
}
Request.prototype.error =
Request.prototype.onError = function(error) {
var queue = this.parent;
if (!queue) {
return;
}
if (this.responded === false) {
this.responded = true;
// Remove this request from the request queue as soon as we get
// at least one response back. This ensures we won't be the target
// of in-flight batch requests.
queue.remove(this);
}
error = error || {};
// Converts errors to object we can insert into the cache.
error = !(error instanceof Error) ?
// if it's $type error, use it raw
error.$type === $error && error ||
// Otherwise make it an error
{ $type: $error, value: error } :
// If it's instanceof Error, pluck error.message
{ $type: $error, value: { message: error.message }};
var modelRoot = queue.modelRoot;
var errorPathValues = toPaths(toCollapseTrees(
this.requested.reduce(function(collapseMap, paths) {
return toCollapseMap(paths, collapseMap);
}, {})
))
.map(function(path) { return { path: path, value: error }; });
if (errorPathValues.length) {
setPathValues(
{ _root: modelRoot, _path: [] },
errorPathValues,
modelRoot.errorSelector,
modelRoot.comparator,
false
);
}
Subject.prototype.onError.call(this, error);
}
Request.prototype.complete =
Request.prototype.onCompleted = function() {
if (this.responded === false) {
this.onNext({});
}
Subject.prototype.onCompleted.call(this);
}
Request.prototype.remove = function(subscription) {
var index = this.subscriptions.indexOf(subscription);
if (~index) {
this.trees.splice(index, 1);
this.requested.splice(index, 1);
this.optimized.splice(index, 1);
this.observers.splice(index, 1);
this.subscriptions.splice(index, 1);
}
if (this.subscriptions.length === 0) {
this.dispose();
}
return this;
}
Request.prototype.dispose =
Request.prototype.unsubscribe = function () {
this.trees = [];
this.data = null;
this.paths = null;
this.active = false;
this.boundPath = null;
this.requested = [];
this.optimized = [];
var queue = this.parent;
if (queue) {
this.parent = null;
queue.remove(this);
}
var disposable = this.disposable;
if (disposable) {
this.disposable = null;
if (disposable.dispose) {
disposable.dispose();
} else if (disposable.unsubscribe) {
disposable.unsubscribe();
}
}
Subject.prototype.dispose.call(this);
}
Request.prototype.connect = function() {
if (!this.active && !this.disposable) {
var scheduledDisposable = this.scheduler.schedule(flush.bind(this));
if (!this.disposable) {
this.disposable = scheduledDisposable;
}
}
return this;
}
Request.prototype.batch = function(requested, optimized,
requestedComplements,
optimizedComplements) {
if (this.active) {
var requestedIntersection = [];
var optimizedIntersection = [];
if (findIntersections(this.trees,
requested, optimized,
requestedComplements,
optimizedComplements,
requestedIntersection,
optimizedIntersection)) {
this.requested.push(requestedIntersection);
this.optimized.push(optimizedIntersection);
this.trees.push(toCollapseTrees(toCollapseMap(
optimizedIntersection
)));
return this;
}
return null;
}
this.trees.push({});
this.requested.push(requested);
this.optimized.push(optimized);
return this;
}
function flush() {
var obs, paths = this.paths = toPaths(toCollapseTrees(
this.optimized.reduce(function(collapseMap, paths) {
return toCollapseMap(paths, collapseMap);
}, {})
));
this.trees = this.optimized.map(function(paths) {
return toCollapseTrees(toCollapseMap(paths));
});
this.active = true;
try {
switch (this.type) {
case 'get':
obs = this.dataSource.get(paths);
break;
case 'set':
obs = this.dataSource.set({ paths: paths, jsonGraph: this.data });
break;
case 'call':
obs = this.dataSource.call.apply(this.dataSource, this.data);
break;
}
this.disposable = obs.subscribe(this);
} catch (e) {
this.disposable = {};
Subject.prototype.onError.call(this, new InvalidSourceError(e));
}
}
function findIntersections(trees,
requested, optimized,
requestedComplements,
optimizedComplements,
requestedIntersection,
optimizedIntersection) {
var index = -1;
var complementIndex = -1;
var reqComplementsIdx = -1;
var intersectionIndex = -1;
var reqIntersectionIdx = -1;
var treesLength = trees.length;
var optTotal = optimized.length;
var reqTotal = requested.length - 1;
toNextPath:
while (++index < optTotal) {
var treesIndex = -1;
var path = optimized[index];
var pathLen = path.length;
while (++treesIndex < treesLength) {
var tree = trees[treesIndex];
var subTree = tree[pathLen];
if (subTree && hasIntersection(subTree, path, 0, pathLen)) {
optimizedIntersection[++intersectionIndex] = path;
if (reqIntersectionIdx < reqTotal) {
requestedIntersection[++reqIntersectionIdx] = requested[
index < reqTotal ? index : reqTotal
];
}
continue toNextPath;
}
}
optimizedComplements[++complementIndex] = path;
if (reqComplementsIdx < reqTotal) {
requestedComplements[++reqComplementsIdx] = requested[
index < reqTotal ? index : reqTotal
];
}
}
return ~intersectionIndex;
}
function filterPathsBoundTo(boundPath, paths) {
var boundLength;
if (!boundPath || (boundLength = boundPath.length) === 0) {
return paths;
}
var filtered = [], filteredIndex = -1, keyIndex;
var path, pathsIndex = -1, pathsCount = paths.length;
outer: while (++pathsIndex < pathsCount) {
path = paths[pathsIndex];
if (path.length > boundLength) {
keyIndex = 0;
do {
if (path[keyIndex] !== boundPath[keyIndex]) {
continue outer;
}
} while (++keyIndex < boundLength);
filtered[++filteredIndex] = path.slice(boundLength);
}
}
return filtered;
}