forked from Netflix/falcor-router-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
falcor.browser.js
17502 lines (14957 loc) · 564 KB
/
falcor.browser.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Copyright 2015 Netflix, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.falcor = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var falcor = require(30);
var jsong = require(154);
falcor.atom = jsong.atom;
falcor.ref = jsong.ref;
falcor.error = jsong.error;
falcor.pathValue = jsong.pathValue;
falcor.HttpDataSource = require(139);
module.exports = falcor;
},{"139":139,"154":154,"30":30}],2:[function(require,module,exports){
var ModelRoot = require(4);
var ModelDataSourceAdapter = require(3);
var RequestQueue = require(53);
var GetResponse = require(56);
var SetResponse = require(60);
var CallResponse = require(55);
var InvalidateResponse = require(58);
var ASAPScheduler = require(61);
var TimeoutScheduler = require(63);
var ImmediateScheduler = require(62);
var identity = require(96);
var arrayClone = require(79);
var arraySlice = require(83);
var collectLru = require(47);
var pathSyntax = require(158);
var getSize = require(92);
var isObject = require(103);
var isFunction = require(100);
var isPathValue = require(104);
var isJsonEnvelope = require(101);
var isJsonGraphEnvelope = require(102);
var setCache = require(64);
var setJsonGraphAsJsonDense = require(65);
var jsong = require(154);
var ID = 0;
module.exports = Model;
Model.ref = jsong.ref;
Model.atom = jsong.atom;
Model.error = jsong.error;
Model.pathValue = jsong.pathValue;
/**
* A Model object is used to execute commands against a {@link JSONGraph} object. {@link Model}s can work with a local JSONGraph cache, or it can work with a remote {@link JSONGraph} object through a {@link DataSource}.
* @constructor
* @param {?Object} options - a set of options to customize behavior
* @param {?DataSource} options.source - a data source to retrieve and manage the {@link JSONGraph}
* @param {?JSONGraph} options.cache - initial state of the {@link JSONGraph}
* @param {?number} options.maxSize - the maximum size of the cache
* @param {?number} options.collectRatio - the ratio of the maximum size to collect when the maxSize is exceeded
* @param {?Model~errorSelector} options.errorSelector - a function used to translate errors before they are returned
*/
function Model(o) {
var options = o || {};
this._root = options._root || new ModelRoot(options);
this._path = options.path || options._path || [];
this._scheduler = options.scheduler || options._scheduler || new ImmediateScheduler();
this._source = options.source || options._source;
this._request = options.request || options._request || new RequestQueue(this, this._scheduler);
this._ID = ID++;
if (typeof options.maxSize === "number") {
this._maxSize = options.maxSize;
} else {
this._maxSize = options._maxSize || Model.prototype._maxSize;
}
if (typeof options.collectRatio === "number") {
this._collectRatio = options.collectRatio;
} else {
this._collectRatio = options._collectRatio || Model.prototype._collectRatio;
}
if (options.boxed || options.hasOwnProperty("_boxed")) {
this._boxed = options.boxed || options._boxed;
}
if (options.materialized || options.hasOwnProperty("_materialized")) {
this._materialized = options.materialized || options._materialized;
}
if (typeof options.treatErrorsAsValues === "boolean") {
this._treatErrorsAsValues = options.treatErrorsAsValues;
} else if (options.hasOwnProperty("_treatErrorsAsValues")) {
this._treatErrorsAsValues = options._treatErrorsAsValues;
}
if (options.cache) {
this.setCache(options.cache);
}
}
Model.prototype.constructor = Model;
/**
* @property {Boolean} Materialized This is a test property
*/
Model.prototype._materialized = false;
Model.prototype._boxed = false;
Model.prototype._progressive = false;
Model.prototype._treatErrorsAsValues = false;
Model.prototype._maxSize = Math.pow(2, 53) - 1;
Model.prototype._collectRatio = 0.75;
/**
* The get method retrieves several {@link Path}s or {@link PathSet}s from a {@link Model}. The get method is versatile and may be called in several different ways, allowing you to make different trade-offs between performance and expressiveness. The simplest invocation returns an ModelResponse stream that contains a JSON object with all of the requested values. An optional selector function can also be passed in order to translate the retrieved data before it appears in the Observable stream. If a selector function is provided, the output will be an Observable stream with the result of the selector function invocation instead of a ModelResponse stream.
If you intend to transform the JSON data into another form, specifying a selector function may be more efficient. The selector function is run once all of the requested path values are available. In the body of the selector function, you can read data from the Model's cache using {@link Model.prototype.getValueSync} and transform it directly into its final representation (ex. an HTML string). This technique can reduce allocations by preventing the get method from copying the data in {@link Model}'s cache into an intermediary JSON representation.
Instead of directly accessing the cache within the selector function, you can optionally pass arguments to the selector function and they will be automatically bound to the corresponding {@link Path} or {@link PathSet} passed to the get method. If a {@link Path} is bound to a selector function argument, the function argument will contain the value found at that path. However if a {@link PathSet} is bound to a selector function argument, the function argument will be a JSON structure containing all of the path values. Using argument binding can provide a good balance between allocations and expressiveness. For more detail on how {@link Path}s and {@link PathSet}s are bound to selector function arguments, see the examples below.
* @function
* @param {...PathSet} path - the path(s) to retrieve
* @param {?Function} selector - the callback to execute once all of the paths have been retrieved
* @return {ModelResponse.<JSONEnvelope>|Observable} - the requested data as JSON, or the result of the optional selector function
*/
Model.prototype.get = function get() {
var args;
var argsIdx = -1;
var argsLen = arguments.length;
var selector = arguments[argsLen - 1];
if (isFunction(selector)) {
argsLen = argsLen - 1;
} else {
selector = void 0;
}
args = new Array(argsLen);
while (++argsIdx < argsLen) {
args[argsIdx] = arguments[argsIdx];
}
return GetResponse.create(this, args, selector);
};
/**
* Sets the value at one or more places in the JSONGraph model. The set method accepts one or more {@link PathValue}s, each of which is a combination of a location in the document and the value to place there. In addition to accepting {@link PathValue}s, the set method also returns the values after the set operation is complete.
* @function
* @param {...(PathValue | JSONGraphEnvelope | JSONEnvelope)} value - a value or collection of values to set into the Model.
* @return {ModelResponse.<JSON> | Observable} - an {@link Observable} stream containing the values in the JSONGraph model after the set was attempted
*/
Model.prototype.set = function set() {
var args;
var argsIdx = -1;
var argsLen = arguments.length;
var selector = arguments[argsLen - 1];
if (isFunction(selector)) {
argsLen = argsLen - 1;
} else {
selector = void 0;
}
args = new Array(argsLen);
while (++argsIdx < argsLen) {
args[argsIdx] = arguments[argsIdx];
}
return SetResponse.create(this, args, selector);
};
/*
* Invoke a function
* @function
* @param {Path} functionPath - the path to the function to invoke
* @param {Array.<Object>} args - the arguments to pass to the function
* @param {Array.<PathSet>} pathSuffixes - the paths to retrieve from objects returned from the function
* @param {Array.<PathSet>} calleePaths - the paths to retrieve from function callee after successful function execution
* @param {Function} selector the selector function
* @returns {ModelResponse.<*> | Observable} the {JSONGraph} fragment and associated metadata returned from the invoked function
*/
Model.prototype.call = function call() {
var args;
var argsIdx = -1;
var argsLen = arguments.length;
var selector = arguments[argsLen - 1];
if (isFunction(selector)) {
argsLen = argsLen - 1;
} else {
selector = void 0;
}
args = new Array(argsLen);
while (++argsIdx < argsLen) {
args[argsIdx] = arguments[argsIdx];
}
return CallResponse.create(this, args, selector);
};
Model.prototype.invalidate = function invalidate() {
var args;
var argsIdx = -1;
var argsLen = arguments.length;
var selector = arguments[argsLen - 1];
if (isFunction(selector)) {
argsLen = argsLen - 1;
} else {
selector = void 0;
}
args = new Array(argsLen);
while (++argsIdx < argsLen) {
args[argsIdx] = arguments[argsIdx];
}
InvalidateResponse.create(this, args, selector).subscribe();
return this;
};
/**
* Returns a clone of the {@link Model} bound to a location within the {@link JSONGraph}. The bound location is never a {@link Reference}: any {@link Reference}s encountered while resolving the bound {@link Path} are always replaced with the {@link Reference}s target value. For subsequent operations on the {@link Model}, all paths will be evaluated relative to the bound path. Deref allows you to:
* - Expose only a fragment of the {@link JSONGraph} to components, rather than the entire graph
* - Hide the location of a {@link JSONGraph} fragment from components
* - Optimize for executing multiple operations and path looksup at/below the same location in the {@link JSONGraph}
* @method
* @param {Path} boundPath - the path to bind to
* @param {...PathSet} relativePathsToPreload - paths (relative to the bound path) to preload before Model is created
* @return {Observable.<Model>} - an Observable stream with a single value, the bound {@link Model}, or an empty stream if nothing is found at the path
*/
Model.prototype.deref = require(5);
/**
* Get data for a single {@link Path}.
* @param {Path} path - the path to retrieve
* @return {Observable.<*>} - the value for the path
* @example
var model = new falcor.Model({source: new falcor.HttpDataSource("/model.json") });
model.
getValue('user.name').
subscribe(function(name) {
console.log(name);
});
// The code above prints "Jim" to the console.
*/
Model.prototype.getValue = function getValue(path) {
return this.get(path, identity);
};
Model.prototype.setValue = function setValue(pathArg, valueArg) {
var path = pathSyntax.fromPath(pathArg);
var value = isPathValue(path) ? path : Model.pathValue(path, valueArg);
return this.set(value, identity);
};
// TODO: Does not throw if given a PathSet rather than a Path, not sure if it should or not.
// TODO: Doc not accurate? I was able to invoke directly against the Model, perhaps because I don't have a data source?
// TODO: Not clear on what it means to "retrieve objects in addition to JSONGraph values"
/**
* Synchronously retrieves a single path from the local {@link Model} only and will not retrieve missing paths from the {@link DataSource}. This method can only be invoked when the {@link Model} does not have a {@link DataSource} or from within a selector function. See {@link Model.prototype.get}. The getValueSync method differs from the asynchronous get methods (ex. get, getValues) in that it can be used to retrieve objects in addition to JSONGraph values.
* @method
* @arg {Path} path - the path to retrieve
* @return {*} - the value for the specified path
*/
Model.prototype.getValueSync = require(21);
Model.prototype.setValueSync = require(77);
Model.prototype.derefSync = require(6);
/**
* Set the local cache to a {@link JSONGraph} fragment. This method can be a useful way of mocking a remote document, or restoring the local cache from a previously stored state.
* @param {JSONGraph} jsonGraph - the {@link JSONGraph} fragment to use as the local cache
*/
Model.prototype.setCache = function modelSetCache(cacheOrJSONGraphEnvelope) {
var cache = this._root.cache;
if (cacheOrJSONGraphEnvelope !== cache) {
var modelRoot = this._root;
this._root.cache = {};
if (typeof cache !== "undefined") {
collectLru(modelRoot, modelRoot.expired, getSize(cache), 0);
}
if (isJsonGraphEnvelope(cacheOrJSONGraphEnvelope)) {
setJsonGraphAsJsonDense(this, [cacheOrJSONGraphEnvelope], []);
} else if (isJsonEnvelope(cacheOrJSONGraphEnvelope)) {
setCache(this, cacheOrJSONGraphEnvelope.json);
} else if (isObject(cacheOrJSONGraphEnvelope)) {
setCache(this, cacheOrJSONGraphEnvelope);
}
} else if (typeof cache === "undefined") {
this._root.cache = {};
}
return this;
};
/**
* Get the local {@link JSONGraph} cache. This method can be a useful to store the state of the cache.
* @param {...Array.<PathSet>} [pathSets] - The path(s) to retrieve. If no paths are specified, the entire {@link JSONGraph} is returned.
* @return {JSONGraph} jsonGraph - a {@link JSONGraph} fragment
* @example
// Storing the boxshot of the first 10 titles in the first 10 genreLists to local storage.
localStorage.setItem('cache', JSON.stringify(model.getCache("genreLists[0...10][0...10].boxshot")));
*/
Model.prototype.getCache = function getCache() {
var paths = arraySlice(arguments);
if (paths.length === 0) {
paths[0] = {
json: this._root.cache
};
}
var result;
this.get.apply(this.withoutDataSource().boxValues().treatErrorsAsValues().materialize(), paths).
toJSONG().
subscribe(function(envelope) {
result = envelope.jsonGraph || envelope.jsong;
});
return result;
};
Model.prototype.getVersion = function getVersion(pathArg) {
var path = pathArg && pathSyntax.fromPath(pathArg) || [];
if (Array.isArray(path) === false) {
throw new Error("Model#getVersion must be called with an Array path.");
}
if (this._path.length) {
path = this._path.concat(path);
}
return this._getVersion(this, path);
};
Model.prototype.syncCheck = function syncCheck(name) {
if (Boolean(this._source) && this._root.syncRefCount <= 0 && this._root.unsafeMode === false) {
throw new Error("Model#" + name + " may only be called within the context of a request selector.");
}
return true;
};
/* eslint-disable guard-for-in */
Model.prototype.clone = function cloneModel(opts) {
var clone = new Model(this);
for (var key in opts) {
var value = opts[key];
if (value === "delete") {
delete clone[key];
} else {
clone[key] = value;
}
}
clone.setCache = void 0;
return clone;
};
/* eslint-enable */
// TODO: Should we be clearer this only applies to "get" operations? I'm assuming that is true
/**
* Returns a clone of the {@link Model} that eanbles batching. Within the configured time period, paths for operations of the same type are collected and executed on the {@link DataSource} in a batch. Batching can make more efficient use of the {@link DataSource} depending on its implementation, for example, reducing the number of HTTP requests to the server.
* @param {?Scheduler|number} schedulerOrDelay - Either a {@link Scheduler} that determines when to send a batch to the {@link DataSource}, or the number in milliseconds to collect a batch before sending to the {@link DataSource}. If this parameter is omitted, then batch collection ends at the end of the next tick.
* @return {Model}
*/
Model.prototype.batch = function batch(schedulerOrDelayArg) {
var schedulerOrDelay = schedulerOrDelayArg;
if (typeof schedulerOrDelay === "number") {
schedulerOrDelay = new TimeoutScheduler(Math.round(Math.abs(schedulerOrDelay)));
} else if (!schedulerOrDelay || !schedulerOrDelay.schedule) {
schedulerOrDelay = new ASAPScheduler();
}
var clone = this.clone();
clone._request = new RequestQueue(clone, schedulerOrDelay);
return clone;
};
/**
* Returns a clone of the {@link Model} that disables batching. This is the default mode. Each operation will be executed on the {@link DataSource} separately.
* @name unbatch
* @memberof Model.prototype
* @function
* @return {Model} A {@link Model} that batches requests of the same type and sends them to the data source together
*/
Model.prototype.unbatch = function unbatch() {
var clone = this.clone();
clone._request = new RequestQueue(clone, new ImmediateScheduler());
return clone;
};
// TODO: Add example of treatErrorsAsValues
/**
* Returns a clone of the {@link Model} that treats errors as values. Errors will be reported in the same callback used to report data. Errors will appear as objects in responses, rather than being sent to the {@link Observable~onErrorCallback} callback of the {@link ModelResponse}.
* @return {Model}
*/
Model.prototype.treatErrorsAsValues = function treatErrorsAsValues() {
return this.clone({
_treatErrorsAsValues: true
});
};
Model.prototype.asDataSource = function asDataSource() {
return new ModelDataSourceAdapter(this);
};
Model.prototype.materialize = function materialize() {
return this.clone({
_materialized: true
});
};
Model.prototype.dematerialize = function dematerialize() {
return this.clone({
_materialized: "delete"
});
};
/**
* Returns a clone of the {@link Model} that boxes values returning the wrapper ({@link Atom}, {@link Reference}, or {@link Error}), rather than the value inside it. This allows any metadata attached to the wrapper to be inspected.
* @return {Model}
*/
Model.prototype.boxValues = function boxValues() {
return this.clone({
_boxed: true
});
};
/**
* Returns a clone of the {@link Model} that unboxes values, returning the value inside of the wrapper ({@link Atom}, {@link Reference}, or {@link Error}), rather than the wrapper itself. This is the default mode.
* @return {Model}
*/
Model.prototype.unboxValues = function unboxValues() {
return this.clone({
_boxed: "delete"
});
};
/**
* Returns a clone of the {@link Model} that only uses the local {@link JSONGraph} and never uses a {@link DataSource} to retrieve missing paths.
* @return {Model}
*/
Model.prototype.withoutDataSource = function withoutDataSource() {
return this.clone({
_source: "delete"
});
};
Model.prototype.toJSON = function toJSON() {
return {
$type: "ref",
value: this._path
};
};
Model.prototype.getPath = function getPath() {
return arrayClone(this._path);
};
var getWalk = require(17);
Model.prototype._getBoundValue = require(14);
Model.prototype._getVersion = require(16);
Model.prototype._getValueSync = require(15);
Model.prototype._getPathSetsAsValues = require(13)(getWalk);
Model.prototype._getPathSetsAsJSON = require(10)(getWalk);
Model.prototype._getPathSetsAsPathMap = require(12)(getWalk);
Model.prototype._getPathSetsAsJSONG = require(11)(getWalk);
Model.prototype._getPathMapsAsValues = require(13)(getWalk);
Model.prototype._getPathMapsAsJSON = require(10)(getWalk);
Model.prototype._getPathMapsAsPathMap = require(12)(getWalk);
Model.prototype._getPathMapsAsJSONG = require(11)(getWalk);
Model.prototype._setPathValuesAsJSON = require(73);
Model.prototype._setPathValuesAsJSONG = require(74);
Model.prototype._setPathValuesAsPathMap = require(75);
Model.prototype._setPathValuesAsValues = require(76);
Model.prototype._setPathMapsAsJSON = require(69);
Model.prototype._setPathMapsAsJSONG = require(70);
Model.prototype._setPathMapsAsPathMap = require(71);
Model.prototype._setPathMapsAsValues = require(72);
Model.prototype._setJSONGsAsJSON = require(65);
Model.prototype._setJSONGsAsJSONG = require(66);
Model.prototype._setJSONGsAsPathMap = require(67);
Model.prototype._setJSONGsAsValues = require(68);
Model.prototype._setCache = require(64);
Model.prototype._invalidatePathSetsAsJSON = require(46);
Model.prototype._invalidatePathMapsAsJSON = require(45);
},{"10":10,"100":100,"101":101,"102":102,"103":103,"104":104,"11":11,"12":12,"13":13,"14":14,"15":15,"154":154,"158":158,"16":16,"17":17,"21":21,"3":3,"4":4,"45":45,"46":46,"47":47,"5":5,"53":53,"55":55,"56":56,"58":58,"6":6,"60":60,"61":61,"62":62,"63":63,"64":64,"65":65,"66":66,"67":67,"68":68,"69":69,"70":70,"71":71,"72":72,"73":73,"74":74,"75":75,"76":76,"77":77,"79":79,"83":83,"92":92,"96":96}],3:[function(require,module,exports){
function ModelDataSourceAdapter(model) {
this._model = model.materialize().boxValues().treatErrorsAsValues();
}
ModelDataSourceAdapter.prototype.get = function get(pathSets) {
return this._model.get.apply(this._model, pathSets).toJSONG();
};
ModelDataSourceAdapter.prototype.set = function set(jsongResponse) {
return this._model.set(jsongResponse).toJSONG();
};
ModelDataSourceAdapter.prototype.call = function call(path, args, suffixes, paths) {
var params = [path, args, suffixes].concat(paths);
return this._model.call.apply(this._model, params).toJSONG();
};
module.exports = ModelDataSourceAdapter;
},{}],4:[function(require,module,exports){
var isFunction = require(100);
var ImmediateScheduler = require(62);
function ModelRoot(o) {
var options = o || {};
this.syncRefCount = 0;
this.expired = options.expired || [];
this.unsafeMode = options.unsafeMode || false;
this.collectionScheduler = options.collectionScheduler || new ImmediateScheduler();
this.cache = {};
if (isFunction(options.comparator)) {
this.comparator = options.comparator;
}
if (isFunction(options.errorSelector)) {
this.errorSelector = options.errorSelector;
}
if (isFunction(options.onChange)) {
this.onChange = options.onChange;
}
}
ModelRoot.prototype.errorSelector = function errorSelector(x, y) {
return y;
};
ModelRoot.prototype.comparator = function comparator(a, b) {
if (Boolean(a) && typeof a === "object" && a.hasOwnProperty("value") &&
Boolean(b) && typeof b === "object" && b.hasOwnProperty("value")) {
return a.value === b.value;
}
return a === b;
};
module.exports = ModelRoot;
},{"100":100,"62":62}],5:[function(require,module,exports){
var Rx = require(180);
var pathSyntax = require(158);
module.exports = function deref(boundPathArg) {
var model = this;
var modelRoot = model._root;
var pathsIndex = -1;
var pathsCount = arguments.length - 1;
var paths = new Array(pathsCount);
var boundPath = pathSyntax.fromPath(boundPathArg);
while (++pathsIndex < pathsCount) {
paths[pathsIndex] = pathSyntax.fromPath(arguments[pathsIndex + 1]);
}
if (modelRoot.syncRefCount <= 0 && pathsCount === 0) {
throw new Error("Model#deref requires at least one value path.");
}
return Rx.Observable.defer(function() {
var value;
var errorHappened = false;
try {
++modelRoot.syncRefCount;
value = model.derefSync(boundPath);
} catch (e) {
value = e;
errorHappened = true;
} finally {
--modelRoot.syncRefCount;
return errorHappened ?
Rx.Observable.throw(value) :
Rx.Observable.return(value);
}
}).
flatMap(function(boundModel) {
if (Boolean(boundModel)) {
if (pathsCount > 0) {
return boundModel.get.apply(boundModel, paths.concat(function() {
return boundModel;
})).catch(Rx.Observable.empty());
}
return Rx.Observable.return(boundModel);
} else if (pathsCount > 0) {
return (model.get.apply(model, paths.map(function(path) {
return boundPath.concat(path);
}).concat(function() {
return model.deref(boundPath);
}))
.mergeAll());
}
return Rx.Observable.empty();
});
};
},{"158":158,"180":180}],6:[function(require,module,exports){
var $error = require(124);
var pathSyntax = require(158);
var getBoundValue = require(14);
var getType = require(93);
module.exports = function derefSync(boundPathArg) {
var boundPath = pathSyntax.fromPath(boundPathArg);
if (!Array.isArray(boundPath)) {
throw new Error("Model#derefSync must be called with an Array path.");
}
var boundValue = this.syncCheck("bindSync") && getBoundValue(this, this._path.concat(boundPath));
var path = boundValue.path;
var node = boundValue.value;
var found = boundValue.found;
if (!found) {
return void 0;
}
var type = getType(node);
if (Boolean(node) && Boolean(type)) {
if (type === $error) {
if (this._boxed) {
throw node;
}
throw node.value;
} else if (node.value === void 0) {
return void 0;
}
}
return this.clone({ _path: path });
};
},{"124":124,"14":14,"158":158,"93":93}],7:[function(require,module,exports){
/**
* An InvalidModelError can only happen when a user binds, whether sync
* or async to shorted value. See the unit tests for examples.
*
* @param {String} message
* @private
*/
function InvalidModelError(boundPath, shortedPath) {
this.message = "The boundPath of the model is not valid since a value or error was found before the path end.";
this.stack = (new Error()).stack;
this.boundPath = boundPath;
this.shortedPath = shortedPath;
}
// instanceof will be an error, but stack will be correct because its defined in the constructor.
InvalidModelError.prototype = new Error();
InvalidModelError.prototype.name = "InvalidModel";
module.exports = InvalidModelError;
},{}],8:[function(require,module,exports){
var NAME = "InvalidSourceError";
/**
* InvalidSourceError happens when a dataSource syncronously throws
* an exception during a get/set/call operation.
*
* @param {Error} error - The error that was thrown.
* @private
*/
function InvalidSourceError(error) {
this.message = "An exception was thrown when making a request.";
this.stack = (new Error()).stack;
this.innerError = error;
}
// instanceof will be an error, but stack will be correct because its defined
// in the constructor.
InvalidSourceError.prototype = new Error();
InvalidSourceError.prototype.name = NAME;
InvalidSourceError.name = NAME;
InvalidSourceError.is = function(e) {
return e && e.name === NAME;
};
module.exports = InvalidSourceError;
},{}],9:[function(require,module,exports){
var hardLink = require(23);
var createHardlink = hardLink.create;
var onValue = require(20);
var isExpired = require(24);
var $ref = require(125);
var __context = require(31);
var promote = require(27).promote;
/* eslint-disable no-constant-condition */
function followReference(model, root, nodeArg, referenceContainerArg, referenceArg, seed, outputFormat) {
var node = nodeArg;
var reference = referenceArg;
var referenceContainer = referenceContainerArg;
var depth = 0;
var k, next;
while (true) {
if (depth === 0 && referenceContainer[__context]) {
depth = reference.length;
next = referenceContainer[__context];
} else {
k = reference[depth++];
next = node[k];
}
if (next) {
var type = next.$type;
var value = type && next.value || next;
if (depth < reference.length) {
if (type) {
node = next;
break;
}
node = next;
continue;
}
// We need to report a value or follow another reference.
else {
node = next;
if (type && isExpired(next)) {
break;
}
if (!referenceContainer[__context]) {
createHardlink(referenceContainer, next);
}
// Restart the reference follower.
if (type === $ref) {
if (outputFormat === "JSONG") {
onValue(model, next, seed, null, null, reference, null, outputFormat);
} else {
promote(model, next);
}
depth = 0;
reference = value;
referenceContainer = next;
node = root;
continue;
}
break;
}
} else {
node = void 0;
}
break;
}
if (depth < reference.length && node !== void 0) {
var ref = [];
for (var i = 0; i < depth; i++) {
ref[i] = reference[i];
}
reference = ref;
}
return [node, reference];
}
/* eslint-enable */
module.exports = followReference;
},{"125":125,"20":20,"23":23,"24":24,"27":27,"31":31}],10:[function(require,module,exports){
var getBoundValue = require(14);
var isPathValue = require(26);
module.exports = function(walk) {
return function getAsJSON(model, paths, valuesArg) {
var values = valuesArg;
var results = {
values: [],
errors: [],
requestedPaths: [],
optimizedPaths: [],
requestedMissingPaths: [],
optimizedMissingPaths: []
};
var requestedMissingPaths = results.requestedMissingPaths;
var inputFormat = Array.isArray(paths[0]) || isPathValue(paths[0]) ?
"Paths" : "JSON";
var cache = model._root.cache;
var boundPath = model._path;
var currentCachePosition;
var missingIdx = 0;
var boundOptimizedPath, optimizedPath;
var i, j, len, bLen;
var valueNode, length;
results.values = values;
if (!values) {
values = [];
}
if (boundPath.length) {
var boundValue = getBoundValue(model, boundPath);
currentCachePosition = boundValue.value;
optimizedPath = boundOptimizedPath = boundValue.path;
} else {
currentCachePosition = cache;
optimizedPath = boundOptimizedPath = [];
}
for (i = 0, len = paths.length; i < len; i++) {
valueNode = void 0;
var pathSet = paths[i];
if (values[i]) {
valueNode = values[i];
}
if (len > 1) {
optimizedPath = [];
for (j = 0, bLen = boundOptimizedPath.length; j < bLen; j++) {
optimizedPath[j] = boundOptimizedPath[j];
}
}
if (inputFormat === "JSON") {
pathSet = pathSet.json;
} else if (pathSet.path) {
pathSet = pathSet.path;
}
walk(model, cache, currentCachePosition, pathSet, 0, valueNode, [], results, optimizedPath, [], inputFormat, "JSON");
if (missingIdx < requestedMissingPaths.length) {
for (j = missingIdx, length = requestedMissingPaths.length; j < length; j++) {
requestedMissingPaths[j].pathSetIndex = i;
}
missingIdx = length;
}
}
return results;
};
};
},{"14":14,"26":26}],11:[function(require,module,exports){
var isPathValue = require(26);
module.exports = function(walk) {
return function getAsJSONG(model, paths, values) {
var results = {
values: [],
errors: [],
requestedPaths: [],
optimizedPaths: [],
requestedMissingPaths: [],
optimizedMissingPaths: []
};
var inputFormat = Array.isArray(paths[0]) || isPathValue(paths[0]) ?
"Paths" : "JSON";
results.values = values;
var cache = model._root.cache;
var boundPath = model._path;
var currentCachePosition;
if (boundPath.length) {
throw new Error("It is not legal to use the JSON Graph format from a bound Model. JSON Graph format can only be used from a root model.");
} else {
currentCachePosition = cache;
}
for (var i = 0, len = paths.length; i < len; i++) {
var pathSet = paths[i];
if (inputFormat === "JSON") {
pathSet = pathSet.json;
} else if (pathSet.path) {
pathSet = pathSet.path;
}
walk(model, cache, currentCachePosition, pathSet, 0, values[0], [], results, [], [], inputFormat, "JSONG");
}
return results;
};
};
},{"26":26}],12:[function(require,module,exports){
var getBoundValue = require(14);
var isPathValue = require(26);
module.exports = function(walk) {
return function getAsPathMap(model, paths, values) {
var valueNode;
var results = {
values: [],
errors: [],
requestedPaths: [],
optimizedPaths: [],
requestedMissingPaths: [],
optimizedMissingPaths: []
};
var inputFormat = Array.isArray(paths[0]) || isPathValue(paths[0]) ?
"Paths" : "JSON";
valueNode = values[0];
results.values = values;
var cache = model._root.cache;
var boundPath = model._path;
var currentCachePosition;
var optimizedPath, boundOptimizedPath;
if (boundPath.length) {
var boundValue = getBoundValue(model, boundPath);
currentCachePosition = boundValue.value;
optimizedPath = boundOptimizedPath = boundValue.path;
} else {
currentCachePosition = cache;
optimizedPath = boundOptimizedPath = [];
}
for (var i = 0, len = paths.length; i < len; i++) {
if (len > 1) {
optimizedPath = [];
for (var j = 0, bLen = boundOptimizedPath.length; j < bLen; j++) {
optimizedPath[j] = boundOptimizedPath[j];
}
}
var pathSet = paths[i];
if (inputFormat === "JSON") {
pathSet = pathSet.json;
} else if (pathSet.path) {
pathSet = pathSet.path;
}
walk(model, cache, currentCachePosition, pathSet, 0, valueNode, [], results, optimizedPath, [], inputFormat, "PathMap");
}
return results;
};
};
},{"14":14,"26":26}],13:[function(require,module,exports){
var getBoundValue = require(14);
var isPathValue = require(26);
module.exports = function(walk) {
return function getAsValues(model, paths, onNext) {
var results = {
values: [],
errors: [],
requestedPaths: [],
optimizedPaths: [],
requestedMissingPaths: [],
optimizedMissingPaths: []
};
var inputFormat = Array.isArray(paths[0]) || isPathValue(paths[0]) ?
"Paths" : "JSON";
var cache = model._root.cache;
var boundPath = model._path;
var currentCachePosition;
var optimizedPath, boundOptimizedPath;
if (boundPath.length) {
var boundValue = getBoundValue(model, boundPath);
currentCachePosition = boundValue.value;
optimizedPath = boundOptimizedPath = boundValue.path;
} else {
currentCachePosition = cache;
optimizedPath = boundOptimizedPath = [];
}
for (var i = 0, len = paths.length; i < len; i++) {
if (len > 1) {
optimizedPath = [];
for (var j = 0, bLen = boundOptimizedPath.length; j < bLen; j++) {
optimizedPath[j] = boundOptimizedPath[j];
}
}
var pathSet = paths[i];
if (inputFormat === "JSON") {
pathSet = pathSet.json;
} else if (pathSet.path) {
pathSet = pathSet.path;
}
walk(model, cache, currentCachePosition, pathSet, 0, onNext, null, results, optimizedPath, [], inputFormat, "Values");
}
return results;
};
};
},{"14":14,"26":26}],14:[function(require,module,exports){
var getValueSync = require(15);
var InvalidModelError = require(7);