mobx 3.2.2 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +2 -1
- package/lib/api/expr.d.ts +13 -13
- package/lib/api/intercept-read.d.ts +4 -4
- package/lib/api/intercept.d.ts +1 -1
- package/lib/api/isobservable.d.ts +4 -4
- package/lib/api/tojs.d.ts +2 -2
- package/lib/core/computedvalue.d.ts +3 -1
- package/lib/mobx.d.ts +1 -1
- package/lib/mobx.js +324 -211
- package/lib/mobx.js.flow +487 -549
- package/lib/mobx.min.js +2 -2
- package/lib/mobx.min.js.map +1 -1
- package/lib/mobx.module.js +324 -211
- package/lib/mobx.umd.js +324 -211
- package/lib/mobx.umd.min.js +2 -2
- package/lib/mobx.umd.min.js.map +1 -1
- package/lib/types/observableobject.d.ts +4 -4
- package/package.json +15 -3
package/lib/mobx.umd.js
CHANGED
|
@@ -113,7 +113,7 @@ var Atom = (function (_super) {
|
|
|
113
113
|
var isAtom = createInstanceofPredicate("Atom", BaseAtom);
|
|
114
114
|
|
|
115
115
|
function hasInterceptors(interceptable) {
|
|
116
|
-
return
|
|
116
|
+
return interceptable.interceptors && interceptable.interceptors.length > 0;
|
|
117
117
|
}
|
|
118
118
|
function registerInterceptor(interceptable, handler) {
|
|
119
119
|
var interceptors = interceptable.interceptors || (interceptable.interceptors = []);
|
|
@@ -225,7 +225,11 @@ var MAX_SPLICE_SIZE = 10000; // See e.g. https://github.com/mobxjs/mobx/issues/8
|
|
|
225
225
|
var safariPrototypeSetterInheritanceBug = (function () {
|
|
226
226
|
var v = false;
|
|
227
227
|
var p = {};
|
|
228
|
-
Object.defineProperty(p, "0", {
|
|
228
|
+
Object.defineProperty(p, "0", {
|
|
229
|
+
set: function () {
|
|
230
|
+
v = true;
|
|
231
|
+
}
|
|
232
|
+
});
|
|
229
233
|
Object.create(p)["0"] = 1;
|
|
230
234
|
return v === false;
|
|
231
235
|
})();
|
|
@@ -253,6 +257,32 @@ function inherit(ctor, proto) {
|
|
|
253
257
|
}
|
|
254
258
|
}
|
|
255
259
|
inherit(StubArray, Array.prototype);
|
|
260
|
+
// Weex freeze Array.prototype
|
|
261
|
+
// Make them writeable and configurable in prototype chain
|
|
262
|
+
// https://github.com/alibaba/weex/pull/1529
|
|
263
|
+
if (Object.isFrozen(Array)) {
|
|
264
|
+
|
|
265
|
+
[
|
|
266
|
+
"constructor",
|
|
267
|
+
"push",
|
|
268
|
+
"shift",
|
|
269
|
+
"concat",
|
|
270
|
+
"pop",
|
|
271
|
+
"unshift",
|
|
272
|
+
"replace",
|
|
273
|
+
"find",
|
|
274
|
+
"findIndex",
|
|
275
|
+
"splice",
|
|
276
|
+
"reverse",
|
|
277
|
+
"sort"
|
|
278
|
+
].forEach(function (key) {
|
|
279
|
+
Object.defineProperty(StubArray.prototype, key, {
|
|
280
|
+
configurable: true,
|
|
281
|
+
writable: true,
|
|
282
|
+
value: Array.prototype[key]
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
}
|
|
256
286
|
var ObservableArrayAdministration = (function () {
|
|
257
287
|
function ObservableArrayAdministration(name, enhancer, array, owned) {
|
|
258
288
|
this.array = array;
|
|
@@ -261,7 +291,7 @@ var ObservableArrayAdministration = (function () {
|
|
|
261
291
|
this.lastKnownLength = 0;
|
|
262
292
|
this.interceptors = null;
|
|
263
293
|
this.changeListeners = null;
|
|
264
|
-
this.atom = new BaseAtom(name ||
|
|
294
|
+
this.atom = new BaseAtom(name || "ObservableArray@" + getNextId());
|
|
265
295
|
this.enhancer = function (newV, oldV) { return enhancer(newV, oldV, name + "[..]"); };
|
|
266
296
|
}
|
|
267
297
|
ObservableArrayAdministration.prototype.dehanceValue = function (value) {
|
|
@@ -364,7 +394,9 @@ var ObservableArrayAdministration = (function () {
|
|
|
364
394
|
}
|
|
365
395
|
else {
|
|
366
396
|
var res = this.values.slice(index, index + deleteCount);
|
|
367
|
-
this.values = this.values
|
|
397
|
+
this.values = this.values
|
|
398
|
+
.slice(0, index)
|
|
399
|
+
.concat(newItems, this.values.slice(index + deleteCount));
|
|
368
400
|
return res;
|
|
369
401
|
}
|
|
370
402
|
var _a;
|
|
@@ -372,11 +404,15 @@ var ObservableArrayAdministration = (function () {
|
|
|
372
404
|
ObservableArrayAdministration.prototype.notifyArrayChildUpdate = function (index, newValue, oldValue) {
|
|
373
405
|
var notifySpy = !this.owned && isSpyEnabled();
|
|
374
406
|
var notify = hasListeners(this);
|
|
375
|
-
var change = notify || notifySpy
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
407
|
+
var change = notify || notifySpy
|
|
408
|
+
? {
|
|
409
|
+
object: this.array,
|
|
410
|
+
type: "update",
|
|
411
|
+
index: index,
|
|
412
|
+
newValue: newValue,
|
|
413
|
+
oldValue: oldValue
|
|
414
|
+
}
|
|
415
|
+
: null;
|
|
380
416
|
if (notifySpy)
|
|
381
417
|
spyReportStart(change);
|
|
382
418
|
this.atom.reportChanged();
|
|
@@ -388,13 +424,17 @@ var ObservableArrayAdministration = (function () {
|
|
|
388
424
|
ObservableArrayAdministration.prototype.notifyArraySplice = function (index, added, removed) {
|
|
389
425
|
var notifySpy = !this.owned && isSpyEnabled();
|
|
390
426
|
var notify = hasListeners(this);
|
|
391
|
-
var change = notify || notifySpy
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
427
|
+
var change = notify || notifySpy
|
|
428
|
+
? {
|
|
429
|
+
object: this.array,
|
|
430
|
+
type: "splice",
|
|
431
|
+
index: index,
|
|
432
|
+
removed: removed,
|
|
433
|
+
added: added,
|
|
434
|
+
removedCount: removed.length,
|
|
435
|
+
addedCount: added.length
|
|
436
|
+
}
|
|
437
|
+
: null;
|
|
398
438
|
if (notifySpy)
|
|
399
439
|
spyReportStart(change);
|
|
400
440
|
this.atom.reportChanged();
|
|
@@ -440,7 +480,7 @@ var ObservableArray = (function (_super) {
|
|
|
440
480
|
arrays[_i] = arguments[_i];
|
|
441
481
|
}
|
|
442
482
|
this.$mobx.atom.reportObserved();
|
|
443
|
-
return Array.prototype.concat.apply(this.peek(), arrays.map(function (a) { return isObservableArray(a) ? a.peek() : a; }));
|
|
483
|
+
return Array.prototype.concat.apply(this.peek(), arrays.map(function (a) { return (isObservableArray(a) ? a.peek() : a); }));
|
|
444
484
|
};
|
|
445
485
|
ObservableArray.prototype.replace = function (newItems) {
|
|
446
486
|
return this.$mobx.spliceWithArray(0, this.$mobx.values.length, newItems);
|
|
@@ -476,11 +516,11 @@ var ObservableArray = (function (_super) {
|
|
|
476
516
|
return -1;
|
|
477
517
|
};
|
|
478
518
|
/*
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
519
|
+
* functions that do alter the internal structure of the array, (based on lib.es6.d.ts)
|
|
520
|
+
* since these functions alter the inner structure of the array, the have side effects.
|
|
521
|
+
* Because the have side effects, they should not be used in computed function,
|
|
522
|
+
* and for that reason the do not call dependencyState.notifyObserved
|
|
523
|
+
*/
|
|
484
524
|
ObservableArray.prototype.splice = function (index, deleteCount) {
|
|
485
525
|
var newItems = [];
|
|
486
526
|
for (var _i = 2; _i < arguments.length; _i++) {
|
|
@@ -562,10 +602,15 @@ var ObservableArray = (function (_super) {
|
|
|
562
602
|
var oldItems = this.$mobx.values;
|
|
563
603
|
var newItems;
|
|
564
604
|
if (fromIndex < toIndex) {
|
|
565
|
-
newItems = oldItems.slice(0, fromIndex).concat(oldItems.slice(fromIndex + 1, toIndex + 1), [
|
|
605
|
+
newItems = oldItems.slice(0, fromIndex).concat(oldItems.slice(fromIndex + 1, toIndex + 1), [
|
|
606
|
+
oldItems[fromIndex]
|
|
607
|
+
], oldItems.slice(toIndex + 1));
|
|
566
608
|
}
|
|
567
609
|
else {
|
|
568
|
-
|
|
610
|
+
// toIndex < fromIndex
|
|
611
|
+
newItems = oldItems.slice(0, toIndex).concat([
|
|
612
|
+
oldItems[fromIndex]
|
|
613
|
+
], oldItems.slice(toIndex, fromIndex), oldItems.slice(fromIndex + 1));
|
|
569
614
|
}
|
|
570
615
|
this.replace(newItems);
|
|
571
616
|
};
|
|
@@ -577,7 +622,9 @@ var ObservableArray = (function (_super) {
|
|
|
577
622
|
impl.atom.reportObserved();
|
|
578
623
|
return impl.dehanceValue(impl.values[index]);
|
|
579
624
|
}
|
|
580
|
-
console.warn("[mobx.array] Attempt to read an array index (" + index + ") that is out of bounds (" + impl
|
|
625
|
+
console.warn("[mobx.array] Attempt to read an array index (" + index + ") that is out of bounds (" + impl
|
|
626
|
+
.values
|
|
627
|
+
.length + "). Please check length first. Out of bound indices will not be tracked by MobX");
|
|
581
628
|
}
|
|
582
629
|
return undefined;
|
|
583
630
|
};
|
|
@@ -593,7 +640,8 @@ var ObservableArray = (function (_super) {
|
|
|
593
640
|
var change = interceptChange(adm, {
|
|
594
641
|
type: "update",
|
|
595
642
|
object: this,
|
|
596
|
-
index: index,
|
|
643
|
+
index: index,
|
|
644
|
+
newValue: newValue
|
|
597
645
|
});
|
|
598
646
|
if (!change)
|
|
599
647
|
return;
|
|
@@ -630,9 +678,6 @@ Object.defineProperty(ObservableArray.prototype, "length", {
|
|
|
630
678
|
this.$mobx.setArrayLength(newLength);
|
|
631
679
|
}
|
|
632
680
|
});
|
|
633
|
-
/**
|
|
634
|
-
* Wrap function from prototype
|
|
635
|
-
*/
|
|
636
681
|
[
|
|
637
682
|
"every",
|
|
638
683
|
"filter",
|
|
@@ -744,7 +789,8 @@ var ObservableValue = (function (_super) {
|
|
|
744
789
|
spyReportStart({
|
|
745
790
|
type: "update",
|
|
746
791
|
object: this,
|
|
747
|
-
newValue: newValue,
|
|
792
|
+
newValue: newValue,
|
|
793
|
+
oldValue: oldValue
|
|
748
794
|
});
|
|
749
795
|
}
|
|
750
796
|
this.setNewValue(newValue);
|
|
@@ -755,16 +801,18 @@ var ObservableValue = (function (_super) {
|
|
|
755
801
|
ObservableValue.prototype.prepareNewValue = function (newValue) {
|
|
756
802
|
checkIfStateModificationsAreAllowed(this);
|
|
757
803
|
if (hasInterceptors(this)) {
|
|
758
|
-
var change = interceptChange(this, {
|
|
804
|
+
var change = interceptChange(this, {
|
|
805
|
+
object: this,
|
|
806
|
+
type: "update",
|
|
807
|
+
newValue: newValue
|
|
808
|
+
});
|
|
759
809
|
if (!change)
|
|
760
810
|
return UNCHANGED;
|
|
761
811
|
newValue = change.newValue;
|
|
762
812
|
}
|
|
763
813
|
// apply modifier
|
|
764
814
|
newValue = this.enhancer(newValue, this.value, this.name);
|
|
765
|
-
return this.value !== newValue
|
|
766
|
-
? newValue
|
|
767
|
-
: UNCHANGED;
|
|
815
|
+
return this.value !== newValue ? newValue : UNCHANGED;
|
|
768
816
|
};
|
|
769
817
|
ObservableValue.prototype.setNewValue = function (newValue) {
|
|
770
818
|
var oldValue = this.value;
|
|
@@ -774,7 +822,8 @@ var ObservableValue = (function (_super) {
|
|
|
774
822
|
notifyListeners(this, {
|
|
775
823
|
type: "update",
|
|
776
824
|
object: this,
|
|
777
|
-
newValue: newValue,
|
|
825
|
+
newValue: newValue,
|
|
826
|
+
oldValue: oldValue
|
|
778
827
|
});
|
|
779
828
|
}
|
|
780
829
|
};
|
|
@@ -810,44 +859,43 @@ ObservableValue.prototype[primitiveSymbol()] = ObservableValue.prototype.valueOf
|
|
|
810
859
|
var isObservableValue = createInstanceofPredicate("ObservableValue", ObservableValue);
|
|
811
860
|
|
|
812
861
|
var messages = {
|
|
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
|
-
"m038": "Missing items in this list?\n 1. Check whether all used values are properly marked as observable (use isObservable to verify)\n 2. Make sure you didn't dereference values too early. MobX observes props, not primitives. E.g: use 'person.name' instead of 'name' in your computation.\n"
|
|
862
|
+
m001: "It is not allowed to assign new values to @action fields",
|
|
863
|
+
m002: "`runInAction` expects a function",
|
|
864
|
+
m003: "`runInAction` expects a function without arguments",
|
|
865
|
+
m004: "autorun expects a function",
|
|
866
|
+
m005: "Warning: attempted to pass an action to autorun. Actions are untracked and will not trigger on state changes. Use `reaction` or wrap only your state modification code in an action.",
|
|
867
|
+
m006: "Warning: attempted to pass an action to autorunAsync. Actions are untracked and will not trigger on state changes. Use `reaction` or wrap only your state modification code in an action.",
|
|
868
|
+
m007: "reaction only accepts 2 or 3 arguments. If migrating from MobX 2, please provide an options object",
|
|
869
|
+
m008: "wrapping reaction expression in `asReference` is no longer supported, use options object instead",
|
|
870
|
+
m009: "@computed can only be used on getter functions, like: '@computed get myProps() { return ...; }'. It looks like it was used on a property.",
|
|
871
|
+
m010: "@computed can only be used on getter functions, like: '@computed get myProps() { return ...; }'",
|
|
872
|
+
m011: "First argument to `computed` should be an expression. If using computed as decorator, don't pass it arguments",
|
|
873
|
+
m012: "computed takes one or two arguments if used as function",
|
|
874
|
+
m013: "[mobx.expr] 'expr' should only be used inside other reactive functions.",
|
|
875
|
+
m014: "extendObservable expected 2 or more arguments",
|
|
876
|
+
m015: "extendObservable expects an object as first argument",
|
|
877
|
+
m016: "extendObservable should not be used on maps, use map.merge instead",
|
|
878
|
+
m017: "all arguments of extendObservable should be objects",
|
|
879
|
+
m018: "extending an object with another observable (object) is not supported. Please construct an explicit propertymap, using `toJS` if need. See issue #540",
|
|
880
|
+
m019: "[mobx.isObservable] isObservable(object, propertyName) is not supported for arrays and maps. Use map.has or array.length instead.",
|
|
881
|
+
m020: "modifiers can only be used for individual object properties",
|
|
882
|
+
m021: "observable expects zero or one arguments",
|
|
883
|
+
m022: "@observable can not be used on getters, use @computed instead",
|
|
884
|
+
m024: "whyRun() can only be used if a derivation is active, or by passing an computed value / reaction explicitly. If you invoked whyRun from inside a computation; the computation is currently suspended but re-evaluating because somebody requested its value.",
|
|
885
|
+
m025: "whyRun can only be used on reactions and computed values",
|
|
886
|
+
m026: "`action` can only be invoked on functions",
|
|
887
|
+
m028: "It is not allowed to set `useStrict` when a derivation is running",
|
|
888
|
+
m029: "INTERNAL ERROR only onBecomeUnobserved shouldn't be called twice in a row",
|
|
889
|
+
m030a: "Since strict-mode is enabled, changing observed observable values outside actions is not allowed. Please wrap the code in an `action` if this change is intended. Tried to modify: ",
|
|
890
|
+
m030b: "Side effects like changing state are not allowed at this point. Are you trying to modify state from, for example, the render function of a React component? Tried to modify: ",
|
|
891
|
+
m031: "Computed values are not allowed to cause side effects by changing observables that are already being observed. Tried to modify: ",
|
|
892
|
+
m032: "* This computation is suspended (not in use by any reaction) and won't run automatically.\n Didn't expect this computation to be suspended at this point?\n 1. Make sure this computation is used by a reaction (reaction, autorun, observer).\n 2. Check whether you are using this computation synchronously (in the same stack as they reaction that needs it).",
|
|
893
|
+
m033: "`observe` doesn't support the fire immediately property for observable maps.",
|
|
894
|
+
m034: "`mobx.map` is deprecated, use `new ObservableMap` or `mobx.observable.map` instead",
|
|
895
|
+
m035: "Cannot make the designated object observable; it is not extensible",
|
|
896
|
+
m036: "It is not possible to get index atoms from arrays",
|
|
897
|
+
m037: "Hi there! I'm sorry you have just run into an exception.\nIf your debugger ends up here, know that some reaction (like the render() of an observer component, autorun or reaction)\nthrew an exception and that mobx caught it, to avoid that it brings the rest of your application down.\nThe original cause of the exception (the code that caused this reaction to run (again)), is still in the stack.\n\nHowever, more interesting is the actual stack trace of the error itself.\nHopefully the error is an instanceof Error, because in that case you can inspect the original stack of the error from where it was thrown.\nSee `error.stack` property, or press the very subtle \"(...)\" link you see near the console.error message that probably brought you here.\nThat stack is more interesting than the stack of this console.error itself.\n\nIf the exception you see is an exception you created yourself, make sure to use `throw new Error(\"Oops\")` instead of `throw \"Oops\"`,\nbecause the javascript environment will only preserve the original stack trace in the first form.\n\nYou can also make sure the debugger pauses the next time this very same exception is thrown by enabling \"Pause on caught exception\".\n(Note that it might pause on many other, unrelated exception as well).\n\nIf that all doesn't help you out, feel free to open an issue https://github.com/mobxjs/mobx/issues!\n",
|
|
898
|
+
m038: "Missing items in this list?\n 1. Check whether all used values are properly marked as observable (use isObservable to verify)\n 2. Make sure you didn't dereference values too early. MobX observes props, not primitives. E.g: use 'person.name' instead of 'name' in your computation.\n"
|
|
851
899
|
};
|
|
852
900
|
function getMessage(id) {
|
|
853
901
|
return messages[id];
|
|
@@ -986,7 +1034,7 @@ function createClassPropertyDecorator(
|
|
|
986
1034
|
}
|
|
987
1035
|
}
|
|
988
1036
|
};
|
|
989
|
-
if (arguments.length < 3 || arguments.length === 5 && argLen < 3) {
|
|
1037
|
+
if (arguments.length < 3 || (arguments.length === 5 && argLen < 3)) {
|
|
990
1038
|
// Typescript target is ES3, so it won't define property for us
|
|
991
1039
|
// or using Reflect.decorate polyfill, which will return no descriptor
|
|
992
1040
|
// (see https://github.com/mobxjs/mobx/issues/333)
|
|
@@ -1002,10 +1050,11 @@ function createClassPropertyDecorator(
|
|
|
1002
1050
|
}
|
|
1003
1051
|
var value_1 = descriptor.value, initializer_1 = descriptor.initializer;
|
|
1004
1052
|
target.__mobxLazyInitializers.push(function (instance) {
|
|
1005
|
-
onInitialize(instance, key,
|
|
1053
|
+
onInitialize(instance, key, initializer_1 ? initializer_1.call(instance) : value_1, customArgs, descriptor);
|
|
1006
1054
|
});
|
|
1007
1055
|
return {
|
|
1008
|
-
enumerable: enumerable,
|
|
1056
|
+
enumerable: enumerable,
|
|
1057
|
+
configurable: true,
|
|
1009
1058
|
get: function () {
|
|
1010
1059
|
if (this.__mobxDidRunLazyInitializers !== true)
|
|
1011
1060
|
runLazyInitializers(this);
|
|
@@ -1028,7 +1077,9 @@ function createClassPropertyDecorator(
|
|
|
1028
1077
|
/** Indirect invocation: @decorator(args) bla */
|
|
1029
1078
|
var outerArgs = arguments;
|
|
1030
1079
|
var argLen = arguments.length;
|
|
1031
|
-
return function (target, key, descriptor) {
|
|
1080
|
+
return function (target, key, descriptor) {
|
|
1081
|
+
return classPropertyDecorator(target, key, descriptor, outerArgs, argLen);
|
|
1082
|
+
};
|
|
1032
1083
|
};
|
|
1033
1084
|
}
|
|
1034
1085
|
return classPropertyDecorator;
|
|
@@ -1044,7 +1095,8 @@ function runLazyInitializers(instance) {
|
|
|
1044
1095
|
return;
|
|
1045
1096
|
if (instance.__mobxLazyInitializers) {
|
|
1046
1097
|
addHiddenProp(instance, "__mobxDidRunLazyInitializers", true);
|
|
1047
|
-
instance.__mobxDidRunLazyInitializers &&
|
|
1098
|
+
instance.__mobxDidRunLazyInitializers &&
|
|
1099
|
+
instance.__mobxLazyInitializers.forEach(function (initializer) { return initializer(instance); });
|
|
1048
1100
|
}
|
|
1049
1101
|
}
|
|
1050
1102
|
function quacksLikeADecorator(args) {
|
|
@@ -1052,7 +1104,7 @@ function quacksLikeADecorator(args) {
|
|
|
1052
1104
|
}
|
|
1053
1105
|
|
|
1054
1106
|
var actionFieldDecorator = createClassPropertyDecorator(function (target, key, value, args, originalDescriptor) {
|
|
1055
|
-
var actionName =
|
|
1107
|
+
var actionName = args && args.length === 1 ? args[0] : value.name || key || "<unnamed action>";
|
|
1056
1108
|
var wrappedAction = action(actionName, value);
|
|
1057
1109
|
addHiddenProp(target, key, wrappedAction);
|
|
1058
1110
|
}, function (key) {
|
|
@@ -1094,6 +1146,9 @@ function namedActionDecorator(name) {
|
|
|
1094
1146
|
descriptor.configurable = true;
|
|
1095
1147
|
return descriptor;
|
|
1096
1148
|
}
|
|
1149
|
+
if (descriptor !== undefined && descriptor.get !== undefined) {
|
|
1150
|
+
throw new Error("[mobx] action is not expected to be used with getters");
|
|
1151
|
+
}
|
|
1097
1152
|
// bound instance methods
|
|
1098
1153
|
return actionFieldDecorator(name).apply(this, arguments);
|
|
1099
1154
|
};
|
|
@@ -1122,13 +1177,13 @@ function identityComparer(a, b) {
|
|
|
1122
1177
|
return a === b;
|
|
1123
1178
|
}
|
|
1124
1179
|
function structuralComparer(a, b) {
|
|
1125
|
-
if (typeof a ===
|
|
1180
|
+
if (typeof a === "number" && typeof b === "number" && isNaN(a) && isNaN(b)) {
|
|
1126
1181
|
return true;
|
|
1127
1182
|
}
|
|
1128
1183
|
return deepEqual(a, b);
|
|
1129
1184
|
}
|
|
1130
1185
|
function defaultComparer(a, b) {
|
|
1131
|
-
if (typeof a ===
|
|
1186
|
+
if (typeof a === "number" && typeof b === "number" && isNaN(a) && isNaN(b)) {
|
|
1132
1187
|
return true;
|
|
1133
1188
|
}
|
|
1134
1189
|
return identityComparer(a, b);
|
|
@@ -1147,7 +1202,7 @@ function autorun(arg1, arg2, arg3) {
|
|
|
1147
1202
|
scope = arg3;
|
|
1148
1203
|
}
|
|
1149
1204
|
else {
|
|
1150
|
-
name = arg1.name ||
|
|
1205
|
+
name = arg1.name || "Autorun@" + getNextId();
|
|
1151
1206
|
view = arg1;
|
|
1152
1207
|
scope = arg2;
|
|
1153
1208
|
}
|
|
@@ -1173,7 +1228,7 @@ function when(arg1, arg2, arg3, arg4) {
|
|
|
1173
1228
|
scope = arg4;
|
|
1174
1229
|
}
|
|
1175
1230
|
else {
|
|
1176
|
-
name =
|
|
1231
|
+
name = "When@" + getNextId();
|
|
1177
1232
|
predicate = arg1;
|
|
1178
1233
|
effect = arg2;
|
|
1179
1234
|
scope = arg3;
|
|
@@ -1197,7 +1252,7 @@ function autorunAsync(arg1, arg2, arg3, arg4) {
|
|
|
1197
1252
|
scope = arg4;
|
|
1198
1253
|
}
|
|
1199
1254
|
else {
|
|
1200
|
-
name = arg1.name ||
|
|
1255
|
+
name = arg1.name || "AutorunAsync@" + getNextId();
|
|
1201
1256
|
func = arg1;
|
|
1202
1257
|
delay = arg2;
|
|
1203
1258
|
scope = arg3;
|
|
@@ -1218,7 +1273,9 @@ function autorunAsync(arg1, arg2, arg3, arg4) {
|
|
|
1218
1273
|
}, delay);
|
|
1219
1274
|
}
|
|
1220
1275
|
});
|
|
1221
|
-
function reactionRunner() {
|
|
1276
|
+
function reactionRunner() {
|
|
1277
|
+
func(r);
|
|
1278
|
+
}
|
|
1222
1279
|
r.schedule();
|
|
1223
1280
|
return r.getDisposer();
|
|
1224
1281
|
}
|
|
@@ -1236,7 +1293,8 @@ function reaction(expression, effect, arg3) {
|
|
|
1236
1293
|
else {
|
|
1237
1294
|
opts = {};
|
|
1238
1295
|
}
|
|
1239
|
-
opts.name =
|
|
1296
|
+
opts.name =
|
|
1297
|
+
opts.name || expression.name || effect.name || "Reaction@" + getNextId();
|
|
1240
1298
|
opts.fireImmediately = arg3 === true || opts.fireImmediately === true;
|
|
1241
1299
|
opts.delay = opts.delay || 0;
|
|
1242
1300
|
opts.compareStructural = opts.compareStructural || opts.struct || false;
|
|
@@ -1250,9 +1308,7 @@ function reaction(expression, effect, arg3) {
|
|
|
1250
1308
|
var value;
|
|
1251
1309
|
var equals = opts.equals
|
|
1252
1310
|
? opts.equals
|
|
1253
|
-
:
|
|
1254
|
-
? comparer.structural
|
|
1255
|
-
: comparer.default;
|
|
1311
|
+
: opts.compareStructural || opts.struct ? comparer.structural : comparer.default;
|
|
1256
1312
|
var r = new Reaction(opts.name, function () {
|
|
1257
1313
|
if (firstTime || opts.delay < 1) {
|
|
1258
1314
|
reactionRunner();
|
|
@@ -1288,7 +1344,9 @@ function reaction(expression, effect, arg3) {
|
|
|
1288
1344
|
/**
|
|
1289
1345
|
* A node in the state dependency root that observes other nodes, and can be observed itself.
|
|
1290
1346
|
*
|
|
1291
|
-
* ComputedValue will remember result of the computation for duration of
|
|
1347
|
+
* ComputedValue will remember the result of the computation for the duration of the batch, or
|
|
1348
|
+
* while being observed.
|
|
1349
|
+
*
|
|
1292
1350
|
* During this time it will recompute only when one of its direct dependencies changed,
|
|
1293
1351
|
* but only when it is being accessed with `ComputedValue.get()`.
|
|
1294
1352
|
*
|
|
@@ -1379,7 +1437,8 @@ var ComputedValue = (function () {
|
|
|
1379
1437
|
};
|
|
1380
1438
|
ComputedValue.prototype.set = function (value) {
|
|
1381
1439
|
if (this.setter) {
|
|
1382
|
-
invariant(!this.isRunningSetter, "The setter of computed value '" + this
|
|
1440
|
+
invariant(!this.isRunningSetter, "The setter of computed value '" + this
|
|
1441
|
+
.name + "' is trying to update itself. Did you intend to update an _observable_ value, instead of the computed property?");
|
|
1383
1442
|
this.isRunningSetter = true;
|
|
1384
1443
|
try {
|
|
1385
1444
|
this.setter.call(this.scope, value);
|
|
@@ -1389,7 +1448,8 @@ var ComputedValue = (function () {
|
|
|
1389
1448
|
}
|
|
1390
1449
|
}
|
|
1391
1450
|
else
|
|
1392
|
-
invariant(false, "[ComputedValue '" + this
|
|
1451
|
+
invariant(false, "[ComputedValue '" + this
|
|
1452
|
+
.name + "'] It is not possible to assign a new value to a computed value.");
|
|
1393
1453
|
};
|
|
1394
1454
|
ComputedValue.prototype.trackAndCompute = function () {
|
|
1395
1455
|
if (isSpyEnabled()) {
|
|
@@ -1400,7 +1460,7 @@ var ComputedValue = (function () {
|
|
|
1400
1460
|
});
|
|
1401
1461
|
}
|
|
1402
1462
|
var oldValue = this.value;
|
|
1403
|
-
var newValue = this.value = this.computeValue(true);
|
|
1463
|
+
var newValue = (this.value = this.computeValue(true));
|
|
1404
1464
|
return (isCaughtException(oldValue) ||
|
|
1405
1465
|
isCaughtException(newValue) ||
|
|
1406
1466
|
!this.equals(oldValue, newValue));
|
|
@@ -1424,7 +1484,6 @@ var ComputedValue = (function () {
|
|
|
1424
1484
|
this.isComputing = false;
|
|
1425
1485
|
return res;
|
|
1426
1486
|
};
|
|
1427
|
-
|
|
1428
1487
|
ComputedValue.prototype.observe = function (listener, fireImmediately) {
|
|
1429
1488
|
var _this = this;
|
|
1430
1489
|
var firstTime = true;
|
|
@@ -1454,14 +1513,20 @@ var ComputedValue = (function () {
|
|
|
1454
1513
|
ComputedValue.prototype.valueOf = function () {
|
|
1455
1514
|
return toPrimitive(this.get());
|
|
1456
1515
|
};
|
|
1457
|
-
|
|
1458
1516
|
ComputedValue.prototype.whyRun = function () {
|
|
1459
1517
|
var isTracking = Boolean(globalState.trackingDerivation);
|
|
1460
1518
|
var observing = unique(this.isComputing ? this.newObserving : this.observing).map(function (dep) { return dep.name; });
|
|
1461
1519
|
var observers = unique(getObservers(this).map(function (dep) { return dep.name; }));
|
|
1462
|
-
return ("\nWhyRun? computation '" + this.name + "':\n * Running because: " + (isTracking
|
|
1463
|
-
|
|
1464
|
-
|
|
1520
|
+
return ("\nWhyRun? computation '" + this.name + "':\n * Running because: " + (isTracking
|
|
1521
|
+
? "[active] the value of this computation is needed by a reaction"
|
|
1522
|
+
: this.isComputing
|
|
1523
|
+
? "[get] The value of this computed was requested outside a reaction"
|
|
1524
|
+
: "[idle] not running at the moment") + "\n" +
|
|
1525
|
+
(this.dependenciesState === exports.IDerivationState.NOT_TRACKING
|
|
1526
|
+
? getMessage("m032")
|
|
1527
|
+
: " * This computation will re-run if any of the following observables changes:\n " + joinStrings(observing) + "\n " + (this.isComputing && isTracking
|
|
1528
|
+
? " (... or any observable accessed during the remainder of the current run)"
|
|
1529
|
+
: "") + "\n\t" + getMessage("m038") + "\n\n * If the outcome of this computation changes, the following observers will be re-run:\n " + joinStrings(observers) + "\n"));
|
|
1465
1530
|
};
|
|
1466
1531
|
return ComputedValue;
|
|
1467
1532
|
}());
|
|
@@ -1477,10 +1542,10 @@ var ObservableObjectAdministration = (function () {
|
|
|
1477
1542
|
this.interceptors = null;
|
|
1478
1543
|
}
|
|
1479
1544
|
/**
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1545
|
+
* Observes this object. Triggers for the events 'add', 'update' and 'delete'.
|
|
1546
|
+
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
|
|
1547
|
+
* for callback details
|
|
1548
|
+
*/
|
|
1484
1549
|
ObservableObjectAdministration.prototype.observe = function (callback, fireImmediately) {
|
|
1485
1550
|
invariant(fireImmediately !== true, "`observe` doesn't support the fire immediately property for observable objects.");
|
|
1486
1551
|
return registerListener(this, callback);
|
|
@@ -1491,7 +1556,7 @@ var ObservableObjectAdministration = (function () {
|
|
|
1491
1556
|
return ObservableObjectAdministration;
|
|
1492
1557
|
}());
|
|
1493
1558
|
function asObservableObject(target, name) {
|
|
1494
|
-
if (isObservableObject(target) && target.hasOwnProperty(
|
|
1559
|
+
if (isObservableObject(target) && target.hasOwnProperty("$mobx"))
|
|
1495
1560
|
return target.$mobx;
|
|
1496
1561
|
invariant(Object.isExtensible(target), getMessage("m035"));
|
|
1497
1562
|
if (!isPlainObject(target))
|
|
@@ -1503,7 +1568,7 @@ function asObservableObject(target, name) {
|
|
|
1503
1568
|
return adm;
|
|
1504
1569
|
}
|
|
1505
1570
|
function defineObservablePropertyFromDescriptor(adm, propName, descriptor, defaultEnhancer) {
|
|
1506
|
-
if (adm.values[propName]) {
|
|
1571
|
+
if (adm.values[propName] && !isComputedValue(adm.values[propName])) {
|
|
1507
1572
|
// already observable property
|
|
1508
1573
|
invariant("value" in descriptor, "The property " + propName + " in " + adm.name + " is already observable, cannot redefine it as computed property");
|
|
1509
1574
|
adm.target[propName] = descriptor.value; // the property setter will make 'value' reactive if needed.
|
|
@@ -1547,7 +1612,7 @@ function defineObservableProperty(adm, propName, newValue, enhancer) {
|
|
|
1547
1612
|
return;
|
|
1548
1613
|
newValue = change.newValue;
|
|
1549
1614
|
}
|
|
1550
|
-
var observable = adm.values[propName] = new ObservableValue(newValue, enhancer, adm.name + "." + propName, false);
|
|
1615
|
+
var observable = (adm.values[propName] = new ObservableValue(newValue, enhancer, adm.name + "." + propName, false));
|
|
1551
1616
|
newValue = observable.value; // observableValue might have changed it
|
|
1552
1617
|
Object.defineProperty(adm.target, propName, generateObservablePropConfig(propName));
|
|
1553
1618
|
notifyPropertyAddition(adm, adm.target, propName, newValue);
|
|
@@ -1571,28 +1636,30 @@ function defineComputedPropertyFromComputedValue(adm, propName, computedValue) {
|
|
|
1571
1636
|
var observablePropertyConfigs = {};
|
|
1572
1637
|
var computedPropertyConfigs = {};
|
|
1573
1638
|
function generateObservablePropConfig(propName) {
|
|
1574
|
-
return observablePropertyConfigs[propName] ||
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1639
|
+
return (observablePropertyConfigs[propName] ||
|
|
1640
|
+
(observablePropertyConfigs[propName] = {
|
|
1641
|
+
configurable: true,
|
|
1642
|
+
enumerable: true,
|
|
1643
|
+
get: function () {
|
|
1644
|
+
return this.$mobx.values[propName].get();
|
|
1645
|
+
},
|
|
1646
|
+
set: function (v) {
|
|
1647
|
+
setPropertyValue(this, propName, v);
|
|
1648
|
+
}
|
|
1649
|
+
}));
|
|
1584
1650
|
}
|
|
1585
1651
|
function generateComputedPropConfig(propName) {
|
|
1586
|
-
return computedPropertyConfigs[propName] ||
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1652
|
+
return (computedPropertyConfigs[propName] ||
|
|
1653
|
+
(computedPropertyConfigs[propName] = {
|
|
1654
|
+
configurable: true,
|
|
1655
|
+
enumerable: false,
|
|
1656
|
+
get: function () {
|
|
1657
|
+
return this.$mobx.values[propName].get();
|
|
1658
|
+
},
|
|
1659
|
+
set: function (v) {
|
|
1660
|
+
return this.$mobx.values[propName].set(v);
|
|
1661
|
+
}
|
|
1662
|
+
}));
|
|
1596
1663
|
}
|
|
1597
1664
|
function setPropertyValue(instance, name, newValue) {
|
|
1598
1665
|
var adm = instance.$mobx;
|
|
@@ -1602,7 +1669,8 @@ function setPropertyValue(instance, name, newValue) {
|
|
|
1602
1669
|
var change = interceptChange(adm, {
|
|
1603
1670
|
type: "update",
|
|
1604
1671
|
object: instance,
|
|
1605
|
-
name: name,
|
|
1672
|
+
name: name,
|
|
1673
|
+
newValue: newValue
|
|
1606
1674
|
});
|
|
1607
1675
|
if (!change)
|
|
1608
1676
|
return;
|
|
@@ -1613,12 +1681,15 @@ function setPropertyValue(instance, name, newValue) {
|
|
|
1613
1681
|
if (newValue !== UNCHANGED) {
|
|
1614
1682
|
var notify = hasListeners(adm);
|
|
1615
1683
|
var notifySpy = isSpyEnabled();
|
|
1616
|
-
var change = notify || notifySpy
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1684
|
+
var change = notify || notifySpy
|
|
1685
|
+
? {
|
|
1686
|
+
type: "update",
|
|
1687
|
+
object: instance,
|
|
1688
|
+
oldValue: observable.value,
|
|
1689
|
+
name: name,
|
|
1690
|
+
newValue: newValue
|
|
1691
|
+
}
|
|
1692
|
+
: null;
|
|
1622
1693
|
if (notifySpy)
|
|
1623
1694
|
spyReportStart(change);
|
|
1624
1695
|
observable.setNewValue(newValue);
|
|
@@ -1631,10 +1702,14 @@ function setPropertyValue(instance, name, newValue) {
|
|
|
1631
1702
|
function notifyPropertyAddition(adm, object, name, newValue) {
|
|
1632
1703
|
var notify = hasListeners(adm);
|
|
1633
1704
|
var notifySpy = isSpyEnabled();
|
|
1634
|
-
var change = notify || notifySpy
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1705
|
+
var change = notify || notifySpy
|
|
1706
|
+
? {
|
|
1707
|
+
type: "add",
|
|
1708
|
+
object: object,
|
|
1709
|
+
name: name,
|
|
1710
|
+
newValue: newValue
|
|
1711
|
+
}
|
|
1712
|
+
: null;
|
|
1638
1713
|
if (notifySpy)
|
|
1639
1714
|
spyReportStart(change);
|
|
1640
1715
|
if (notify)
|
|
@@ -1653,10 +1728,10 @@ function isObservableObject(thing) {
|
|
|
1653
1728
|
}
|
|
1654
1729
|
|
|
1655
1730
|
/**
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1731
|
+
* Returns true if the provided value is reactive.
|
|
1732
|
+
* @param value object, function or array
|
|
1733
|
+
* @param property if property is specified, checks whether value.property is reactive.
|
|
1734
|
+
*/
|
|
1660
1735
|
function isObservable(value, property) {
|
|
1661
1736
|
if (value === null || value === undefined)
|
|
1662
1737
|
return false;
|
|
@@ -1670,7 +1745,11 @@ function isObservable(value, property) {
|
|
|
1670
1745
|
return false;
|
|
1671
1746
|
}
|
|
1672
1747
|
// For first check, see #701
|
|
1673
|
-
return isObservableObject(value) ||
|
|
1748
|
+
return (isObservableObject(value) ||
|
|
1749
|
+
!!value.$mobx ||
|
|
1750
|
+
isAtom(value) ||
|
|
1751
|
+
isReaction(value) ||
|
|
1752
|
+
isComputedValue(value));
|
|
1674
1753
|
}
|
|
1675
1754
|
|
|
1676
1755
|
function createDecoratorForEnhancer(enhancer) {
|
|
@@ -1682,7 +1761,8 @@ function createDecoratorForEnhancer(enhancer) {
|
|
|
1682
1761
|
defineObservableProperty(adm, name, baseValue, enhancer);
|
|
1683
1762
|
}, function (name) {
|
|
1684
1763
|
var observable = this.$mobx.values[name];
|
|
1685
|
-
if (observable === undefined
|
|
1764
|
+
if (observable === undefined // See #505
|
|
1765
|
+
)
|
|
1686
1766
|
return undefined;
|
|
1687
1767
|
return observable.get();
|
|
1688
1768
|
}, function (name, value) {
|
|
@@ -1707,7 +1787,7 @@ function extendShallowObservable(target) {
|
|
|
1707
1787
|
function extendObservableHelper(target, defaultEnhancer, properties) {
|
|
1708
1788
|
invariant(arguments.length >= 2, getMessage("m014"));
|
|
1709
1789
|
invariant(typeof target === "object", getMessage("m015"));
|
|
1710
|
-
invariant(!
|
|
1790
|
+
invariant(!isObservableMap(target), getMessage("m016"));
|
|
1711
1791
|
properties.forEach(function (propSet) {
|
|
1712
1792
|
invariant(typeof propSet === "object", getMessage("m017"));
|
|
1713
1793
|
invariant(!isObservable(propSet), getMessage("m018"));
|
|
@@ -1854,7 +1934,7 @@ var observable = createObservable;
|
|
|
1854
1934
|
// ES6 class methods aren't enumerable, can't use Object.keys
|
|
1855
1935
|
Object.getOwnPropertyNames(IObservableFactories.prototype)
|
|
1856
1936
|
.filter(function (name) { return name !== "constructor"; })
|
|
1857
|
-
.forEach(function (name) { return observable[name] = IObservableFactories.prototype[name]; });
|
|
1937
|
+
.forEach(function (name) { return (observable[name] = IObservableFactories.prototype[name]); });
|
|
1858
1938
|
observable.deep.struct = observable.struct;
|
|
1859
1939
|
observable.ref.struct = function () {
|
|
1860
1940
|
if (arguments.length < 2) {
|
|
@@ -1952,7 +2032,6 @@ function refStructEnhancer(v, oldValue, name) {
|
|
|
1952
2032
|
*/
|
|
1953
2033
|
function transaction(action, thisArg) {
|
|
1954
2034
|
if (thisArg === void 0) { thisArg = undefined; }
|
|
1955
|
-
deprecated(getMessage("m023"));
|
|
1956
2035
|
return runInTransaction.apply(undefined, arguments);
|
|
1957
2036
|
}
|
|
1958
2037
|
function runInTransaction(action, thisArg) {
|
|
@@ -2026,12 +2105,14 @@ var ObservableMap = (function () {
|
|
|
2026
2105
|
if (this._has(key)) {
|
|
2027
2106
|
var notifySpy = isSpyEnabled();
|
|
2028
2107
|
var notify = hasListeners(this);
|
|
2029
|
-
var change = notify || notifySpy
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2108
|
+
var change = notify || notifySpy
|
|
2109
|
+
? {
|
|
2110
|
+
type: "delete",
|
|
2111
|
+
object: this,
|
|
2112
|
+
oldValue: this._data[key].value,
|
|
2113
|
+
name: key
|
|
2114
|
+
}
|
|
2115
|
+
: null;
|
|
2035
2116
|
if (notifySpy)
|
|
2036
2117
|
spyReportStart(change);
|
|
2037
2118
|
runInTransaction(function () {
|
|
@@ -2066,12 +2147,15 @@ var ObservableMap = (function () {
|
|
|
2066
2147
|
if (newValue !== UNCHANGED) {
|
|
2067
2148
|
var notifySpy = isSpyEnabled();
|
|
2068
2149
|
var notify = hasListeners(this);
|
|
2069
|
-
var change = notify || notifySpy
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2150
|
+
var change = notify || notifySpy
|
|
2151
|
+
? {
|
|
2152
|
+
type: "update",
|
|
2153
|
+
object: this,
|
|
2154
|
+
oldValue: observable$$1.value,
|
|
2155
|
+
name: name,
|
|
2156
|
+
newValue: newValue
|
|
2157
|
+
}
|
|
2158
|
+
: null;
|
|
2075
2159
|
if (notifySpy)
|
|
2076
2160
|
spyReportStart(change);
|
|
2077
2161
|
observable$$1.setNewValue(newValue);
|
|
@@ -2084,19 +2168,21 @@ var ObservableMap = (function () {
|
|
|
2084
2168
|
ObservableMap.prototype._addValue = function (name, newValue) {
|
|
2085
2169
|
var _this = this;
|
|
2086
2170
|
runInTransaction(function () {
|
|
2087
|
-
var observable$$1 = _this._data[name] = new ObservableValue(newValue, _this.enhancer, _this.name + "." + name, false);
|
|
2171
|
+
var observable$$1 = (_this._data[name] = new ObservableValue(newValue, _this.enhancer, _this.name + "." + name, false));
|
|
2088
2172
|
newValue = observable$$1.value; // value might have been changed
|
|
2089
2173
|
_this._updateHasMapEntry(name, true);
|
|
2090
2174
|
_this._keys.push(name);
|
|
2091
2175
|
});
|
|
2092
2176
|
var notifySpy = isSpyEnabled();
|
|
2093
2177
|
var notify = hasListeners(this);
|
|
2094
|
-
var change = notify || notifySpy
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2178
|
+
var change = notify || notifySpy
|
|
2179
|
+
? {
|
|
2180
|
+
type: "add",
|
|
2181
|
+
object: this,
|
|
2182
|
+
name: name,
|
|
2183
|
+
newValue: newValue
|
|
2184
|
+
}
|
|
2185
|
+
: null;
|
|
2100
2186
|
if (notifySpy)
|
|
2101
2187
|
spyReportStart(change);
|
|
2102
2188
|
if (notify)
|
|
@@ -2181,7 +2267,7 @@ var ObservableMap = (function () {
|
|
|
2181
2267
|
ObservableMap.prototype.toJS = function () {
|
|
2182
2268
|
var _this = this;
|
|
2183
2269
|
var res = {};
|
|
2184
|
-
this.keys().forEach(function (key) { return res[key] = _this.get(key); });
|
|
2270
|
+
this.keys().forEach(function (key) { return (res[key] = _this.get(key)); });
|
|
2185
2271
|
return res;
|
|
2186
2272
|
};
|
|
2187
2273
|
ObservableMap.prototype.toJSON = function () {
|
|
@@ -2201,7 +2287,10 @@ var ObservableMap = (function () {
|
|
|
2201
2287
|
};
|
|
2202
2288
|
ObservableMap.prototype.toString = function () {
|
|
2203
2289
|
var _this = this;
|
|
2204
|
-
return this.name +
|
|
2290
|
+
return (this.name +
|
|
2291
|
+
"[{ " +
|
|
2292
|
+
this.keys().map(function (key) { return key + ": " + ("" + _this.get(key)); }).join(", ") +
|
|
2293
|
+
" }]");
|
|
2205
2294
|
};
|
|
2206
2295
|
/**
|
|
2207
2296
|
* Observes this object. Triggers for the events 'add', 'update' and 'delete'.
|
|
@@ -2230,7 +2319,7 @@ var isObservableMap = createInstanceofPredicate("ObservableMap", ObservableMap);
|
|
|
2230
2319
|
var EMPTY_ARRAY = [];
|
|
2231
2320
|
Object.freeze(EMPTY_ARRAY);
|
|
2232
2321
|
function getGlobal() {
|
|
2233
|
-
return typeof window !==
|
|
2322
|
+
return typeof window !== "undefined" ? window : global;
|
|
2234
2323
|
}
|
|
2235
2324
|
function getNextId() {
|
|
2236
2325
|
return ++globalState.mobxGuid;
|
|
@@ -2282,7 +2371,9 @@ function joinStrings(things, limit, separator) {
|
|
|
2282
2371
|
if (!things)
|
|
2283
2372
|
return "";
|
|
2284
2373
|
var sliced = things.slice(0, limit);
|
|
2285
|
-
return "" + sliced.join(separator) + (things.length > limit
|
|
2374
|
+
return "" + sliced.join(separator) + (things.length > limit
|
|
2375
|
+
? " (... and " + (things.length - limit) + "more)"
|
|
2376
|
+
: "");
|
|
2286
2377
|
}
|
|
2287
2378
|
function isObject(value) {
|
|
2288
2379
|
return value !== null && typeof value === "object";
|
|
@@ -2424,7 +2515,7 @@ function primitiveSymbol() {
|
|
|
2424
2515
|
return (typeof Symbol === "function" && Symbol.toPrimitive) || "@@toPrimitive";
|
|
2425
2516
|
}
|
|
2426
2517
|
function toPrimitive(value) {
|
|
2427
|
-
return value === null ? null : typeof value === "object" ?
|
|
2518
|
+
return value === null ? null : typeof value === "object" ? "" + value : value;
|
|
2428
2519
|
}
|
|
2429
2520
|
|
|
2430
2521
|
/**
|
|
@@ -2569,6 +2660,7 @@ function addObserver(observable, node) {
|
|
|
2569
2660
|
// invariantObservers(observable);
|
|
2570
2661
|
var l = observable.observers.length;
|
|
2571
2662
|
if (l) {
|
|
2663
|
+
// because object assignment is relatively expensive, let's not store data about index 0.
|
|
2572
2664
|
observable.observersIndexes[node.__mapid] = l;
|
|
2573
2665
|
}
|
|
2574
2666
|
observable.observers[l] = node;
|
|
@@ -2592,8 +2684,10 @@ function removeObserver(observable, node) {
|
|
|
2592
2684
|
var map = observable.observersIndexes;
|
|
2593
2685
|
var filler = list.pop(); // get last element, which should fill the place of `node`, so the array doesn't have holes
|
|
2594
2686
|
if (filler !== node) {
|
|
2687
|
+
// otherwise node was the last element, which already got removed from array
|
|
2595
2688
|
var index = map[node.__mapid] || 0; // getting index of `node`. this is the only place we actually use map.
|
|
2596
2689
|
if (index) {
|
|
2690
|
+
// map store all indexes but 0, see comment in `addObserver`
|
|
2597
2691
|
map[filler.__mapid] = index;
|
|
2598
2692
|
}
|
|
2599
2693
|
else {
|
|
@@ -2690,7 +2784,8 @@ function propagateChangeConfirmed(observable) {
|
|
|
2690
2784
|
var d = observers[i];
|
|
2691
2785
|
if (d.dependenciesState === exports.IDerivationState.POSSIBLY_STALE)
|
|
2692
2786
|
d.dependenciesState = exports.IDerivationState.STALE;
|
|
2693
|
-
else if (d.dependenciesState === exports.IDerivationState.UP_TO_DATE
|
|
2787
|
+
else if (d.dependenciesState === exports.IDerivationState.UP_TO_DATE // this happens during computing of `d`, just keep lowestObserverState up to date.
|
|
2788
|
+
)
|
|
2694
2789
|
observable.lowestObserverState = exports.IDerivationState.UP_TO_DATE;
|
|
2695
2790
|
}
|
|
2696
2791
|
// invariantLOS(observable, "confirmed end");
|
|
@@ -2755,9 +2850,11 @@ function isCaughtException(e) {
|
|
|
2755
2850
|
*/
|
|
2756
2851
|
function shouldCompute(derivation) {
|
|
2757
2852
|
switch (derivation.dependenciesState) {
|
|
2758
|
-
case exports.IDerivationState.UP_TO_DATE:
|
|
2853
|
+
case exports.IDerivationState.UP_TO_DATE:
|
|
2854
|
+
return false;
|
|
2759
2855
|
case exports.IDerivationState.NOT_TRACKING:
|
|
2760
|
-
case exports.IDerivationState.STALE:
|
|
2856
|
+
case exports.IDerivationState.STALE:
|
|
2857
|
+
return true;
|
|
2761
2858
|
case exports.IDerivationState.POSSIBLY_STALE: {
|
|
2762
2859
|
var prevUntracked = untrackedStart(); // no need for those computeds to be reported, they will be picked up in trackDerivedFunction.
|
|
2763
2860
|
var obs = derivation.observing, l = obs.length;
|
|
@@ -2831,9 +2928,8 @@ function trackDerivedFunction(derivation, f, context) {
|
|
|
2831
2928
|
function bindDependencies(derivation) {
|
|
2832
2929
|
// invariant(derivation.dependenciesState !== IDerivationState.NOT_TRACKING, "INTERNAL ERROR bindDependencies expects derivation.dependenciesState !== -1");
|
|
2833
2930
|
var prevObserving = derivation.observing;
|
|
2834
|
-
var observing = derivation.observing = derivation.newObserving;
|
|
2931
|
+
var observing = (derivation.observing = derivation.newObserving);
|
|
2835
2932
|
var lowestNewObservingDerivationState = exports.IDerivationState.UP_TO_DATE;
|
|
2836
|
-
derivation.newObserving = null; // newObserving shouldn't be needed outside tracking
|
|
2837
2933
|
// Go through all new observables and check diffValue: (this list can contain duplicates):
|
|
2838
2934
|
// 0: first occurrence, change to 1 and keep it
|
|
2839
2935
|
// 1: extra occurrence, drop it
|
|
@@ -2853,6 +2949,7 @@ function bindDependencies(derivation) {
|
|
|
2853
2949
|
}
|
|
2854
2950
|
}
|
|
2855
2951
|
observing.length = i0;
|
|
2952
|
+
derivation.newObserving = null; // newObserving shouldn't be needed outside tracking (statement moved down to work around FF bug, see #614)
|
|
2856
2953
|
// Go through all old observables and check diffValue: (it is unique after last bindDependencies)
|
|
2857
2954
|
// 0: it's not in new observables, unobserve it
|
|
2858
2955
|
// 1: it keeps being observed, don't want to notify it. change to 0
|
|
@@ -2874,8 +2971,8 @@ function bindDependencies(derivation) {
|
|
|
2874
2971
|
addObserver(dep, derivation);
|
|
2875
2972
|
}
|
|
2876
2973
|
}
|
|
2877
|
-
// Some new observed derivations
|
|
2878
|
-
// so
|
|
2974
|
+
// Some new observed derivations may become stale during this derivation computation
|
|
2975
|
+
// so they have had no chance to propagate staleness (#916)
|
|
2879
2976
|
if (lowestNewObservingDerivationState !== exports.IDerivationState.UP_TO_DATE) {
|
|
2880
2977
|
derivation.dependenciesState = lowestNewObservingDerivationState;
|
|
2881
2978
|
derivation.onBecomeStale();
|
|
@@ -3040,7 +3137,11 @@ var Reaction = (function () {
|
|
|
3040
3137
|
};
|
|
3041
3138
|
Reaction.prototype.whyRun = function () {
|
|
3042
3139
|
var observing = unique(this._isRunning ? this.newObserving : this.observing).map(function (dep) { return dep.name; });
|
|
3043
|
-
return
|
|
3140
|
+
return "\nWhyRun? reaction '" + this.name + "':\n * Status: [" + (this.isDisposed
|
|
3141
|
+
? "stopped"
|
|
3142
|
+
: this._isRunning ? "running" : this.isScheduled() ? "scheduled" : "idle") + "]\n * This reaction will re-run if any of the following observables changes:\n " + joinStrings(observing) + "\n " + (this._isRunning
|
|
3143
|
+
? " (... or any observable accessed during the remainder of the current run)"
|
|
3144
|
+
: "") + "\n\t" + getMessage("m038") + "\n";
|
|
3044
3145
|
};
|
|
3045
3146
|
return Reaction;
|
|
3046
3147
|
}());
|
|
@@ -3079,8 +3180,8 @@ function runReactionsHelper() {
|
|
|
3079
3180
|
// we converge to no remaining reactions after a while.
|
|
3080
3181
|
while (allReactions.length > 0) {
|
|
3081
3182
|
if (++iterations === MAX_REACTION_ITERATIONS) {
|
|
3082
|
-
console.error("Reaction doesn't converge to a stable state after " + MAX_REACTION_ITERATIONS + " iterations."
|
|
3083
|
-
|
|
3183
|
+
console.error("Reaction doesn't converge to a stable state after " + MAX_REACTION_ITERATIONS + " iterations." +
|
|
3184
|
+
(" Probably there is a cycle in the reactive function: " + allReactions[0]));
|
|
3084
3185
|
allReactions.splice(0); // clear reactions
|
|
3085
3186
|
}
|
|
3086
3187
|
var remainingReactions = allReactions.splice(0);
|
|
@@ -3120,7 +3221,8 @@ function createComputedDecorator(equals) {
|
|
|
3120
3221
|
defineComputedProperty(adm, name, originalDescriptor.get, originalDescriptor.set, equals, false);
|
|
3121
3222
|
}, function (name) {
|
|
3122
3223
|
var observable = this.$mobx.values[name];
|
|
3123
|
-
if (observable === undefined
|
|
3224
|
+
if (observable === undefined // See #505
|
|
3225
|
+
)
|
|
3124
3226
|
return undefined;
|
|
3125
3227
|
return observable.get();
|
|
3126
3228
|
}, function (name, value) {
|
|
@@ -3133,7 +3235,7 @@ var computedStructDecorator = createComputedDecorator(comparer.structural);
|
|
|
3133
3235
|
* Decorator for class properties: @computed get value() { return expr; }.
|
|
3134
3236
|
* For legacy purposes also invokable as ES5 observable created: `computed(() => expr)`;
|
|
3135
3237
|
*/
|
|
3136
|
-
var computed =
|
|
3238
|
+
var computed = function computed(arg1, arg2, arg3) {
|
|
3137
3239
|
if (typeof arg2 === "string") {
|
|
3138
3240
|
return computedDecorator.apply(null, arguments);
|
|
3139
3241
|
}
|
|
@@ -3143,11 +3245,9 @@ var computed = (function computed(arg1, arg2, arg3) {
|
|
|
3143
3245
|
opts.setter = typeof arg2 === "function" ? arg2 : opts.setter;
|
|
3144
3246
|
var equals = opts.equals
|
|
3145
3247
|
? opts.equals
|
|
3146
|
-
:
|
|
3147
|
-
? comparer.structural
|
|
3148
|
-
: comparer.default;
|
|
3248
|
+
: opts.compareStructural || opts.struct ? comparer.structural : comparer.default;
|
|
3149
3249
|
return new ComputedValue(arg1, opts.context, equals, opts.name || arg1.name || "", opts.setter);
|
|
3150
|
-
}
|
|
3250
|
+
};
|
|
3151
3251
|
computed.struct = computedStructDecorator;
|
|
3152
3252
|
computed.equals = createComputedDecorator;
|
|
3153
3253
|
|
|
@@ -3252,19 +3352,19 @@ function interceptProperty(thing, property, handler) {
|
|
|
3252
3352
|
}
|
|
3253
3353
|
|
|
3254
3354
|
/**
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3355
|
+
* expr can be used to create temporarily views inside views.
|
|
3356
|
+
* This can be improved to improve performance if a value changes often, but usually doesn't affect the outcome of an expression.
|
|
3357
|
+
*
|
|
3358
|
+
* In the following example the expression prevents that a component is rerender _each time_ the selection changes;
|
|
3359
|
+
* instead it will only rerenders when the current todo is (de)selected.
|
|
3360
|
+
*
|
|
3361
|
+
* reactiveComponent((props) => {
|
|
3362
|
+
* const todo = props.todo;
|
|
3363
|
+
* const isSelected = mobx.expr(() => props.viewState.selection === todo);
|
|
3364
|
+
* return <div className={isSelected ? "todo todo-selected" : "todo"}>{todo.title}</div>
|
|
3365
|
+
* });
|
|
3366
|
+
*
|
|
3367
|
+
*/
|
|
3268
3368
|
function expr(expr, scope) {
|
|
3269
3369
|
if (!isComputingDerivation())
|
|
3270
3370
|
console.warn(getMessage("m013"));
|
|
@@ -3306,7 +3406,7 @@ function toJS(source, detectCycles, __alreadySeen) {
|
|
|
3306
3406
|
}
|
|
3307
3407
|
if (isObservableMap(source)) {
|
|
3308
3408
|
var res_1 = cache({});
|
|
3309
|
-
source.forEach(function (value, key) { return res_1[key] = toJS(value, detectCycles, __alreadySeen); });
|
|
3409
|
+
source.forEach(function (value, key) { return (res_1[key] = toJS(value, detectCycles, __alreadySeen)); });
|
|
3310
3410
|
return res_1;
|
|
3311
3411
|
}
|
|
3312
3412
|
if (isObservableValue(source))
|
|
@@ -3355,7 +3455,7 @@ function createTransformer(transformer, onCleanup) {
|
|
|
3355
3455
|
};
|
|
3356
3456
|
}
|
|
3357
3457
|
function getMemoizationId(object) {
|
|
3358
|
-
if (typeof object ===
|
|
3458
|
+
if (typeof object === "string" || typeof object === "number")
|
|
3359
3459
|
return object;
|
|
3360
3460
|
if (object === null || typeof object !== "object")
|
|
3361
3461
|
throw new Error("[mobx] transform expected some kind of object or primitive value, got: " + object);
|
|
@@ -3476,32 +3576,45 @@ var extras = {
|
|
|
3476
3576
|
var everything = {
|
|
3477
3577
|
Reaction: Reaction,
|
|
3478
3578
|
untracked: untracked,
|
|
3479
|
-
Atom: Atom,
|
|
3480
|
-
|
|
3579
|
+
Atom: Atom,
|
|
3580
|
+
BaseAtom: BaseAtom,
|
|
3581
|
+
useStrict: useStrict,
|
|
3582
|
+
isStrictModeEnabled: isStrictModeEnabled,
|
|
3481
3583
|
spy: spy,
|
|
3482
3584
|
comparer: comparer,
|
|
3483
|
-
asReference: asReference,
|
|
3585
|
+
asReference: asReference,
|
|
3586
|
+
asFlat: asFlat,
|
|
3587
|
+
asStructure: asStructure,
|
|
3588
|
+
asMap: asMap,
|
|
3484
3589
|
isModifierDescriptor: isModifierDescriptor,
|
|
3485
3590
|
isObservableObject: isObservableObject,
|
|
3486
3591
|
isBoxedObservable: isObservableValue,
|
|
3487
3592
|
isObservableArray: isObservableArray,
|
|
3488
|
-
ObservableMap: ObservableMap,
|
|
3593
|
+
ObservableMap: ObservableMap,
|
|
3594
|
+
isObservableMap: isObservableMap,
|
|
3595
|
+
map: map,
|
|
3489
3596
|
transaction: transaction,
|
|
3490
3597
|
observable: observable,
|
|
3491
3598
|
computed: computed,
|
|
3492
3599
|
isObservable: isObservable,
|
|
3493
3600
|
isComputed: isComputed,
|
|
3494
|
-
extendObservable: extendObservable,
|
|
3601
|
+
extendObservable: extendObservable,
|
|
3602
|
+
extendShallowObservable: extendShallowObservable,
|
|
3495
3603
|
observe: observe,
|
|
3496
3604
|
intercept: intercept,
|
|
3497
|
-
autorun: autorun,
|
|
3498
|
-
|
|
3605
|
+
autorun: autorun,
|
|
3606
|
+
autorunAsync: autorunAsync,
|
|
3607
|
+
when: when,
|
|
3608
|
+
reaction: reaction,
|
|
3609
|
+
action: action,
|
|
3610
|
+
isAction: isAction,
|
|
3611
|
+
runInAction: runInAction,
|
|
3499
3612
|
expr: expr,
|
|
3500
3613
|
toJS: toJS,
|
|
3501
3614
|
createTransformer: createTransformer,
|
|
3502
3615
|
whyRun: whyRun,
|
|
3503
3616
|
isArrayLike: isArrayLike,
|
|
3504
|
-
extras: extras
|
|
3617
|
+
extras: extras
|
|
3505
3618
|
};
|
|
3506
3619
|
var warnedAboutDefaultExport = false;
|
|
3507
3620
|
var _loop_1 = function (p) {
|
|
@@ -3510,9 +3623,9 @@ var _loop_1 = function (p) {
|
|
|
3510
3623
|
get: function () {
|
|
3511
3624
|
if (!warnedAboutDefaultExport) {
|
|
3512
3625
|
warnedAboutDefaultExport = true;
|
|
3513
|
-
console.warn(
|
|
3514
|
-
|
|
3515
|
-
|
|
3626
|
+
console.warn("Using default export (`import mobx from 'mobx'`) is deprecated " +
|
|
3627
|
+
"and won’t work in mobx@4.0.0\n" +
|
|
3628
|
+
"Use `import * as mobx from 'mobx'` instead");
|
|
3516
3629
|
}
|
|
3517
3630
|
return val;
|
|
3518
3631
|
}
|