function Atom(name, onBecomeObservedHandler, onBecomeUnobservedHandler) { if (name === void 0) { name = "Atom@" + getNextId(); } if (onBecomeObservedHandler === void 0) { onBecomeObservedHandler = noop; } if (onBecomeUnobservedHandler === void 0) { onBecomeUnobservedHandler = noop; } var _this = _super.call(this, name) || this; _this.name = name; _this.onBecomeObservedHandler = onBecomeObservedHandler; _this.onBecomeUnobservedHandler = onBecomeUnobservedHandler; _this.isPendingUnobservation = false; _this.isBeingTracked = false; return _this; }
n/a
function BaseAtom(name) { if (name === void 0) { name = "Atom@" + getNextId(); } this.name = name; this.isPendingUnobservation = true; this.observers = []; this.observersIndexes = {}; this.diffValue = 0; this.lastAccessedBy = 0; this.lowestObserverState = IDerivationState.NOT_TRACKING; }
n/a
function IObservableFactories() { }
n/a
IObservableFactories.prototype.deep = function () { if (arguments.length < 2) { return createModifierDescriptor(deepEnhancer, arguments[0]); } else { return deepDecorator.apply(null, arguments); } }
n/a
IObservableFactories.prototype.ref = function () { if (arguments.length < 2) { return createModifierDescriptor(referenceEnhancer, arguments[0]); } else { return refDecorator.apply(null, arguments); } }
n/a
function ObservableMap(initialData, enhancer, name) { if (enhancer === void 0) { enhancer = deepEnhancer; } if (name === void 0) { name = "ObservableMap@" + getNextId(); } this.enhancer = enhancer; this.name = name; this.$mobx = ObservableMapMarker; this._data = Object.create(null); this._hasMap = Object.create(null); this._keys = new ObservableArray(undefined, referenceEnhancer, this.name + ".keys()", true); this.interceptors = null; this.changeListeners = null; this.merge(initialData); }
n/a
function Reaction(name, onInvalidate) { if (name === void 0) { name = "Reaction@" + getNextId(); } this.name = name; this.onInvalidate = onInvalidate; this.observing = []; this.newObserving = []; this.dependenciesState = IDerivationState.NOT_TRACKING; this.diffValue = 0; this.runId = 0; this.unboundDepsCount = 0; this.__mapid = "#" + getNextId(); this.isDisposed = false; this._isScheduled = false; this._isTrackPending = false; this._isRunning = false; }
n/a
function action(arg1, arg2, arg3, arg4) { if (arguments.length === 1 && typeof arg1 === "function") return createAction(arg1.name || "<unnamed action>", arg1); if (arguments.length === 2 && typeof arg2 === "function") return createAction(arg1, arg2); if (arguments.length === 1 && typeof arg1 === "string") return namedActionDecorator(arg1); return namedActionDecorator(arg2).apply(null, arguments); }
n/a
function asFlat(value) { deprecated("asFlat is deprecated, use observable.shallow instead"); return observable.shallow(value); }
n/a
function asMap(data) { deprecated("asMap is deprecated, use observable.map or observable.shallowMap instead"); return observable.map(data || {}); }
n/a
function asReference(value) { deprecated("asReference is deprecated, use observable.ref instead"); return observable.ref(value); }
n/a
function asStructure(value) { deprecated("asStructure is deprecated. Use observable.struct, computed.struct or reaction options instead."); return observable.struct(value); }
n/a
function autorun(arg1, arg2, arg3) { var name, view, scope; if (typeof arg1 === "string") { name = arg1; view = arg2; scope = arg3; } else { name = arg1.name || ("Autorun@" + getNextId()); view = arg1; scope = arg2; } invariant(typeof view === "function", getMessage("m004")); invariant(isAction(view) === false, getMessage("m005")); if (scope) view = view.bind(scope); var reaction = new Reaction(name, function () { this.track(reactionRunner); }); function reactionRunner() { view(reaction); } reaction.schedule(); return reaction.getDisposer(); }
n/a
function autorunAsync(arg1, arg2, arg3, arg4) { var name, func, delay, scope; if (typeof arg1 === "string") { name = arg1; func = arg2; delay = arg3; scope = arg4; } else { name = arg1.name || ("AutorunAsync@" + getNextId()); func = arg1; delay = arg2; scope = arg3; } invariant(isAction(func) === false, getMessage("m006")); if (delay === void 0) delay = 1; if (scope) func = func.bind(scope); var isScheduled = false; var r = new Reaction(name, function () { if (!isScheduled) { isScheduled = true; setTimeout(function () { isScheduled = false; if (!r.isDisposed) r.track(reactionRunner); }, delay); } }); function reactionRunner() { func(r); } r.schedule(); return r.getDisposer(); }
...
* `toJSON` now serializes object trees with cycles as well. If you know the object tree is acyclic, pass in `false` as second parameter
for a performance gain.
# 1.1.0
* Exposed `ObservableMap` type
* Introduced `mobservable.untracked(block)`
* Introduced `mobservable.autorunAsync(block, delay)`
# 1.0.9
Removed accidental log message
# 1.0.7 / 1.0.8
...
function computed(arg1, arg2, arg3) { if (typeof arg2 === "string") { return computedDecorator.apply(null, arguments); } invariant(typeof arg1 === "function", getMessage("m011")); invariant(arguments.length < 3, getMessage("m012")); var opts = typeof arg2 === "object" ? arg2 : {}; opts.setter = typeof arg2 === "function" ? arg2 : opts.setter; return new ComputedValue(arg1, opts.context, opts.compareStructural || opts.struct || false, opts.name || arg1.name || "", opts .setter); }
n/a
function createTransformer(transformer, onCleanup) { invariant(typeof transformer === "function" && transformer.length < 2, "createTransformer expects a function that accepts one argument"); var objectCache = {}; var resetId = globalState.resetId; var Transformer = (function (_super) { __extends(Transformer, _super); function Transformer(sourceIdentifier, sourceObject) { var _this = _super.call(this, function () { return transformer(sourceObject); }, undefined, false, "Transformer-" + transformer.name + "-" + sourceIdentifier, undefined) || this; _this.sourceIdentifier = sourceIdentifier; _this.sourceObject = sourceObject; return _this; } Transformer.prototype.onBecomeUnobserved = function () { var lastValue = this.value; _super.prototype.onBecomeUnobserved.call(this); delete objectCache[this.sourceIdentifier]; if (onCleanup) onCleanup(lastValue, this.sourceObject); }; return Transformer; }(ComputedValue)); return function (object) { if (resetId !== globalState.resetId) { objectCache = {}; resetId = globalState.resetId; } var identifier = getMemoizationId(object); var reactiveTransformer = objectCache[identifier]; if (reactiveTransformer) return reactiveTransformer.get(); reactiveTransformer = objectCache[identifier] = new Transformer(identifier, object); return reactiveTransformer.get(); }; }
n/a
function expr(expr, scope) { if (!isComputingDerivation()) console.warn(getMessage("m013")); return computed(expr, { context: scope }).get(); }
n/a
function extendObservable(target) { var properties = []; for (var _i = 1; _i < arguments.length; _i++) { properties[_i - 1] = arguments[_i]; } return extendObservableHelper(target, deepEnhancer, properties); }
n/a
function extendShallowObservable(target) { var properties = []; for (var _i = 1; _i < arguments.length; _i++) { properties[_i - 1] = arguments[_i]; } return extendObservableHelper(target, referenceEnhancer, properties); }
n/a
function intercept(thing, propOrHandler, handler) { if (typeof handler === "function") return interceptProperty(thing, propOrHandler, handler); else return interceptInterceptable(thing, propOrHandler); }
...
if (typeof handler === "function")
return interceptProperty(thing, propOrHandler, handler);
else
return interceptInterceptable(thing, propOrHandler);
}
exports.intercept = intercept;
function interceptInterceptable(thing, handler) {
return getAdministration(thing).intercept(handler);
}
function interceptProperty(thing, property, handler) {
return getAdministration(thing, property).intercept(handler);
}
function isComputed(value, property) {
if (value === null || value === undefined)
return false;
...
function isAction(thing) { return typeof thing === "function" && thing.isMobxAction === true; }
n/a
function isArrayLike(x) { return Array.isArray(x) || isObservableArray(x); }
n/a
isBoxedObservable = function (x) { return isObject(x) && x[propName] === true; }
n/a
function isComputed(value, property) { if (value === null || value === undefined) return false; if (property !== undefined) { if (isObservableObject(value) === false) return false; var atom = getAtom(value, property); return isComputedValue(atom); } return isComputedValue(value); }
n/a
function isModifierDescriptor(thing) { return typeof thing === "object" && thing !== null && thing.isMobxModifierDescriptor === true; }
n/a
function isObservable(value, property) { if (value === null || value === undefined) return false; if (property !== undefined) { if (isObservableArray(value) || isObservableMap(value)) throw new Error(getMessage("m019")); else if (isObservableObject(value)) { var o = value.$mobx; return o.values && !!o.values[property]; } return false; } return isObservableObject(value) || !!value.$mobx || isAtom(value) || isReaction(value) || isComputedValue(value); }
n/a
function isObservableArray(thing) { return isObject(thing) && isObservableArrayAdministration(thing.$mobx); }
n/a
isObservableMap = function (x) { return isObject(x) && x[propName] === true; }
n/a
function isObservableObject(thing) { if (isObject(thing)) { runLazyInitializers(thing); return isObservableObjectAdministration(thing.$mobx); } return false; }
n/a
function isStrictModeEnabled() { return globalState.strictMode; }
n/a
function map(initialValues) { deprecated("`mobx.map` is deprecated, use `new ObservableMap` or `mobx.observable.map` instead"); return observable.map(initialValues); }
...
### Factories per observable type
There are now explicit methods to create an observable of a specific type.
* `observable.object(props, name?)` creates a new observable object, by cloning the give props and making them observable
* `observable.array(initialValues, name?)`. Take a guess..
* `observable.map(initialValues, name?)`
* `observable.box(initialValue, name?)`. Creates a [boxed](http://mobxjs.github.io/mobx/refguide/boxed.html) value, which can be
read from / written to using `.get()` and `.set(newValue)`
* `observable(value)`, as-is, based on the type of `value`, uses any of the above four functions to create a new observable.
### Shallow factories per type
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
...
function createObservable(v) { if (v === void 0) { v = undefined; } if (typeof arguments[1] === "string") return deepDecorator.apply(null, arguments); invariant(arguments.length <= 1, getMessage("m021")); invariant(!isModifierDescriptor(v), getMessage("m020")); if (isObservable(v)) return v; var res = deepEnhancer(v, undefined, undefined); if (res !== v) return res; return observable.box(v); }
...
# 1.1.7
* Fixed #77: package consumers with --noImplicitAny should be able to build
# 1.1.6
* Introduced `mobservable.fastArray(array)`, in addition to `mobservable.observable(array
)`. Which is much faster when adding items but doesn't support enumerability (`for (var idx in ar) ..` loops).
* Introduced `observableArray.peek()`, for fast access to the array values. Should be used read-only.
# 1.1.5
* Fixed 71: transactions should not influence running computations
# 1.1.4
...
function observe(thing, propOrCb, cbOrFire, fireImmediately) { if (typeof cbOrFire === "function") return observeObservableProperty(thing, propOrCb, cbOrFire, fireImmediately); else return observeObservable(thing, propOrCb, cbOrFire); }
...
if (typeof cbOrFire === "function")
return observeObservableProperty(thing, propOrCb, cbOrFire, fireImmediately);
else
return observeObservable(thing, propOrCb, cbOrFire);
}
exports.observe = observe;
function observeObservable(thing, listener, fireImmediately) {
return getAdministration(thing).observe(listener, fireImmediately);
}
function observeObservableProperty(thing, property, listener, fireImmediately) {
return getAdministration(thing, property).observe(listener, fireImmediately);
}
function toJS(source, detectCycles, __alreadySeen) {
if (detectCycles === void 0) { detectCycles = true; }
if (__alreadySeen === void 0) { __alreadySeen = []; }
...
function reaction(expression, effect, arg3) { if (arguments.length > 3) { fail(getMessage("m007")); } if (isModifierDescriptor(expression)) { fail(getMessage("m008")); } var opts; if (typeof arg3 === "object") { opts = arg3; } else { opts = {}; } opts.name = opts.name || expression.name || effect.name || ("Reaction@" + getNextId()); opts.fireImmediately = arg3 === true || opts.fireImmediately === true; opts.delay = opts.delay || 0; opts.compareStructural = opts.compareStructural || opts.struct || false; effect = action(opts.name, opts.context ? effect.bind(opts.context) : effect); if (opts.context) { expression = expression.bind(opts.context); } var firstTime = true; var isScheduled = false; var nextValue; var r = new Reaction(opts.name, function () { if (firstTime || opts.delay < 1) { reactionRunner(); } else if (!isScheduled) { isScheduled = true; setTimeout(function () { isScheduled = false; reactionRunner(); }, opts.delay); } }); function reactionRunner() { if (r.isDisposed) return; var changed = false; r.track(function () { var v = expression(r); changed = valueDidChange(opts.compareStructural, nextValue, v); nextValue = v; }); if (firstTime && opts.fireImmediately) effect(nextValue, r); if (!firstTime && changed === true) effect(nextValue, r); if (firstTime) firstTime = false; } r.schedule(); return r.getDisposer(); }
n/a
function runInAction(arg1, arg2, arg3) { var actionName = typeof arg1 === "string" ? arg1 : arg1.name || "<unnamed action>"; var fn = typeof arg1 === "function" ? arg1 : arg2; var scope = typeof arg1 === "function" ? arg2 : arg3; invariant(typeof fn === "function", getMessage("m002")); invariant(fn.length === 0, getMessage("m003")); invariant(typeof actionName === "string" && actionName.length > 0, "actions should have valid names, got: '" + actionName + "'"); return executeAction(actionName, fn, scope, undefined); }
n/a
function spy(listener) { globalState.spyListeners.push(listener); return once(function () { var idx = globalState.spyListeners.indexOf(listener); if (idx !== -1) globalState.spyListeners.splice(idx, 1); }); }
n/a
function toJS(source, detectCycles, __alreadySeen) { if (detectCycles === void 0) { detectCycles = true; } if (__alreadySeen === void 0) { __alreadySeen = []; } function cache(value) { if (detectCycles) __alreadySeen.push([source, value]); return value; } if (isObservable(source)) { if (detectCycles && __alreadySeen === null) __alreadySeen = []; if (detectCycles && source !== null && typeof source === "object") { for (var i = 0, l = __alreadySeen.length; i < l; i++) if (__alreadySeen[i][0] === source) return __alreadySeen[i][1]; } if (isObservableArray(source)) { var res = cache([]); var toAdd = source.map(function (value) { return toJS(value, detectCycles, __alreadySeen); }); res.length = toAdd.length; for (var i = 0, l = toAdd.length; i < l; i++) res[i] = toAdd[i]; return res; } if (isObservableObject(source)) { var res = cache({}); for (var key in source) res[key] = toJS(source[key], detectCycles, __alreadySeen); return res; } if (isObservableMap(source)) { var res_1 = cache({}); source.forEach(function (value, key) { return res_1[key] = toJS(value, detectCycles, __alreadySeen); }); return res_1; } if (isObservableValue(source)) return toJS(source.get(), detectCycles, __alreadySeen); } return source; }
...
ObservableArray.prototype.replace = function (newItems) {
return this.$mobx.spliceWithArray(0, this.$mobx.values.length, newItems);
};
ObservableArray.prototype.toJS = function () {
return this.slice();
};
ObservableArray.prototype.toJSON = function () {
return this.toJS();
};
ObservableArray.prototype.peek = function () {
return this.$mobx.values;
};
ObservableArray.prototype.find = function (predicate, thisArg, fromIndex) {
if (fromIndex === void 0) { fromIndex = 0; }
this.$mobx.atom.reportObserved();
...
function transaction(action, thisArg) { if (thisArg === void 0) { thisArg = undefined; } deprecated(getMessage("m023")); return runInTransaction.apply(undefined, arguments); }
n/a
function untracked(action) { var prev = untrackedStart(); var res = action(); untrackedEnd(prev); return res; }
...
# 1.1.1
* `toJSON` now serializes object trees with cycles as well. If you know the object tree is acyclic, pass in `false` as second parameter
for a performance gain.
# 1.1.0
* Exposed `ObservableMap` type
* Introduced `mobservable.untracked(block)`
* Introduced `mobservable.autorunAsync(block, delay)`
# 1.0.9
Removed accidental log message
# 1.0.7 / 1.0.8
...
function useStrict(strict) { invariant(globalState.trackingDerivation === null, getMessage("m028")); globalState.strictMode = strict; globalState.allowStateChanges = !strict; }
n/a
function when(arg1, arg2, arg3, arg4) { var name, predicate, effect, scope; if (typeof arg1 === "string") { name = arg1; predicate = arg2; effect = arg3; scope = arg4; } else { name = ("When@" + getNextId()); predicate = arg1; effect = arg2; scope = arg3; } var disposer = autorun(name, function (r) { if (predicate.call(scope)) { r.dispose(); var prevUntracked = untrackedStart(); effect.call(scope); untrackedEnd(prevUntracked); } }); return disposer; }
n/a
function whyRun(thing, prop) { switch (arguments.length) { case 0: thing = globalState.trackingDerivation; if (!thing) return log(getMessage("m024")); break; case 2: thing = getAtom(thing, prop); break; } thing = getAtom(thing); if (isComputedValue(thing)) return log(thing.whyRun()); else if (isReaction(thing)) return log(thing.whyRun()); return fail(getMessage("m025")); }
...
break;
case 2:
thing = getAtom(thing, prop);
break;
}
thing = getAtom(thing);
if (isComputedValue(thing))
return log(thing.whyRun());
else if (isReaction(thing))
return log(thing.whyRun());
return fail(getMessage("m025"));
}
exports.whyRun = whyRun;
function createAction(actionName, fn) {
invariant(typeof fn === "function", getMessage("m026"));
...
function Atom(name, onBecomeObservedHandler, onBecomeUnobservedHandler) { if (name === void 0) { name = "Atom@" + getNextId(); } if (onBecomeObservedHandler === void 0) { onBecomeObservedHandler = noop; } if (onBecomeUnobservedHandler === void 0) { onBecomeUnobservedHandler = noop; } var _this = _super.call(this, name) || this; _this.name = name; _this.onBecomeObservedHandler = onBecomeObservedHandler; _this.onBecomeUnobservedHandler = onBecomeUnobservedHandler; _this.isPendingUnobservation = false; _this.isBeingTracked = false; return _this; }
n/a
function Atom(name, onBecomeObservedHandler, onBecomeUnobservedHandler) { if (name === void 0) { name = "Atom@" + getNextId(); } if (onBecomeObservedHandler === void 0) { onBecomeObservedHandler = noop; } if (onBecomeUnobservedHandler === void 0) { onBecomeUnobservedHandler = noop; } var _this = _super.call(this, name) || this; _this.name = name; _this.onBecomeObservedHandler = onBecomeObservedHandler; _this.onBecomeUnobservedHandler = onBecomeUnobservedHandler; _this.isPendingUnobservation = false; _this.isBeingTracked = false; return _this; }
n/a
onBecomeUnobserved = function () { this.isBeingTracked = false; this.onBecomeUnobservedHandler(); }
...
if (--globalState.inBatch === 0) {
runReactions();
var list = globalState.pendingUnobservations;
for (var i = 0; i < list.length; i++) {
var observable_1 = list[i];
observable_1.isPendingUnobservation = false;
if (observable_1.observers.length === 0) {
observable_1.onBecomeUnobserved();
}
}
globalState.pendingUnobservations = [];
}
}
function reportObserved(observable) {
var derivation = globalState.trackingDerivation;
...
reportObserved = function () { startBatch(); _super.prototype.reportObserved.call(this); if (!this.isBeingTracked) { this.isBeingTracked = true; this.onBecomeObservedHandler(); } endBatch(); return !!globalState.trackingDerivation; }
...
removed: [],
removedCount: 0
});
}
return registerListener(this, listener);
};
ObservableArrayAdministration.prototype.getArrayLength = function () {
this.atom.reportObserved();
return this.values.length;
};
ObservableArrayAdministration.prototype.setArrayLength = function (newLength) {
if (typeof newLength !== "number" || newLength < 0)
throw new Error("[mobx.array] Out of range: " + newLength);
var currentLength = this.values.length;
if (newLength === currentLength)
...
function BaseAtom(name) { if (name === void 0) { name = "Atom@" + getNextId(); } this.name = name; this.isPendingUnobservation = true; this.observers = []; this.observersIndexes = {}; this.diffValue = 0; this.lastAccessedBy = 0; this.lowestObserverState = IDerivationState.NOT_TRACKING; }
n/a
onBecomeUnobserved = function () { }
...
if (--globalState.inBatch === 0) {
runReactions();
var list = globalState.pendingUnobservations;
for (var i = 0; i < list.length; i++) {
var observable_1 = list[i];
observable_1.isPendingUnobservation = false;
if (observable_1.observers.length === 0) {
observable_1.onBecomeUnobserved();
}
}
globalState.pendingUnobservations = [];
}
}
function reportObserved(observable) {
var derivation = globalState.trackingDerivation;
...
reportChanged = function () { startBatch(); propagateChanged(this); endBatch(); }
...
var change = notify || notifySpy ? {
object: this.array,
type: "update",
index: index, newValue: newValue, oldValue: oldValue
} : null;
if (notifySpy)
spyReportStart(change);
this.atom.reportChanged();
if (notify)
notifyListeners(this, change);
if (notifySpy)
spyReportEnd();
};
ObservableArrayAdministration.prototype.notifyArraySplice = function (index, added, removed) {
var notifySpy = !this.owned && isSpyEnabled();
...
reportObserved = function () { reportObserved(this); }
...
removed: [],
removedCount: 0
});
}
return registerListener(this, listener);
};
ObservableArrayAdministration.prototype.getArrayLength = function () {
this.atom.reportObserved();
return this.values.length;
};
ObservableArrayAdministration.prototype.setArrayLength = function (newLength) {
if (typeof newLength !== "number" || newLength < 0)
throw new Error("[mobx.array] Out of range: " + newLength);
var currentLength = this.values.length;
if (newLength === currentLength)
...
toString = function () { return this.name; }
...
prevValue = newValue;
});
};
ComputedValue.prototype.toJSON = function () {
return this.get();
};
ComputedValue.prototype.toString = function () {
return this.name + "[" + this.derivation.toString() + "]";
};
ComputedValue.prototype.valueOf = function () {
return toPrimitive(this.get());
};
;
ComputedValue.prototype.whyRun = function () {
var isTracking = Boolean(globalState.trackingDerivation);
...
function IObservableFactories() { }
n/a
array = function (initialValues, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("array"); return new ObservableArray(initialValues, deepEnhancer, name); }
...
See [#649](https://github.com/mobxjs/mobx/issues/649)
### Factories per observable type
There are now explicit methods to create an observable of a specific type.
* `observable.object(props, name?)` creates a new observable object, by cloning the give props and making them observable
* `observable.array(initialValues, name?)`. Take a guess..
* `observable.map(initialValues, name?)`
* `observable.box(initialValue, name?)`. Creates a [boxed](http://mobxjs.github.io/mobx/refguide/boxed.html) value, which can be
read from / written to using `.get()` and `.set(newValue)`
* `observable(value)`, as-is, based on the type of `value`, uses any of the above four functions to create a new observable.
### Shallow factories per type
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
...
box = function (value, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("box"); return new ObservableValue(value, deepEnhancer, name); }
...
### Factories per observable type
There are now explicit methods to create an observable of a specific type.
* `observable.object(props, name?)` creates a new observable object, by cloning the give props and making them observable
* `observable.array(initialValues, name?)`. Take a guess..
* `observable.map(initialValues, name?)`
* `observable.box(initialValue, name?)`. Creates a [boxed](http://mobxjs.github.io/mobx
/refguide/boxed.html) value, which can be read from / written to using `.get()` and `.set(newValue)`
* `observable(value)`, as-is, based on the type of `value`, uses any of the above four functions to create a new observable.
### Shallow factories per type
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
For example when storing objects from external libraries.
...
deep = function () { if (arguments.length < 2) { return createModifierDescriptor(deepEnhancer, arguments[0]); } else { return deepDecorator.apply(null, arguments); } }
n/a
map = function (initialValues, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("map"); return new ObservableMap(initialValues, deepEnhancer, name); }
...
### Factories per observable type
There are now explicit methods to create an observable of a specific type.
* `observable.object(props, name?)` creates a new observable object, by cloning the give props and making them observable
* `observable.array(initialValues, name?)`. Take a guess..
* `observable.map(initialValues, name?)`
* `observable.box(initialValue, name?)`. Creates a [boxed](http://mobxjs.github.io/mobx/refguide/boxed.html) value, which can be
read from / written to using `.get()` and `.set(newValue)`
* `observable(value)`, as-is, based on the type of `value`, uses any of the above four functions to create a new observable.
### Shallow factories per type
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
...
object = function (props, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("object"); var res = {}; asObservableObject(res, name); extendObservable(res, props); return res; }
...
See [#649](https://github.com/mobxjs/mobx/issues/649)
### Factories per observable type
There are now explicit methods to create an observable of a specific type.
* `observable.object(props, name?)` creates a new observable object, by cloning the give
props and making them observable
* `observable.array(initialValues, name?)`. Take a guess..
* `observable.map(initialValues, name?)`
* `observable.box(initialValue, name?)`. Creates a [boxed](http://mobxjs.github.io/mobx/refguide/boxed.html) value, which can be
read from / written to using `.get()` and `.set(newValue)`
* `observable(value)`, as-is, based on the type of `value`, uses any of the above four functions to create a new observable.
### Shallow factories per type
...
ref = function () { if (arguments.length < 2) { return createModifierDescriptor(referenceEnhancer, arguments[0]); } else { return refDecorator.apply(null, arguments); } }
...
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i](change);
}
untrackedEnd(prevU);
}
function asReference(value) {
deprecated("asReference is deprecated, use observable.ref instead");
return observable.ref(value);
}
exports.asReference = asReference;
function asStructure(value) {
deprecated("asStructure is deprecated. Use observable.struct, computed.struct or reaction options instead.");
return observable.struct(value);
}
exports.asStructure = asStructure;
...
shallow = function () { if (arguments.length < 2) { return createModifierDescriptor(shallowEnhancer, arguments[0]); } else { return shallowDecorator.apply(null, arguments); } }
...
```
Or as property modifier in combination with `observable.object` / `observable.extendObservable`.
Note that modifiers always 'stick' to the property. So they will remain in effect even if a new value is assigned.
```javascript
const taskStore = observable({
tasks: observable.shallow([])
})
```
See [modifiers](http://mobxjs.github.io/mobx/refguide/modifiers.html)
### `computed` api has been simplified
...
shallowArray = function (initialValues, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("shallowArray"); return new ObservableArray(initialValues, referenceEnhancer, name); }
...
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
For example when storing objects from external libraries.
In MobX 2 you needed to use `asFlat` or `asReference` modifiers for this.
In MobX 3, there are factories to directly create non-converting data structures:
* `observable.shallowObject(props, name?)`
* `observable.shallowArray(initialValues, name?)`
* `observable.shallowMap(initialValues, name?)`
* `observable.shallowBox(initialValue, name?)`
So for example, `observable.shallowArray([todo1, todo2])` will create an observable array, but it won't try to convert the
todos inside the array into observables as well.
### Shallow properties
...
shallowBox = function (value, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("shallowBox"); return new ObservableValue(value, referenceEnhancer, name); }
...
For example when storing objects from external libraries.
In MobX 2 you needed to use `asFlat` or `asReference` modifiers for this.
In MobX 3, there are factories to directly create non-converting data structures:
* `observable.shallowObject(props, name?)`
* `observable.shallowArray(initialValues, name?)`
* `observable.shallowMap(initialValues, name?)`
* `observable.shallowBox(initialValue, name?)`
So for example, `observable.shallowArray([todo1, todo2])` will create an observable array, but it won't try to convert the
todos inside the array into observables as well.
### Shallow properties
The `@observable` decorator can still be used to introduce observable properties. And like in MobX 2, it will automatically convert
its values.
...
shallowMap = function (initialValues, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("shallowMap"); return new ObservableMap(initialValues, referenceEnhancer, name); }
...
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
For example when storing objects from external libraries.
In MobX 2 you needed to use `asFlat` or `asReference` modifiers for this.
In MobX 3, there are factories to directly create non-converting data structures:
* `observable.shallowObject(props, name?)`
* `observable.shallowArray(initialValues, name?)`
* `observable.shallowMap(initialValues, name?)`
* `observable.shallowBox(initialValue, name?)`
So for example, `observable.shallowArray([todo1, todo2])` will create an observable array, but it won't try to convert the
todos inside the array into observables as well.
### Shallow properties
The `@observable` decorator can still be used to introduce observable properties. And like in MobX 2, it will automatically convert
its values.
...
shallowObject = function (props, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("shallowObject"); var res = {}; asObservableObject(res, name); extendShallowObservable(res, props); return res; }
...
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
For example when storing objects from external libraries.
In MobX 2 you needed to use `asFlat` or `asReference` modifiers for this.
In MobX 3, there are factories to directly create non-converting data structures:
* `observable.shallowObject(props, name?)`
* `observable.shallowArray(initialValues, name?)`
* `observable.shallowMap(initialValues, name?)`
* `observable.shallowBox(initialValue, name?)`
So for example, `observable.shallowArray([todo1, todo2])` will create an observable array, but it won't try to convert the
todos inside the array into observables as well.
### Shallow properties
...
struct = function () { if (arguments.length < 2) { return createModifierDescriptor(deepStructEnhancer, arguments[0]); } else { return deepStructDecorator.apply(null, arguments); } }
...
function asReference(value) {
deprecated("asReference is deprecated, use observable.ref instead");
return observable.ref(value);
}
exports.asReference = asReference;
function asStructure(value) {
deprecated("asStructure is deprecated. Use observable.struct, computed.struct or reaction options instead.");
return observable.struct(value);
}
exports.asStructure = asStructure;
function asFlat(value) {
deprecated("asFlat is deprecated, use observable.shallow instead");
return observable.shallow(value);
}
exports.asFlat = asFlat;
...
deep = function () { if (arguments.length < 2) { return createModifierDescriptor(deepEnhancer, arguments[0]); } else { return deepDecorator.apply(null, arguments); } }
n/a
struct = function () { if (arguments.length < 2) { return createModifierDescriptor(deepStructEnhancer, arguments[0]); } else { return deepStructDecorator.apply(null, arguments); } }
...
function asReference(value) {
deprecated("asReference is deprecated, use observable.ref instead");
return observable.ref(value);
}
exports.asReference = asReference;
function asStructure(value) {
deprecated("asStructure is deprecated. Use observable.struct, computed.struct or reaction options instead.");
return observable.struct(value);
}
exports.asStructure = asStructure;
function asFlat(value) {
deprecated("asFlat is deprecated, use observable.shallow instead");
return observable.shallow(value);
}
exports.asFlat = asFlat;
...
ref = function () { if (arguments.length < 2) { return createModifierDescriptor(referenceEnhancer, arguments[0]); } else { return refDecorator.apply(null, arguments); } }
...
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i](change);
}
untrackedEnd(prevU);
}
function asReference(value) {
deprecated("asReference is deprecated, use observable.ref instead");
return observable.ref(value);
}
exports.asReference = asReference;
function asStructure(value) {
deprecated("asStructure is deprecated. Use observable.struct, computed.struct or reaction options instead.");
return observable.struct(value);
}
exports.asStructure = asStructure;
...
struct = function () { if (arguments.length < 2) { return createModifierDescriptor(refStructEnhancer, arguments[0]); } else { return refStructDecorator.apply(null, arguments); } }
...
function asReference(value) {
deprecated("asReference is deprecated, use observable.ref instead");
return observable.ref(value);
}
exports.asReference = asReference;
function asStructure(value) {
deprecated("asStructure is deprecated. Use observable.struct, computed.struct or reaction options instead.");
return observable.struct(value);
}
exports.asStructure = asStructure;
function asFlat(value) {
deprecated("asFlat is deprecated, use observable.shallow instead");
return observable.shallow(value);
}
exports.asFlat = asFlat;
...
function ObservableMap(initialData, enhancer, name) { if (enhancer === void 0) { enhancer = deepEnhancer; } if (name === void 0) { name = "ObservableMap@" + getNextId(); } this.enhancer = enhancer; this.name = name; this.$mobx = ObservableMapMarker; this._data = Object.create(null); this._hasMap = Object.create(null); this._keys = new ObservableArray(undefined, referenceEnhancer, this.name + ".keys()", true); this.interceptors = null; this.changeListeners = null; this.merge(initialData); }
n/a
_addValue = function (name, newValue) { var _this = this; runInTransaction(function () { var observable = _this._data[name] = new ObservableValue(newValue, _this.enhancer, _this.name + "." + name, false); newValue = observable.value; _this._updateHasMapEntry(name, true); _this._keys.push(name); }); var notifySpy = isSpyEnabled(); var notify = hasListeners(this); var change = notify || notifySpy ? { type: "add", object: this, name: name, newValue: newValue } : null; if (notifySpy) spyReportStart(change); if (notify) notifyListeners(this, change); if (notifySpy) spyReportEnd(); }
...
return this;
value = change.newValue;
}
if (hasKey) {
this._updateValue(key, value);
}
else {
this._addValue(key, value);
}
return this;
};
ObservableMap.prototype.delete = function (key) {
var _this = this;
this.assertValidKey(key);
key = "" + key;
...
_has = function (key) { return typeof this._data[key] !== "undefined"; }
...
if (this._hasMap[key])
return this._hasMap[key].get();
return this._updateHasMapEntry(key, false).get();
};
ObservableMap.prototype.set = function (key, value) {
this.assertValidKey(key);
key = "" + key;
var hasKey = this._has(key);
if (hasInterceptors(this)) {
var change = interceptChange(this, {
type: hasKey ? "update" : "add",
object: this,
newValue: value,
name: key
});
...
_updateHasMapEntry = function (key, value) { var entry = this._hasMap[key]; if (entry) { entry.setNewValue(value); } else { entry = this._hasMap[key] = new ObservableValue(value, referenceEnhancer, this.name + "." + key + "?", false); } return entry; }
...
};
ObservableMap.prototype.has = function (key) {
if (!this.isValidKey(key))
return false;
key = "" + key;
if (this._hasMap[key])
return this._hasMap[key].get();
return this._updateHasMapEntry(key, false).get();
};
ObservableMap.prototype.set = function (key, value) {
this.assertValidKey(key);
key = "" + key;
var hasKey = this._has(key);
if (hasInterceptors(this)) {
var change = interceptChange(this, {
...
_updateValue = function (name, newValue) { var observable = this._data[name]; newValue = observable.prepareNewValue(newValue); if (newValue !== UNCHANGED) { var notifySpy = isSpyEnabled(); var notify = hasListeners(this); var change = notify || notifySpy ? { type: "update", object: this, oldValue: observable.value, name: name, newValue: newValue } : null; if (notifySpy) spyReportStart(change); observable.setNewValue(newValue); if (notify) notifyListeners(this, change); if (notifySpy) spyReportEnd(); } }
...
name: key
});
if (!change)
return this;
value = change.newValue;
}
if (hasKey) {
this._updateValue(key, value);
}
else {
this._addValue(key, value);
}
return this;
};
ObservableMap.prototype.delete = function (key) {
...
assertValidKey = function (key) { if (!this.isValidKey(key)) throw new Error("[mobx.map] Invalid key: '" + key + "', only strings, numbers and booleans are accepted as key in observable maps."); }
...
return false;
key = "" + key;
if (this._hasMap[key])
return this._hasMap[key].get();
return this._updateHasMapEntry(key, false).get();
};
ObservableMap.prototype.set = function (key, value) {
this.assertValidKey(key);
key = "" + key;
var hasKey = this._has(key);
if (hasInterceptors(this)) {
var change = interceptChange(this, {
type: hasKey ? "update" : "add",
object: this,
newValue: value,
...
clear = function () { var _this = this; runInTransaction(function () { untracked(function () { _this.keys().forEach(_this.delete, _this); }); }); }
...
_this.keys().forEach(_this.delete, _this);
});
});
};
ObservableMap.prototype.replace = function (values) {
var _this = this;
runInTransaction(function () {
_this.clear();
_this.merge(values);
});
return this;
};
Object.defineProperty(ObservableMap.prototype, "size", {
get: function () {
return this._keys.length;
...
delete = function (key) { var _this = this; this.assertValidKey(key); key = "" + key; if (hasInterceptors(this)) { var change = interceptChange(this, { type: "delete", object: this, name: key }); if (!change) return false; } if (this._has(key)) { var notifySpy = isSpyEnabled(); var notify = hasListeners(this); var change = notify || notifySpy ? { type: "delete", object: this, oldValue: this._data[key].value, name: key } : null; if (notifySpy) spyReportStart(change); runInTransaction(function () { _this._keys.remove(key); _this._updateHasMapEntry(key, false); var observable = _this._data[key]; observable.setNewValue(undefined); _this._data[key] = undefined; }); if (notify) notifyListeners(this, change); if (notifySpy) spyReportEnd(); return true; } return false; }
n/a
entries = function () { var _this = this; return arrayAsIterator(this._keys.map(function (key) { return [key, _this.get(key)]; })); }
...
ObservableMap.prototype.intercept = function (handler) {
return registerInterceptor(this, handler);
};
return ObservableMap;
}());
exports.ObservableMap = ObservableMap;
declareIterator(ObservableMap.prototype, function () {
return this.entries();
});
function map(initialValues) {
deprecated("`mobx.map` is deprecated, use `new ObservableMap` or `mobx.observable.map` instead");
return observable.map(initialValues);
}
exports.map = map;
var isObservableMap = createInstanceofPredicate("ObservableMap", ObservableMap);
...
forEach = function (callback, thisArg) { var _this = this; this.keys().forEach(function (key) { return callback.call(thisArg, _this.get(key), key, _this); }); }
...
return extendObservableHelper(target, referenceEnhancer, properties);
}
exports.extendShallowObservable = extendShallowObservable;
function extendObservableHelper(target, defaultEnhancer, properties) {
invariant(arguments.length >= 2, getMessage("m014"));
invariant(typeof target === "object", getMessage("m015"));
invariant(!(isObservableMap(target)), getMessage("m016"));
properties.forEach(function (propSet) {
invariant(typeof propSet === "object", getMessage("m017"));
invariant(!isObservable(propSet), getMessage("m018"));
});
var adm = asObservableObject(target);
var definedProps = {};
for (var i = properties.length - 1; i >= 0; i--) {
var propSet = properties[i];
...
get = function (key) { key = "" + key; if (this.has(key)) return this._data[key].get(); return undefined; }
...
# 3.1.9
* Introduced explicit `.get(index)` and `.set(index, value)` methods on observable arrays
, for issues that have trouble handling many property descriptors on objects. See also #574
* Made sure it is safe to call `onBecomeObserved` twice in row, fixes #874, #898
* Fixed typings of `IReactionDisposer`
# 3.1.8
* Fixed edge case where `autorun` was not triggered again if a computed value was invalidated by the reaction itself, see [#916](
https://github.com/mobxjs/mobx/issues/916), by @andykog
* Added support for primtive keys in `createTransformer`, See #920 by @dnakov
...
has = function (key) { if (!this.isValidKey(key)) return false; key = "" + key; if (this._hasMap[key]) return this._hasMap[key].get(); return this._updateHasMapEntry(key, false).get(); }
...
if (notify)
notifyListeners(this, change);
if (notifySpy)
spyReportEnd();
};
ObservableMap.prototype.get = function (key) {
key = "" + key;
if (this.has(key))
return this._data[key].get();
return undefined;
};
ObservableMap.prototype.keys = function () {
return arrayAsIterator(this._keys.slice());
};
ObservableMap.prototype.values = function () {
...
intercept = function (handler) { return registerInterceptor(this, handler); }
...
if (typeof handler === "function")
return interceptProperty(thing, propOrHandler, handler);
else
return interceptInterceptable(thing, propOrHandler);
}
exports.intercept = intercept;
function interceptInterceptable(thing, handler) {
return getAdministration(thing).intercept(handler);
}
function interceptProperty(thing, property, handler) {
return getAdministration(thing, property).intercept(handler);
}
function isComputed(value, property) {
if (value === null || value === undefined)
return false;
...
isValidKey = function (key) { if (key === null || key === undefined) return false; if (typeof key === "string" || typeof key === "number" || typeof key === "boolean") return true; return false; }
...
this.changeListeners = null;
this.merge(initialData);
}
ObservableMap.prototype._has = function (key) {
return typeof this._data[key] !== "undefined";
};
ObservableMap.prototype.has = function (key) {
if (!this.isValidKey(key))
return false;
key = "" + key;
if (this._hasMap[key])
return this._hasMap[key].get();
return this._updateHasMapEntry(key, false).get();
};
ObservableMap.prototype.set = function (key, value) {
...
keys = function () { return arrayAsIterator(this._keys.slice()); }
...
* Fixed #377: `toJS` serialization of Dates and Regexes preserves the original values
* Fixed #379: `@action` decorated methods can now be inherited / overriden
# 2.3.3
* Fixed #186: Log a warning instead of an error if an exception is thrown in a derivation. Fixes issue where React Native would
produce unusable error screens (because it shows the first logged error)
* Fixed #333: Fixed some interoperability issues in combination with `Reflect` / `InversifyJS` decorators. @andykog
* Fixed #333: `@observable` class properties are now _owned_ by their instance again, meaning they will show up in `Object.keys()` and `.hasOwnProperty` @andykog
# 2.3.2
* Fixed #328: Fixed exception when inspecting observable in `onBecomeObserved`
* Fixed #341: `array.find` now returns `undefined` instead of `null` when nothing was found, behavior now matches the docs. (By @
hellectronic)
# 2.3.1
...
merge = function (other) { var _this = this; if (isObservableMap(other)) { other = other.toJS(); } runInTransaction(function () { if (isPlainObject(other)) Object.keys(other).forEach(function (key) { return _this.set(key, other[key]); }); else if (Array.isArray(other)) other.forEach(function (_a) { var key = _a[0], value = _a[1]; return _this.set(key, value); }); else if (isES6Map(other)) other.forEach(function (value, key) { return _this.set(key, value); }); else if (other !== null && other !== undefined) fail("Cannot initialize map from " + other); }); return this; }
...
this.name = name;
this.$mobx = ObservableMapMarker;
this._data = Object.create(null);
this._hasMap = Object.create(null);
this._keys = new ObservableArray(undefined, referenceEnhancer, this.name + ".keys()", true);
this.interceptors = null;
this.changeListeners = null;
this.merge(initialData);
}
ObservableMap.prototype._has = function (key) {
return typeof this._data[key] !== "undefined";
};
ObservableMap.prototype.has = function (key) {
if (!this.isValidKey(key))
return false;
...
observe = function (listener, fireImmediately) { invariant(fireImmediately !== true, getMessage("m033")); return registerListener(this, listener); }
...
if (typeof cbOrFire === "function")
return observeObservableProperty(thing, propOrCb, cbOrFire, fireImmediately);
else
return observeObservable(thing, propOrCb, cbOrFire);
}
exports.observe = observe;
function observeObservable(thing, listener, fireImmediately) {
return getAdministration(thing).observe(listener, fireImmediately);
}
function observeObservableProperty(thing, property, listener, fireImmediately) {
return getAdministration(thing, property).observe(listener, fireImmediately);
}
function toJS(source, detectCycles, __alreadySeen) {
if (detectCycles === void 0) { detectCycles = true; }
if (__alreadySeen === void 0) { __alreadySeen = []; }
...
replace = function (values) { var _this = this; runInTransaction(function () { _this.clear(); _this.merge(values); }); return this; }
...
### Other changes
* **Breaking change:** The arguments to `observe` listeners for computed and boxed observables have changed and are now consistent
with the other apis. Instead of invoking the callback with `(newValue: T, oldValue: T)` they are now invoked with a single change
object: `(change: {newValue: T, oldValue: T, object, type: "update"})`
* Using transaction is now deprecated, use `action` or `runInAction` instead. Transactions now will enter an `untracked` block as
well, just as actions, which removes the conceptual difference.
* Upgraded to typescript 2
* It is now possible to pass ES6 Maps to `observable` / observable maps. The map will be converted to an observable map (if keys
are string like)
* Made `action` more debug friendly, it should now be easier to step through
* ObservableMap now has an additional method, `.replace(data)`, which is a combination
of `clear()` and `merge(data)`.
* Passing a function to `observable` will now create a boxed observable refering to that function
* Fixed #603: exceptions in transaction breaks future reactions
* Fixed #698: createTransformer should support default arguments
* Transactions are no longer reported grouped in spy events. If you want to group events, use actions instead.
* Normalized `spy` events further. Computed values and actions now report `object` instead of `target` for the scope they have been
applied to.
* The following deprecated methods have been removed:
* `transaction`
...
set = function (key, value) { this.assertValidKey(key); key = "" + key; var hasKey = this._has(key); if (hasInterceptors(this)) { var change = interceptChange(this, { type: hasKey ? "update" : "add", object: this, newValue: value, name: key }); if (!change) return this; value = change.newValue; } if (hasKey) { this._updateValue(key, value); } else { this._addValue(key, value); } return this; }
...
# 3.1.9
* Introduced explicit `.get(index)` and `.set(index, value)` methods on observable arrays
, for issues that have trouble handling many property descriptors on objects. See also #574
* Made sure it is safe to call `onBecomeObserved` twice in row, fixes #874, #898
* Fixed typings of `IReactionDisposer`
# 3.1.8
* Fixed edge case where `autorun` was not triggered again if a computed value was invalidated by the reaction itself, see [#916](
https://github.com/mobxjs/mobx/issues/916), by @andykog
* Added support for primtive keys in `createTransformer`, See #920 by @dnakov
...
toJS = function () { var _this = this; var res = {}; this.keys().forEach(function (key) { return res[key] = _this.get(key); }); return res; }
...
ObservableArray.prototype.replace = function (newItems) {
return this.$mobx.spliceWithArray(0, this.$mobx.values.length, newItems);
};
ObservableArray.prototype.toJS = function () {
return this.slice();
};
ObservableArray.prototype.toJSON = function () {
return this.toJS();
};
ObservableArray.prototype.peek = function () {
return this.$mobx.values;
};
ObservableArray.prototype.find = function (predicate, thisArg, fromIndex) {
if (fromIndex === void 0) { fromIndex = 0; }
this.$mobx.atom.reportObserved();
...
toJSON = function () { return this.toJS(); }
n/a
toString = function () { var _this = this; return this.name + "[{ " + this.keys().map(function (key) { return key + ": " + ("" + _this.get(key)); }).join(", ") + " }]"; }
...
prevValue = newValue;
});
};
ComputedValue.prototype.toJSON = function () {
return this.get();
};
ComputedValue.prototype.toString = function () {
return this.name + "[" + this.derivation.toString() + "]";
};
ComputedValue.prototype.valueOf = function () {
return toPrimitive(this.get());
};
;
ComputedValue.prototype.whyRun = function () {
var isTracking = Boolean(globalState.trackingDerivation);
...
values = function () { return arrayAsIterator(this._keys.map(this.get, this)); }
...
# 0.6.7:
* Improved logging
# 0.6.6:
* Deprecated observable array `.values()` and `.clone()`
* Deprecated observeUntilInvalid; use sideEffect instead
* Renamed mobservable.toJson to mobservable.toJSON
# 0.6.5:
* It is no longer possible to create impure views; views that alter other reactive values.
* Update links to the new documentation.
...
function Reaction(name, onInvalidate) { if (name === void 0) { name = "Reaction@" + getNextId(); } this.name = name; this.onInvalidate = onInvalidate; this.observing = []; this.newObserving = []; this.dependenciesState = IDerivationState.NOT_TRACKING; this.diffValue = 0; this.runId = 0; this.unboundDepsCount = 0; this.__mapid = "#" + getNextId(); this.isDisposed = false; this._isScheduled = false; this._isTrackPending = false; this._isRunning = false; }
n/a
dispose = function () { if (!this.isDisposed) { this.isDisposed = true; if (!this._isRunning) { startBatch(); clearObserving(this); endBatch(); } } }
...
name = ("When@" + getNextId());
predicate = arg1;
effect = arg2;
scope = arg3;
}
var disposer = autorun(name, function (r) {
if (predicate.call(scope)) {
r.dispose();
var prevUntracked = untrackedStart();
effect.call(scope);
untrackedEnd(prevUntracked);
}
});
return disposer;
}
...
getDisposer = function () { var r = this.dispose.bind(this); r.$mobx = this; r.onError = registerErrorHandler; return r; }
...
var reaction = new Reaction(name, function () {
this.track(reactionRunner);
});
function reactionRunner() {
view(reaction);
}
reaction.schedule();
return reaction.getDisposer();
}
exports.autorun = autorun;
function when(arg1, arg2, arg3, arg4) {
var name, predicate, effect, scope;
if (typeof arg1 === "string") {
name = arg1;
predicate = arg2;
...
isScheduled = function () { return this._isScheduled; }
...
return r;
};
Reaction.prototype.toString = function () {
return "Reaction[" + this.name + "]";
};
Reaction.prototype.whyRun = function () {
var observing = unique(this._isRunning ? this.newObserving : this.observing).map(function (dep) { return dep.name; });
return ("\nWhyRun? reaction '" + this.name + "':\n * Status: [" + (this.isDisposed ? "stopped
" : 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) ? " (... or any observable accessed during the remainder of the current run)" : "") + "\n\t" + getMessage("m038") + "\n");
};
return Reaction;
}());
exports.Reaction = Reaction;
function registerErrorHandler(handler) {
invariant(this && this.$mobx && isReaction(this.$mobx), "Invalid `this`");
invariant(!this.$mobx.errorHandler, "Only one onErrorHandler can be registered");
...
onBecomeStale = function () { this.schedule(); }
...
if (dep.diffValue === 1) {
dep.diffValue = 0;
addObserver(dep, derivation);
}
}
if (lowestNewObservingDerivationState !== IDerivationState.UP_TO_DATE) {
derivation.dependenciesState = lowestNewObservingDerivationState;
derivation.onBecomeStale();
}
}
function clearObserving(derivation) {
var obs = derivation.observing;
derivation.observing = [];
var i = obs.length;
while (i--)
...
reportExceptionInDerivation = function (error) { var _this = this; if (this.errorHandler) { this.errorHandler(error, this); return; } var message = "[mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: '" + this; var messageToUser = getMessage("m037"); console.error(message || messageToUser, error); if (isSpyEnabled()) { spyReport({ type: "error", message: message, error: error, object: this }); } globalState.globalReactionErrorHandlers.forEach(function (f) { return f(error, _this); }); }
...
var result = trackDerivedFunction(this, fn, undefined);
this._isRunning = false;
this._isTrackPending = false;
if (this.isDisposed) {
clearObserving(this);
}
if (isCaughtException(result))
this.reportExceptionInDerivation(result.cause);
if (notify) {
spyReportEnd({
time: Date.now() - startTime
});
}
endBatch();
};
...
runReaction = function () { if (!this.isDisposed) { startBatch(); this._isScheduled = false; if (shouldCompute(this)) { this._isTrackPending = true; this.onInvalidate(); if (this._isTrackPending && isSpyEnabled()) { spyReport({ object: this, type: "scheduled-reaction" }); } } endBatch(); } }
...
if (++iterations === MAX_REACTION_ITERATIONS) {
console.error("Reaction doesn't converge to a stable state after " + MAX_REACTION_ITERATIONS + " iterations
."
+ (" Probably there is a cycle in the reactive function: " + allReactions[0]));
allReactions.splice(0);
}
var remainingReactions = allReactions.splice(0);
for (var i = 0, l = remainingReactions.length; i < l; i++)
remainingReactions[i].runReaction();
}
globalState.isRunningReactions = false;
}
var isReaction = createInstanceofPredicate("Reaction", Reaction);
function setReactionScheduler(fn) {
var baseScheduler = reactionScheduler;
reactionScheduler = function (f) { return fn(function () { return baseScheduler(f); }); };
...
schedule = function () { if (!this._isScheduled) { this._isScheduled = true; globalState.pendingReactions.push(this); runReactions(); } }
...
view = view.bind(scope);
var reaction = new Reaction(name, function () {
this.track(reactionRunner);
});
function reactionRunner() {
view(reaction);
}
reaction.schedule();
return reaction.getDisposer();
}
exports.autorun = autorun;
function when(arg1, arg2, arg3, arg4) {
var name, predicate, effect, scope;
if (typeof arg1 === "string") {
name = arg1;
...
toString = function () { return "Reaction[" + this.name + "]"; }
...
prevValue = newValue;
});
};
ComputedValue.prototype.toJSON = function () {
return this.get();
};
ComputedValue.prototype.toString = function () {
return this.name + "[" + this.derivation.toString() + "]";
};
ComputedValue.prototype.valueOf = function () {
return toPrimitive(this.get());
};
;
ComputedValue.prototype.whyRun = function () {
var isTracking = Boolean(globalState.trackingDerivation);
...
track = function (fn) { startBatch(); var notify = isSpyEnabled(); var startTime; if (notify) { startTime = Date.now(); spyReportStart({ object: this, type: "reaction", fn: fn }); } this._isRunning = true; var result = trackDerivedFunction(this, fn, undefined); this._isRunning = false; this._isTrackPending = false; if (this.isDisposed) { clearObserving(this); } if (isCaughtException(result)) this.reportExceptionInDerivation(result.cause); if (notify) { spyReportEnd({ time: Date.now() - startTime }); } endBatch(); }
...
scope = arg2;
}
invariant(typeof view === "function", getMessage("m004"));
invariant(isAction(view) === false, getMessage("m005"));
if (scope)
view = view.bind(scope);
var reaction = new Reaction(name, function () {
this.track(reactionRunner);
});
function reactionRunner() {
view(reaction);
}
reaction.schedule();
return reaction.getDisposer();
}
...
whyRun = function () { var observing = unique(this._isRunning ? this.newObserving : this.observing).map(function (dep) { return dep.name; }); return ("\nWhyRun? reaction '" + this.name + "':\n * Status: [" + (this.isDisposed ? "stopped" : 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) ? " (... or any observable accessed during the remainder of the current run)" : "") + "\n\t" + getMessage("m038") + "\n"); }
...
break;
case 2:
thing = getAtom(thing, prop);
break;
}
thing = getAtom(thing);
if (isComputedValue(thing))
return log(thing.whyRun());
else if (isReaction(thing))
return log(thing.whyRun());
return fail(getMessage("m025"));
}
exports.whyRun = whyRun;
function createAction(actionName, fn) {
invariant(typeof fn === "function", getMessage("m026"));
...
function action(arg1, arg2, arg3, arg4) { if (arguments.length === 1 && typeof arg1 === "function") return createAction(arg1.name || "<unnamed action>", arg1); if (arguments.length === 2 && typeof arg2 === "function") return createAction(arg1, arg2); if (arguments.length === 1 && typeof arg1 === "string") return namedActionDecorator(arg1); return namedActionDecorator(arg2).apply(null, arguments); }
n/a
function boundAction(arg1, arg2, arg3) { if (typeof arg1 === "function") { var action_1 = createAction("<not yet bound action>", arg1); action_1.autoBind = true; return action_1; } return boundActionDecorator.apply(null, arguments); }
n/a
function computed(arg1, arg2, arg3) { if (typeof arg2 === "string") { return computedDecorator.apply(null, arguments); } invariant(typeof arg1 === "function", getMessage("m011")); invariant(arguments.length < 3, getMessage("m012")); var opts = typeof arg2 === "object" ? arg2 : {}; opts.setter = typeof arg2 === "function" ? arg2 : opts.setter; return new ComputedValue(arg1, opts.context, opts.compareStructural || opts.struct || false, opts.name || arg1.name || "", opts .setter); }
n/a
function classPropertyDecorator(target, key, descriptor, customArgs, argLen) { if (argLen === void 0) { argLen = 0; } invariant(allowCustomArguments || quacksLikeADecorator(arguments), "This function is a decorator, but it wasn't invoked like a decorator"); if (!descriptor) { var newDescriptor = { enumerable: enumerable, configurable: true, get: function () { if (!this.__mobxInitializedProps || this.__mobxInitializedProps[key] !== true) typescriptInitializeProperty(this, key, undefined, onInitialize, customArgs, descriptor); return get.call(this, key); }, set: function (v) { if (!this.__mobxInitializedProps || this.__mobxInitializedProps[key] !== true) { typescriptInitializeProperty(this, key, v, onInitialize, customArgs, descriptor); } else { set.call(this, key, v); } } }; if (arguments.length < 3 || arguments.length === 5 && argLen < 3) { Object.defineProperty(target, key, newDescriptor); } return newDescriptor; } else { if (!hasOwnProperty(target, "__mobxLazyInitializers")) { addHiddenProp(target, "__mobxLazyInitializers", (target.__mobxLazyInitializers && target.__mobxLazyInitializers.slice ()) || []); } var value_1 = descriptor.value, initializer_1 = descriptor.initializer; target.__mobxLazyInitializers.push(function (instance) { onInitialize(instance, key, (initializer_1 ? initializer_1.call(instance) : value_1), customArgs, descriptor); }); return { enumerable: enumerable, configurable: true, get: function () { if (this.__mobxDidRunLazyInitializers !== true) runLazyInitializers(this); return get.call(this, key); }, set: function (v) { if (this.__mobxDidRunLazyInitializers !== true) runLazyInitializers(this); set.call(this, key, v); } }; } }
...
function asReference(value) {
deprecated("asReference is deprecated, use observable.ref instead");
return observable.ref(value);
}
exports.asReference = asReference;
function asStructure(value) {
deprecated("asStructure is deprecated. Use observable.struct, computed.struct or reaction options instead.");
return observable.struct(value);
}
exports.asStructure = asStructure;
function asFlat(value) {
deprecated("asFlat is deprecated, use observable.shallow instead");
return observable.shallow(value);
}
exports.asFlat = asFlat;
...
function Atom(name, onBecomeObservedHandler, onBecomeUnobservedHandler) { if (name === void 0) { name = "Atom@" + getNextId(); } if (onBecomeObservedHandler === void 0) { onBecomeObservedHandler = noop; } if (onBecomeUnobservedHandler === void 0) { onBecomeUnobservedHandler = noop; } var _this = _super.call(this, name) || this; _this.name = name; _this.onBecomeObservedHandler = onBecomeObservedHandler; _this.onBecomeUnobservedHandler = onBecomeUnobservedHandler; _this.isPendingUnobservation = false; _this.isBeingTracked = false; return _this; }
n/a
function BaseAtom(name) { if (name === void 0) { name = "Atom@" + getNextId(); } this.name = name; this.isPendingUnobservation = true; this.observers = []; this.observersIndexes = {}; this.diffValue = 0; this.lastAccessedBy = 0; this.lowestObserverState = IDerivationState.NOT_TRACKING; }
n/a
function IObservableFactories() { }
n/a
function ObservableMap(initialData, enhancer, name) { if (enhancer === void 0) { enhancer = deepEnhancer; } if (name === void 0) { name = "ObservableMap@" + getNextId(); } this.enhancer = enhancer; this.name = name; this.$mobx = ObservableMapMarker; this._data = Object.create(null); this._hasMap = Object.create(null); this._keys = new ObservableArray(undefined, referenceEnhancer, this.name + ".keys()", true); this.interceptors = null; this.changeListeners = null; this.merge(initialData); }
n/a
function Reaction(name, onInvalidate) { if (name === void 0) { name = "Reaction@" + getNextId(); } this.name = name; this.onInvalidate = onInvalidate; this.observing = []; this.newObserving = []; this.dependenciesState = IDerivationState.NOT_TRACKING; this.diffValue = 0; this.runId = 0; this.unboundDepsCount = 0; this.__mapid = "#" + getNextId(); this.isDisposed = false; this._isScheduled = false; this._isTrackPending = false; this._isRunning = false; }
n/a
function action(arg1, arg2, arg3, arg4) { if (arguments.length === 1 && typeof arg1 === "function") return createAction(arg1.name || "<unnamed action>", arg1); if (arguments.length === 2 && typeof arg2 === "function") return createAction(arg1, arg2); if (arguments.length === 1 && typeof arg1 === "string") return namedActionDecorator(arg1); return namedActionDecorator(arg2).apply(null, arguments); }
n/a
function asFlat(value) { deprecated("asFlat is deprecated, use observable.shallow instead"); return observable.shallow(value); }
n/a
function asMap(data) { deprecated("asMap is deprecated, use observable.map or observable.shallowMap instead"); return observable.map(data || {}); }
n/a
function asReference(value) { deprecated("asReference is deprecated, use observable.ref instead"); return observable.ref(value); }
n/a
function asStructure(value) { deprecated("asStructure is deprecated. Use observable.struct, computed.struct or reaction options instead."); return observable.struct(value); }
n/a
function autorun(arg1, arg2, arg3) { var name, view, scope; if (typeof arg1 === "string") { name = arg1; view = arg2; scope = arg3; } else { name = arg1.name || ("Autorun@" + getNextId()); view = arg1; scope = arg2; } invariant(typeof view === "function", getMessage("m004")); invariant(isAction(view) === false, getMessage("m005")); if (scope) view = view.bind(scope); var reaction = new Reaction(name, function () { this.track(reactionRunner); }); function reactionRunner() { view(reaction); } reaction.schedule(); return reaction.getDisposer(); }
n/a
function autorunAsync(arg1, arg2, arg3, arg4) { var name, func, delay, scope; if (typeof arg1 === "string") { name = arg1; func = arg2; delay = arg3; scope = arg4; } else { name = arg1.name || ("AutorunAsync@" + getNextId()); func = arg1; delay = arg2; scope = arg3; } invariant(isAction(func) === false, getMessage("m006")); if (delay === void 0) delay = 1; if (scope) func = func.bind(scope); var isScheduled = false; var r = new Reaction(name, function () { if (!isScheduled) { isScheduled = true; setTimeout(function () { isScheduled = false; if (!r.isDisposed) r.track(reactionRunner); }, delay); } }); function reactionRunner() { func(r); } r.schedule(); return r.getDisposer(); }
...
* `toJSON` now serializes object trees with cycles as well. If you know the object tree is acyclic, pass in `false` as second parameter
for a performance gain.
# 1.1.0
* Exposed `ObservableMap` type
* Introduced `mobservable.untracked(block)`
* Introduced `mobservable.autorunAsync(block, delay)`
# 1.0.9
Removed accidental log message
# 1.0.7 / 1.0.8
...
function computed(arg1, arg2, arg3) { if (typeof arg2 === "string") { return computedDecorator.apply(null, arguments); } invariant(typeof arg1 === "function", getMessage("m011")); invariant(arguments.length < 3, getMessage("m012")); var opts = typeof arg2 === "object" ? arg2 : {}; opts.setter = typeof arg2 === "function" ? arg2 : opts.setter; return new ComputedValue(arg1, opts.context, opts.compareStructural || opts.struct || false, opts.name || arg1.name || "", opts .setter); }
n/a
function createTransformer(transformer, onCleanup) { invariant(typeof transformer === "function" && transformer.length < 2, "createTransformer expects a function that accepts one argument"); var objectCache = {}; var resetId = globalState.resetId; var Transformer = (function (_super) { __extends(Transformer, _super); function Transformer(sourceIdentifier, sourceObject) { var _this = _super.call(this, function () { return transformer(sourceObject); }, undefined, false, "Transformer-" + transformer.name + "-" + sourceIdentifier, undefined) || this; _this.sourceIdentifier = sourceIdentifier; _this.sourceObject = sourceObject; return _this; } Transformer.prototype.onBecomeUnobserved = function () { var lastValue = this.value; _super.prototype.onBecomeUnobserved.call(this); delete objectCache[this.sourceIdentifier]; if (onCleanup) onCleanup(lastValue, this.sourceObject); }; return Transformer; }(ComputedValue)); return function (object) { if (resetId !== globalState.resetId) { objectCache = {}; resetId = globalState.resetId; } var identifier = getMemoizationId(object); var reactiveTransformer = objectCache[identifier]; if (reactiveTransformer) return reactiveTransformer.get(); reactiveTransformer = objectCache[identifier] = new Transformer(identifier, object); return reactiveTransformer.get(); }; }
n/a
function expr(expr, scope) { if (!isComputingDerivation()) console.warn(getMessage("m013")); return computed(expr, { context: scope }).get(); }
n/a
function extendObservable(target) { var properties = []; for (var _i = 1; _i < arguments.length; _i++) { properties[_i - 1] = arguments[_i]; } return extendObservableHelper(target, deepEnhancer, properties); }
n/a
function extendShallowObservable(target) { var properties = []; for (var _i = 1; _i < arguments.length; _i++) { properties[_i - 1] = arguments[_i]; } return extendObservableHelper(target, referenceEnhancer, properties); }
n/a
function intercept(thing, propOrHandler, handler) { if (typeof handler === "function") return interceptProperty(thing, propOrHandler, handler); else return interceptInterceptable(thing, propOrHandler); }
...
if (typeof handler === "function")
return interceptProperty(thing, propOrHandler, handler);
else
return interceptInterceptable(thing, propOrHandler);
}
exports.intercept = intercept;
function interceptInterceptable(thing, handler) {
return getAdministration(thing).intercept(handler);
}
function interceptProperty(thing, property, handler) {
return getAdministration(thing, property).intercept(handler);
}
function isComputed(value, property) {
if (value === null || value === undefined)
return false;
...
function isAction(thing) { return typeof thing === "function" && thing.isMobxAction === true; }
n/a
function isArrayLike(x) { return Array.isArray(x) || isObservableArray(x); }
n/a
isBoxedObservable = function (x) { return isObject(x) && x[propName] === true; }
n/a
function isComputed(value, property) { if (value === null || value === undefined) return false; if (property !== undefined) { if (isObservableObject(value) === false) return false; var atom = getAtom(value, property); return isComputedValue(atom); } return isComputedValue(value); }
n/a
function isModifierDescriptor(thing) { return typeof thing === "object" && thing !== null && thing.isMobxModifierDescriptor === true; }
n/a
function isObservable(value, property) { if (value === null || value === undefined) return false; if (property !== undefined) { if (isObservableArray(value) || isObservableMap(value)) throw new Error(getMessage("m019")); else if (isObservableObject(value)) { var o = value.$mobx; return o.values && !!o.values[property]; } return false; } return isObservableObject(value) || !!value.$mobx || isAtom(value) || isReaction(value) || isComputedValue(value); }
n/a
function isObservableArray(thing) { return isObject(thing) && isObservableArrayAdministration(thing.$mobx); }
n/a
isObservableMap = function (x) { return isObject(x) && x[propName] === true; }
n/a
function isObservableObject(thing) { if (isObject(thing)) { runLazyInitializers(thing); return isObservableObjectAdministration(thing.$mobx); } return false; }
n/a
function isStrictModeEnabled() { return globalState.strictMode; }
n/a
function map(initialValues) { deprecated("`mobx.map` is deprecated, use `new ObservableMap` or `mobx.observable.map` instead"); return observable.map(initialValues); }
...
### Factories per observable type
There are now explicit methods to create an observable of a specific type.
* `observable.object(props, name?)` creates a new observable object, by cloning the give props and making them observable
* `observable.array(initialValues, name?)`. Take a guess..
* `observable.map(initialValues, name?)`
* `observable.box(initialValue, name?)`. Creates a [boxed](http://mobxjs.github.io/mobx/refguide/boxed.html) value, which can be
read from / written to using `.get()` and `.set(newValue)`
* `observable(value)`, as-is, based on the type of `value`, uses any of the above four functions to create a new observable.
### Shallow factories per type
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
...
function createObservable(v) { if (v === void 0) { v = undefined; } if (typeof arguments[1] === "string") return deepDecorator.apply(null, arguments); invariant(arguments.length <= 1, getMessage("m021")); invariant(!isModifierDescriptor(v), getMessage("m020")); if (isObservable(v)) return v; var res = deepEnhancer(v, undefined, undefined); if (res !== v) return res; return observable.box(v); }
...
# 1.1.7
* Fixed #77: package consumers with --noImplicitAny should be able to build
# 1.1.6
* Introduced `mobservable.fastArray(array)`, in addition to `mobservable.observable(array
)`. Which is much faster when adding items but doesn't support enumerability (`for (var idx in ar) ..` loops).
* Introduced `observableArray.peek()`, for fast access to the array values. Should be used read-only.
# 1.1.5
* Fixed 71: transactions should not influence running computations
# 1.1.4
...
function observe(thing, propOrCb, cbOrFire, fireImmediately) { if (typeof cbOrFire === "function") return observeObservableProperty(thing, propOrCb, cbOrFire, fireImmediately); else return observeObservable(thing, propOrCb, cbOrFire); }
...
if (typeof cbOrFire === "function")
return observeObservableProperty(thing, propOrCb, cbOrFire, fireImmediately);
else
return observeObservable(thing, propOrCb, cbOrFire);
}
exports.observe = observe;
function observeObservable(thing, listener, fireImmediately) {
return getAdministration(thing).observe(listener, fireImmediately);
}
function observeObservableProperty(thing, property, listener, fireImmediately) {
return getAdministration(thing, property).observe(listener, fireImmediately);
}
function toJS(source, detectCycles, __alreadySeen) {
if (detectCycles === void 0) { detectCycles = true; }
if (__alreadySeen === void 0) { __alreadySeen = []; }
...
function reaction(expression, effect, arg3) { if (arguments.length > 3) { fail(getMessage("m007")); } if (isModifierDescriptor(expression)) { fail(getMessage("m008")); } var opts; if (typeof arg3 === "object") { opts = arg3; } else { opts = {}; } opts.name = opts.name || expression.name || effect.name || ("Reaction@" + getNextId()); opts.fireImmediately = arg3 === true || opts.fireImmediately === true; opts.delay = opts.delay || 0; opts.compareStructural = opts.compareStructural || opts.struct || false; effect = action(opts.name, opts.context ? effect.bind(opts.context) : effect); if (opts.context) { expression = expression.bind(opts.context); } var firstTime = true; var isScheduled = false; var nextValue; var r = new Reaction(opts.name, function () { if (firstTime || opts.delay < 1) { reactionRunner(); } else if (!isScheduled) { isScheduled = true; setTimeout(function () { isScheduled = false; reactionRunner(); }, opts.delay); } }); function reactionRunner() { if (r.isDisposed) return; var changed = false; r.track(function () { var v = expression(r); changed = valueDidChange(opts.compareStructural, nextValue, v); nextValue = v; }); if (firstTime && opts.fireImmediately) effect(nextValue, r); if (!firstTime && changed === true) effect(nextValue, r); if (firstTime) firstTime = false; } r.schedule(); return r.getDisposer(); }
n/a
function runInAction(arg1, arg2, arg3) { var actionName = typeof arg1 === "string" ? arg1 : arg1.name || "<unnamed action>"; var fn = typeof arg1 === "function" ? arg1 : arg2; var scope = typeof arg1 === "function" ? arg2 : arg3; invariant(typeof fn === "function", getMessage("m002")); invariant(fn.length === 0, getMessage("m003")); invariant(typeof actionName === "string" && actionName.length > 0, "actions should have valid names, got: '" + actionName + "'"); return executeAction(actionName, fn, scope, undefined); }
n/a
function spy(listener) { globalState.spyListeners.push(listener); return once(function () { var idx = globalState.spyListeners.indexOf(listener); if (idx !== -1) globalState.spyListeners.splice(idx, 1); }); }
n/a
function toJS(source, detectCycles, __alreadySeen) { if (detectCycles === void 0) { detectCycles = true; } if (__alreadySeen === void 0) { __alreadySeen = []; } function cache(value) { if (detectCycles) __alreadySeen.push([source, value]); return value; } if (isObservable(source)) { if (detectCycles && __alreadySeen === null) __alreadySeen = []; if (detectCycles && source !== null && typeof source === "object") { for (var i = 0, l = __alreadySeen.length; i < l; i++) if (__alreadySeen[i][0] === source) return __alreadySeen[i][1]; } if (isObservableArray(source)) { var res = cache([]); var toAdd = source.map(function (value) { return toJS(value, detectCycles, __alreadySeen); }); res.length = toAdd.length; for (var i = 0, l = toAdd.length; i < l; i++) res[i] = toAdd[i]; return res; } if (isObservableObject(source)) { var res = cache({}); for (var key in source) res[key] = toJS(source[key], detectCycles, __alreadySeen); return res; } if (isObservableMap(source)) { var res_1 = cache({}); source.forEach(function (value, key) { return res_1[key] = toJS(value, detectCycles, __alreadySeen); }); return res_1; } if (isObservableValue(source)) return toJS(source.get(), detectCycles, __alreadySeen); } return source; }
...
ObservableArray.prototype.replace = function (newItems) {
return this.$mobx.spliceWithArray(0, this.$mobx.values.length, newItems);
};
ObservableArray.prototype.toJS = function () {
return this.slice();
};
ObservableArray.prototype.toJSON = function () {
return this.toJS();
};
ObservableArray.prototype.peek = function () {
return this.$mobx.values;
};
ObservableArray.prototype.find = function (predicate, thisArg, fromIndex) {
if (fromIndex === void 0) { fromIndex = 0; }
this.$mobx.atom.reportObserved();
...
function transaction(action, thisArg) { if (thisArg === void 0) { thisArg = undefined; } deprecated(getMessage("m023")); return runInTransaction.apply(undefined, arguments); }
n/a
function untracked(action) { var prev = untrackedStart(); var res = action(); untrackedEnd(prev); return res; }
...
# 1.1.1
* `toJSON` now serializes object trees with cycles as well. If you know the object tree is acyclic, pass in `false` as second parameter
for a performance gain.
# 1.1.0
* Exposed `ObservableMap` type
* Introduced `mobservable.untracked(block)`
* Introduced `mobservable.autorunAsync(block, delay)`
# 1.0.9
Removed accidental log message
# 1.0.7 / 1.0.8
...
function useStrict(strict) { invariant(globalState.trackingDerivation === null, getMessage("m028")); globalState.strictMode = strict; globalState.allowStateChanges = !strict; }
n/a
function when(arg1, arg2, arg3, arg4) { var name, predicate, effect, scope; if (typeof arg1 === "string") { name = arg1; predicate = arg2; effect = arg3; scope = arg4; } else { name = ("When@" + getNextId()); predicate = arg1; effect = arg2; scope = arg3; } var disposer = autorun(name, function (r) { if (predicate.call(scope)) { r.dispose(); var prevUntracked = untrackedStart(); effect.call(scope); untrackedEnd(prevUntracked); } }); return disposer; }
n/a
function whyRun(thing, prop) { switch (arguments.length) { case 0: thing = globalState.trackingDerivation; if (!thing) return log(getMessage("m024")); break; case 2: thing = getAtom(thing, prop); break; } thing = getAtom(thing); if (isComputedValue(thing)) return log(thing.whyRun()); else if (isReaction(thing)) return log(thing.whyRun()); return fail(getMessage("m025")); }
...
break;
case 2:
thing = getAtom(thing, prop);
break;
}
thing = getAtom(thing);
if (isComputedValue(thing))
return log(thing.whyRun());
else if (isReaction(thing))
return log(thing.whyRun());
return fail(getMessage("m025"));
}
exports.whyRun = whyRun;
function createAction(actionName, fn) {
invariant(typeof fn === "function", getMessage("m026"));
...
function allowStateChanges(allowStateChanges, func) { var prev = allowStateChangesStart(allowStateChanges); var res; try { res = func(); } finally { allowStateChangesEnd(prev); } return res; }
n/a
function deepEqual(a, b) { if (a === null && b === null) return true; if (a === undefined && b === undefined) return true; if (typeof a !== "object") return a === b; var aIsArray = isArrayLike(a); var aIsMap = isMapLike(a); if (aIsArray !== isArrayLike(b)) { return false; } else if (aIsMap !== isMapLike(b)) { return false; } else if (aIsArray) { if (a.length !== b.length) return false; for (var i = a.length - 1; i >= 0; i--) if (!deepEqual(a[i], b[i])) return false; return true; } else if (aIsMap) { if (a.size !== b.size) return false; var equals_1 = true; a.forEach(function (value, key) { equals_1 = equals_1 && deepEqual(b.get(key), value); }); return equals_1; } else if (typeof a === "object" && typeof b === "object") { if (a === null || b === null) return false; if (isMapLike(a) && isMapLike(b)) { if (a.size !== b.size) return false; return deepEqual(observable.shallowMap(a).entries(), observable.shallowMap(b).entries()); } if (getEnumerableKeys(a).length !== getEnumerableKeys(b).length) return false; for (var prop in a) { if (!(prop in b)) return false; if (!deepEqual(a[prop], b[prop])) return false; } return true; } return false; }
n/a
function getAdministration(thing, property) { invariant(thing, "Expecting some object"); if (property !== undefined) return getAdministration(getAtom(thing, property)); if (isAtom(thing) || isComputedValue(thing) || isReaction(thing)) return thing; if (isObservableMap(thing)) return thing; runLazyInitializers(thing); if (thing.$mobx) return thing.$mobx; invariant(false, "Cannot obtain administration from " + thing); }
n/a
function getAtom(thing, property) { if (typeof thing === "object" && thing !== null) { if (isObservableArray(thing)) { invariant(property === undefined, getMessage("m036")); return thing.$mobx.atom; } if (isObservableMap(thing)) { var anyThing = thing; if (property === undefined) return getAtom(anyThing._keys); var observable_2 = anyThing._data[property] || anyThing._hasMap[property]; invariant(!!observable_2, "the entry '" + property + "' does not exist in the observable map '" + getDebugName(thing ) + "'"); return observable_2; } runLazyInitializers(thing); if (isObservableObject(thing)) { if (!property) return fail("please specify a property"); var observable_3 = thing.$mobx.values[property]; invariant(!!observable_3, "no observable property '" + property + "' found on the observable object '" + getDebugName (thing) + "'"); return observable_3; } if (isAtom(thing) || isComputedValue(thing) || isReaction(thing)) { return thing; } } else if (typeof thing === "function") { if (isReaction(thing.$mobx)) { return thing.$mobx; } } return fail("Cannot obtain atom from " + thing); }
n/a
function getDebugName(thing, property) { var named; if (property !== undefined) named = getAtom(thing, property); else if (isObservableObject(thing) || isObservableMap(thing)) named = getAdministration(thing); else named = getAtom(thing); return named.name; }
n/a
function getDependencyTree(thing, property) { return nodeToDependencyTree(getAtom(thing, property)); }
n/a
function getGlobalState() { return globalState; }
n/a
function getObserverTree(thing, property) { return nodeToObserverTree(getAtom(thing, property)); }
n/a
function isComputingDerivation() { return globalState.trackingDerivation !== null; }
n/a
function isSpyEnabled() { return !!globalState.spyListeners.length; }
n/a
function onReactionError(handler) { globalState.globalReactionErrorHandlers.push(handler); return function () { var idx = globalState.globalReactionErrorHandlers.indexOf(handler); if (idx >= 0) globalState.globalReactionErrorHandlers.splice(idx, 1); }; }
...
Error handling in MobX has been made more consistent. In MobX 2 there was a best-effort recovery attempt if a derivation throws,
but MobX 3 introduced
more consistent behavior:
* Computed values that throw, store the exception and throw it to the next consumer(s). They keep tracking their data, so they are
able to recover from exceptions in next re-runs.
* Reactions (like `autorun`, `when`, `reaction`, `render()` of `observer` components) will always catch their exceptions, and just
log the error. They will keep tracking their data, so they are able to recover in next re-runs.
* The disposer of a reaction exposes an `onError(handler)` method, which makes it possible to attach custom error handling logic
to an reaction (that overrides the default logging behavior).
* `extras.onReactionError(handler)` can be used to register a global onError handler
for reactions (will fire after spy "error" event). This can be useful in tests etc.
See [#731](https://github.com/mobxjs/mobx/issues/731)
### Removed error handling, improved error recovery
MobX always printed a warning when an exception was thrown from a computed value, reaction or react component: `[mobx] An uncaught
exception occurred while calculating....`.
This warning was often confusing for people because they either had the impression that this was a mobx exception, while it actually
is just informing about an exception that happened in userland code.
...
function reserveArrayBuffer(max) { for (var index = OBSERVABLE_ARRAY_BUFFER_SIZE; index < max; index++) createArrayBufferItem(index); OBSERVABLE_ARRAY_BUFFER_SIZE = max; }
n/a
function resetGlobalState() { globalState.resetId++; var defaultGlobals = new MobXGlobals(); for (var key in defaultGlobals) if (persistentKeys.indexOf(key) === -1) globalState[key] = defaultGlobals[key]; globalState.allowStateChanges = !globalState.strictMode; }
n/a
function setReactionScheduler(fn) { var baseScheduler = reactionScheduler; reactionScheduler = function (f) { return fn(function () { return baseScheduler(f); }); }; }
n/a
function shareGlobalState() { var global = getGlobal(); var ownState = globalState; if (global.__mobservableTrackingStack || global.__mobservableViewStack) throw new Error("[mobx] An incompatible version of mobservable is already loaded."); if (global.__mobxGlobal && global.__mobxGlobal.version !== ownState.version) throw new Error("[mobx] An incompatible version of mobx is already loaded."); if (global.__mobxGlobal) globalState = global.__mobxGlobal; else global.__mobxGlobal = ownState; }
...
even when both packages where shipped with their own (embedded) version of MobX (!).
Obviously this is a nasty default as it breaks package isolation and might actually start to throw errors unintentionally when MobX
is loaded multiple times in the same runtime by completely unrelated packages.
So this sharing behavior is now by default turned off.
Sharing MobX should be achieved by means of proper bundling, de-duplication of packages or using peer dependencies / externals if
needed.
This is similar to packages like React, which will also bail out if you try to load it multiple times.
If you still want to use the old behavior, this can be achieved by running `mobx.extras.shareGlobalState
()` on _all_ packages that want to share state with each other.
Since this behavior is probably not used outside Mendix, it has been deprecated immediately, so if you rely on this feature, please
report in #621, so that it can be undeprecated if there is no more elegant solution.
See [#621](https://github.com/mobxjs/mobx/issues/621)
### Other changes
* **Breaking change:** The arguments to `observe` listeners for computed and boxed observables have changed and are now consistent
with the other apis. Instead of invoking the callback with `(newValue: T, oldValue: T)` they are now invoked with a single change
object: `(change: {newValue: T, oldValue: T, object, type: "update"})`
...
function spyReport(event) { if (!globalState.spyListeners.length) return; var listeners = globalState.spyListeners; for (var i = 0, l = listeners.length; i < l; i++) listeners[i](event); }
n/a
function spyReportEnd(change) { if (change) spyReport(objectAssign({}, change, END_EVENT)); else spyReport(END_EVENT); }
n/a
function spyReportStart(event) { var change = objectAssign({}, event, { spyReportStart: true }); spyReport(change); }
n/a
function Atom(name, onBecomeObservedHandler, onBecomeUnobservedHandler) { if (name === void 0) { name = "Atom@" + getNextId(); } if (onBecomeObservedHandler === void 0) { onBecomeObservedHandler = noop; } if (onBecomeUnobservedHandler === void 0) { onBecomeUnobservedHandler = noop; } var _this = _super.call(this, name) || this; _this.name = name; _this.onBecomeObservedHandler = onBecomeObservedHandler; _this.onBecomeUnobservedHandler = onBecomeUnobservedHandler; _this.isPendingUnobservation = false; _this.isBeingTracked = false; return _this; }
n/a
function BaseAtom(name) { if (name === void 0) { name = "Atom@" + getNextId(); } this.name = name; this.isPendingUnobservation = true; this.observers = []; this.observersIndexes = {}; this.diffValue = 0; this.lastAccessedBy = 0; this.lowestObserverState = IDerivationState.NOT_TRACKING; }
n/a
function IObservableFactories() { }
n/a
function ObservableMap(initialData, enhancer, name) { if (enhancer === void 0) { enhancer = deepEnhancer; } if (name === void 0) { name = "ObservableMap@" + getNextId(); } this.enhancer = enhancer; this.name = name; this.$mobx = ObservableMapMarker; this._data = Object.create(null); this._hasMap = Object.create(null); this._keys = new ObservableArray(undefined, referenceEnhancer, this.name + ".keys()", true); this.interceptors = null; this.changeListeners = null; this.merge(initialData); }
n/a
function Reaction(name, onInvalidate) { if (name === void 0) { name = "Reaction@" + getNextId(); } this.name = name; this.onInvalidate = onInvalidate; this.observing = []; this.newObserving = []; this.dependenciesState = IDerivationState.NOT_TRACKING; this.diffValue = 0; this.runId = 0; this.unboundDepsCount = 0; this.__mapid = "#" + getNextId(); this.isDisposed = false; this._isScheduled = false; this._isTrackPending = false; this._isRunning = false; }
n/a
function action(arg1, arg2, arg3, arg4) { if (arguments.length === 1 && typeof arg1 === "function") return createAction(arg1.name || "<unnamed action>", arg1); if (arguments.length === 2 && typeof arg2 === "function") return createAction(arg1, arg2); if (arguments.length === 1 && typeof arg1 === "string") return namedActionDecorator(arg1); return namedActionDecorator(arg2).apply(null, arguments); }
n/a
function asFlat(value) { deprecated("asFlat is deprecated, use observable.shallow instead"); return observable.shallow(value); }
n/a
function asMap(data) { deprecated("asMap is deprecated, use observable.map or observable.shallowMap instead"); return observable.map(data || {}); }
n/a
function asReference(value) { deprecated("asReference is deprecated, use observable.ref instead"); return observable.ref(value); }
n/a
function asStructure(value) { deprecated("asStructure is deprecated. Use observable.struct, computed.struct or reaction options instead."); return observable.struct(value); }
n/a
function autorun(arg1, arg2, arg3) { var name, view, scope; if (typeof arg1 === "string") { name = arg1; view = arg2; scope = arg3; } else { name = arg1.name || ("Autorun@" + getNextId()); view = arg1; scope = arg2; } invariant(typeof view === "function", getMessage("m004")); invariant(isAction(view) === false, getMessage("m005")); if (scope) view = view.bind(scope); var reaction = new Reaction(name, function () { this.track(reactionRunner); }); function reactionRunner() { view(reaction); } reaction.schedule(); return reaction.getDisposer(); }
n/a
function autorunAsync(arg1, arg2, arg3, arg4) { var name, func, delay, scope; if (typeof arg1 === "string") { name = arg1; func = arg2; delay = arg3; scope = arg4; } else { name = arg1.name || ("AutorunAsync@" + getNextId()); func = arg1; delay = arg2; scope = arg3; } invariant(isAction(func) === false, getMessage("m006")); if (delay === void 0) delay = 1; if (scope) func = func.bind(scope); var isScheduled = false; var r = new Reaction(name, function () { if (!isScheduled) { isScheduled = true; setTimeout(function () { isScheduled = false; if (!r.isDisposed) r.track(reactionRunner); }, delay); } }); function reactionRunner() { func(r); } r.schedule(); return r.getDisposer(); }
...
* `toJSON` now serializes object trees with cycles as well. If you know the object tree is acyclic, pass in `false` as second parameter
for a performance gain.
# 1.1.0
* Exposed `ObservableMap` type
* Introduced `mobservable.untracked(block)`
* Introduced `mobservable.autorunAsync(block, delay)`
# 1.0.9
Removed accidental log message
# 1.0.7 / 1.0.8
...
function computed(arg1, arg2, arg3) { if (typeof arg2 === "string") { return computedDecorator.apply(null, arguments); } invariant(typeof arg1 === "function", getMessage("m011")); invariant(arguments.length < 3, getMessage("m012")); var opts = typeof arg2 === "object" ? arg2 : {}; opts.setter = typeof arg2 === "function" ? arg2 : opts.setter; return new ComputedValue(arg1, opts.context, opts.compareStructural || opts.struct || false, opts.name || arg1.name || "", opts .setter); }
n/a
function createTransformer(transformer, onCleanup) { invariant(typeof transformer === "function" && transformer.length < 2, "createTransformer expects a function that accepts one argument"); var objectCache = {}; var resetId = globalState.resetId; var Transformer = (function (_super) { __extends(Transformer, _super); function Transformer(sourceIdentifier, sourceObject) { var _this = _super.call(this, function () { return transformer(sourceObject); }, undefined, false, "Transformer-" + transformer.name + "-" + sourceIdentifier, undefined) || this; _this.sourceIdentifier = sourceIdentifier; _this.sourceObject = sourceObject; return _this; } Transformer.prototype.onBecomeUnobserved = function () { var lastValue = this.value; _super.prototype.onBecomeUnobserved.call(this); delete objectCache[this.sourceIdentifier]; if (onCleanup) onCleanup(lastValue, this.sourceObject); }; return Transformer; }(ComputedValue)); return function (object) { if (resetId !== globalState.resetId) { objectCache = {}; resetId = globalState.resetId; } var identifier = getMemoizationId(object); var reactiveTransformer = objectCache[identifier]; if (reactiveTransformer) return reactiveTransformer.get(); reactiveTransformer = objectCache[identifier] = new Transformer(identifier, object); return reactiveTransformer.get(); }; }
n/a
function expr(expr, scope) { if (!isComputingDerivation()) console.warn(getMessage("m013")); return computed(expr, { context: scope }).get(); }
n/a
function extendObservable(target) { var properties = []; for (var _i = 1; _i < arguments.length; _i++) { properties[_i - 1] = arguments[_i]; } return extendObservableHelper(target, deepEnhancer, properties); }
n/a
function extendShallowObservable(target) { var properties = []; for (var _i = 1; _i < arguments.length; _i++) { properties[_i - 1] = arguments[_i]; } return extendObservableHelper(target, referenceEnhancer, properties); }
n/a
function intercept(thing, propOrHandler, handler) { if (typeof handler === "function") return interceptProperty(thing, propOrHandler, handler); else return interceptInterceptable(thing, propOrHandler); }
...
if (typeof handler === "function")
return interceptProperty(thing, propOrHandler, handler);
else
return interceptInterceptable(thing, propOrHandler);
}
exports.intercept = intercept;
function interceptInterceptable(thing, handler) {
return getAdministration(thing).intercept(handler);
}
function interceptProperty(thing, property, handler) {
return getAdministration(thing, property).intercept(handler);
}
function isComputed(value, property) {
if (value === null || value === undefined)
return false;
...
function isAction(thing) { return typeof thing === "function" && thing.isMobxAction === true; }
n/a
function isArrayLike(x) { return Array.isArray(x) || isObservableArray(x); }
n/a
isBoxedObservable = function (x) { return isObject(x) && x[propName] === true; }
n/a
function isComputed(value, property) { if (value === null || value === undefined) return false; if (property !== undefined) { if (isObservableObject(value) === false) return false; var atom = getAtom(value, property); return isComputedValue(atom); } return isComputedValue(value); }
n/a
function isModifierDescriptor(thing) { return typeof thing === "object" && thing !== null && thing.isMobxModifierDescriptor === true; }
n/a
function isObservable(value, property) { if (value === null || value === undefined) return false; if (property !== undefined) { if (isObservableArray(value) || isObservableMap(value)) throw new Error(getMessage("m019")); else if (isObservableObject(value)) { var o = value.$mobx; return o.values && !!o.values[property]; } return false; } return isObservableObject(value) || !!value.$mobx || isAtom(value) || isReaction(value) || isComputedValue(value); }
n/a
function isObservableArray(thing) { return isObject(thing) && isObservableArrayAdministration(thing.$mobx); }
n/a
isObservableMap = function (x) { return isObject(x) && x[propName] === true; }
n/a
function isObservableObject(thing) { if (isObject(thing)) { runLazyInitializers(thing); return isObservableObjectAdministration(thing.$mobx); } return false; }
n/a
function isStrictModeEnabled() { return globalState.strictMode; }
n/a
function map(initialValues) { deprecated("`mobx.map` is deprecated, use `new ObservableMap` or `mobx.observable.map` instead"); return observable.map(initialValues); }
...
### Factories per observable type
There are now explicit methods to create an observable of a specific type.
* `observable.object(props, name?)` creates a new observable object, by cloning the give props and making them observable
* `observable.array(initialValues, name?)`. Take a guess..
* `observable.map(initialValues, name?)`
* `observable.box(initialValue, name?)`. Creates a [boxed](http://mobxjs.github.io/mobx/refguide/boxed.html) value, which can be
read from / written to using `.get()` and `.set(newValue)`
* `observable(value)`, as-is, based on the type of `value`, uses any of the above four functions to create a new observable.
### Shallow factories per type
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
...
function createObservable(v) { if (v === void 0) { v = undefined; } if (typeof arguments[1] === "string") return deepDecorator.apply(null, arguments); invariant(arguments.length <= 1, getMessage("m021")); invariant(!isModifierDescriptor(v), getMessage("m020")); if (isObservable(v)) return v; var res = deepEnhancer(v, undefined, undefined); if (res !== v) return res; return observable.box(v); }
...
# 1.1.7
* Fixed #77: package consumers with --noImplicitAny should be able to build
# 1.1.6
* Introduced `mobservable.fastArray(array)`, in addition to `mobservable.observable(array
)`. Which is much faster when adding items but doesn't support enumerability (`for (var idx in ar) ..` loops).
* Introduced `observableArray.peek()`, for fast access to the array values. Should be used read-only.
# 1.1.5
* Fixed 71: transactions should not influence running computations
# 1.1.4
...
function observe(thing, propOrCb, cbOrFire, fireImmediately) { if (typeof cbOrFire === "function") return observeObservableProperty(thing, propOrCb, cbOrFire, fireImmediately); else return observeObservable(thing, propOrCb, cbOrFire); }
...
if (typeof cbOrFire === "function")
return observeObservableProperty(thing, propOrCb, cbOrFire, fireImmediately);
else
return observeObservable(thing, propOrCb, cbOrFire);
}
exports.observe = observe;
function observeObservable(thing, listener, fireImmediately) {
return getAdministration(thing).observe(listener, fireImmediately);
}
function observeObservableProperty(thing, property, listener, fireImmediately) {
return getAdministration(thing, property).observe(listener, fireImmediately);
}
function toJS(source, detectCycles, __alreadySeen) {
if (detectCycles === void 0) { detectCycles = true; }
if (__alreadySeen === void 0) { __alreadySeen = []; }
...
function reaction(expression, effect, arg3) { if (arguments.length > 3) { fail(getMessage("m007")); } if (isModifierDescriptor(expression)) { fail(getMessage("m008")); } var opts; if (typeof arg3 === "object") { opts = arg3; } else { opts = {}; } opts.name = opts.name || expression.name || effect.name || ("Reaction@" + getNextId()); opts.fireImmediately = arg3 === true || opts.fireImmediately === true; opts.delay = opts.delay || 0; opts.compareStructural = opts.compareStructural || opts.struct || false; effect = action(opts.name, opts.context ? effect.bind(opts.context) : effect); if (opts.context) { expression = expression.bind(opts.context); } var firstTime = true; var isScheduled = false; var nextValue; var r = new Reaction(opts.name, function () { if (firstTime || opts.delay < 1) { reactionRunner(); } else if (!isScheduled) { isScheduled = true; setTimeout(function () { isScheduled = false; reactionRunner(); }, opts.delay); } }); function reactionRunner() { if (r.isDisposed) return; var changed = false; r.track(function () { var v = expression(r); changed = valueDidChange(opts.compareStructural, nextValue, v); nextValue = v; }); if (firstTime && opts.fireImmediately) effect(nextValue, r); if (!firstTime && changed === true) effect(nextValue, r); if (firstTime) firstTime = false; } r.schedule(); return r.getDisposer(); }
n/a
function runInAction(arg1, arg2, arg3) { var actionName = typeof arg1 === "string" ? arg1 : arg1.name || "<unnamed action>"; var fn = typeof arg1 === "function" ? arg1 : arg2; var scope = typeof arg1 === "function" ? arg2 : arg3; invariant(typeof fn === "function", getMessage("m002")); invariant(fn.length === 0, getMessage("m003")); invariant(typeof actionName === "string" && actionName.length > 0, "actions should have valid names, got: '" + actionName + "'"); return executeAction(actionName, fn, scope, undefined); }
n/a
function spy(listener) { globalState.spyListeners.push(listener); return once(function () { var idx = globalState.spyListeners.indexOf(listener); if (idx !== -1) globalState.spyListeners.splice(idx, 1); }); }
n/a
function toJS(source, detectCycles, __alreadySeen) { if (detectCycles === void 0) { detectCycles = true; } if (__alreadySeen === void 0) { __alreadySeen = []; } function cache(value) { if (detectCycles) __alreadySeen.push([source, value]); return value; } if (isObservable(source)) { if (detectCycles && __alreadySeen === null) __alreadySeen = []; if (detectCycles && source !== null && typeof source === "object") { for (var i = 0, l = __alreadySeen.length; i < l; i++) if (__alreadySeen[i][0] === source) return __alreadySeen[i][1]; } if (isObservableArray(source)) { var res = cache([]); var toAdd = source.map(function (value) { return toJS(value, detectCycles, __alreadySeen); }); res.length = toAdd.length; for (var i = 0, l = toAdd.length; i < l; i++) res[i] = toAdd[i]; return res; } if (isObservableObject(source)) { var res = cache({}); for (var key in source) res[key] = toJS(source[key], detectCycles, __alreadySeen); return res; } if (isObservableMap(source)) { var res_1 = cache({}); source.forEach(function (value, key) { return res_1[key] = toJS(value, detectCycles, __alreadySeen); }); return res_1; } if (isObservableValue(source)) return toJS(source.get(), detectCycles, __alreadySeen); } return source; }
...
ObservableArray.prototype.replace = function (newItems) {
return this.$mobx.spliceWithArray(0, this.$mobx.values.length, newItems);
};
ObservableArray.prototype.toJS = function () {
return this.slice();
};
ObservableArray.prototype.toJSON = function () {
return this.toJS();
};
ObservableArray.prototype.peek = function () {
return this.$mobx.values;
};
ObservableArray.prototype.find = function (predicate, thisArg, fromIndex) {
if (fromIndex === void 0) { fromIndex = 0; }
this.$mobx.atom.reportObserved();
...
function transaction(action, thisArg) { if (thisArg === void 0) { thisArg = undefined; } deprecated(getMessage("m023")); return runInTransaction.apply(undefined, arguments); }
n/a
function untracked(action) { var prev = untrackedStart(); var res = action(); untrackedEnd(prev); return res; }
...
# 1.1.1
* `toJSON` now serializes object trees with cycles as well. If you know the object tree is acyclic, pass in `false` as second parameter
for a performance gain.
# 1.1.0
* Exposed `ObservableMap` type
* Introduced `mobservable.untracked(block)`
* Introduced `mobservable.autorunAsync(block, delay)`
# 1.0.9
Removed accidental log message
# 1.0.7 / 1.0.8
...
function useStrict(strict) { invariant(globalState.trackingDerivation === null, getMessage("m028")); globalState.strictMode = strict; globalState.allowStateChanges = !strict; }
n/a
function when(arg1, arg2, arg3, arg4) { var name, predicate, effect, scope; if (typeof arg1 === "string") { name = arg1; predicate = arg2; effect = arg3; scope = arg4; } else { name = ("When@" + getNextId()); predicate = arg1; effect = arg2; scope = arg3; } var disposer = autorun(name, function (r) { if (predicate.call(scope)) { r.dispose(); var prevUntracked = untrackedStart(); effect.call(scope); untrackedEnd(prevUntracked); } }); return disposer; }
n/a
function whyRun(thing, prop) { switch (arguments.length) { case 0: thing = globalState.trackingDerivation; if (!thing) return log(getMessage("m024")); break; case 2: thing = getAtom(thing, prop); break; } thing = getAtom(thing); if (isComputedValue(thing)) return log(thing.whyRun()); else if (isReaction(thing)) return log(thing.whyRun()); return fail(getMessage("m025")); }
...
break;
case 2:
thing = getAtom(thing, prop);
break;
}
thing = getAtom(thing);
if (isComputedValue(thing))
return log(thing.whyRun());
else if (isReaction(thing))
return log(thing.whyRun());
return fail(getMessage("m025"));
}
exports.whyRun = whyRun;
function createAction(actionName, fn) {
invariant(typeof fn === "function", getMessage("m026"));
...
function createObservable(v) { if (v === void 0) { v = undefined; } if (typeof arguments[1] === "string") return deepDecorator.apply(null, arguments); invariant(arguments.length <= 1, getMessage("m021")); invariant(!isModifierDescriptor(v), getMessage("m020")); if (isObservable(v)) return v; var res = deepEnhancer(v, undefined, undefined); if (res !== v) return res; return observable.box(v); }
...
# 1.1.7
* Fixed #77: package consumers with --noImplicitAny should be able to build
# 1.1.6
* Introduced `mobservable.fastArray(array)`, in addition to `mobservable.observable(array
)`. Which is much faster when adding items but doesn't support enumerability (`for (var idx in ar) ..` loops).
* Introduced `observableArray.peek()`, for fast access to the array values. Should be used read-only.
# 1.1.5
* Fixed 71: transactions should not influence running computations
# 1.1.4
...
array = function (initialValues, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("array"); return new ObservableArray(initialValues, deepEnhancer, name); }
...
See [#649](https://github.com/mobxjs/mobx/issues/649)
### Factories per observable type
There are now explicit methods to create an observable of a specific type.
* `observable.object(props, name?)` creates a new observable object, by cloning the give props and making them observable
* `observable.array(initialValues, name?)`. Take a guess..
* `observable.map(initialValues, name?)`
* `observable.box(initialValue, name?)`. Creates a [boxed](http://mobxjs.github.io/mobx/refguide/boxed.html) value, which can be
read from / written to using `.get()` and `.set(newValue)`
* `observable(value)`, as-is, based on the type of `value`, uses any of the above four functions to create a new observable.
### Shallow factories per type
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
...
box = function (value, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("box"); return new ObservableValue(value, deepEnhancer, name); }
...
### Factories per observable type
There are now explicit methods to create an observable of a specific type.
* `observable.object(props, name?)` creates a new observable object, by cloning the give props and making them observable
* `observable.array(initialValues, name?)`. Take a guess..
* `observable.map(initialValues, name?)`
* `observable.box(initialValue, name?)`. Creates a [boxed](http://mobxjs.github.io/mobx
/refguide/boxed.html) value, which can be read from / written to using `.get()` and `.set(newValue)`
* `observable(value)`, as-is, based on the type of `value`, uses any of the above four functions to create a new observable.
### Shallow factories per type
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
For example when storing objects from external libraries.
...
deep = function () { if (arguments.length < 2) { return createModifierDescriptor(deepEnhancer, arguments[0]); } else { return deepDecorator.apply(null, arguments); } }
n/a
map = function (initialValues, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("map"); return new ObservableMap(initialValues, deepEnhancer, name); }
...
### Factories per observable type
There are now explicit methods to create an observable of a specific type.
* `observable.object(props, name?)` creates a new observable object, by cloning the give props and making them observable
* `observable.array(initialValues, name?)`. Take a guess..
* `observable.map(initialValues, name?)`
* `observable.box(initialValue, name?)`. Creates a [boxed](http://mobxjs.github.io/mobx/refguide/boxed.html) value, which can be
read from / written to using `.get()` and `.set(newValue)`
* `observable(value)`, as-is, based on the type of `value`, uses any of the above four functions to create a new observable.
### Shallow factories per type
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
...
object = function (props, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("object"); var res = {}; asObservableObject(res, name); extendObservable(res, props); return res; }
...
See [#649](https://github.com/mobxjs/mobx/issues/649)
### Factories per observable type
There are now explicit methods to create an observable of a specific type.
* `observable.object(props, name?)` creates a new observable object, by cloning the give
props and making them observable
* `observable.array(initialValues, name?)`. Take a guess..
* `observable.map(initialValues, name?)`
* `observable.box(initialValue, name?)`. Creates a [boxed](http://mobxjs.github.io/mobx/refguide/boxed.html) value, which can be
read from / written to using `.get()` and `.set(newValue)`
* `observable(value)`, as-is, based on the type of `value`, uses any of the above four functions to create a new observable.
### Shallow factories per type
...
ref = function () { if (arguments.length < 2) { return createModifierDescriptor(referenceEnhancer, arguments[0]); } else { return refDecorator.apply(null, arguments); } }
...
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i](change);
}
untrackedEnd(prevU);
}
function asReference(value) {
deprecated("asReference is deprecated, use observable.ref instead");
return observable.ref(value);
}
exports.asReference = asReference;
function asStructure(value) {
deprecated("asStructure is deprecated. Use observable.struct, computed.struct or reaction options instead.");
return observable.struct(value);
}
exports.asStructure = asStructure;
...
shallow = function () { if (arguments.length < 2) { return createModifierDescriptor(shallowEnhancer, arguments[0]); } else { return shallowDecorator.apply(null, arguments); } }
...
```
Or as property modifier in combination with `observable.object` / `observable.extendObservable`.
Note that modifiers always 'stick' to the property. So they will remain in effect even if a new value is assigned.
```javascript
const taskStore = observable({
tasks: observable.shallow([])
})
```
See [modifiers](http://mobxjs.github.io/mobx/refguide/modifiers.html)
### `computed` api has been simplified
...
shallowArray = function (initialValues, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("shallowArray"); return new ObservableArray(initialValues, referenceEnhancer, name); }
...
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
For example when storing objects from external libraries.
In MobX 2 you needed to use `asFlat` or `asReference` modifiers for this.
In MobX 3, there are factories to directly create non-converting data structures:
* `observable.shallowObject(props, name?)`
* `observable.shallowArray(initialValues, name?)`
* `observable.shallowMap(initialValues, name?)`
* `observable.shallowBox(initialValue, name?)`
So for example, `observable.shallowArray([todo1, todo2])` will create an observable array, but it won't try to convert the
todos inside the array into observables as well.
### Shallow properties
...
shallowBox = function (value, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("shallowBox"); return new ObservableValue(value, referenceEnhancer, name); }
...
For example when storing objects from external libraries.
In MobX 2 you needed to use `asFlat` or `asReference` modifiers for this.
In MobX 3, there are factories to directly create non-converting data structures:
* `observable.shallowObject(props, name?)`
* `observable.shallowArray(initialValues, name?)`
* `observable.shallowMap(initialValues, name?)`
* `observable.shallowBox(initialValue, name?)`
So for example, `observable.shallowArray([todo1, todo2])` will create an observable array, but it won't try to convert the
todos inside the array into observables as well.
### Shallow properties
The `@observable` decorator can still be used to introduce observable properties. And like in MobX 2, it will automatically convert
its values.
...
shallowMap = function (initialValues, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("shallowMap"); return new ObservableMap(initialValues, referenceEnhancer, name); }
...
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
For example when storing objects from external libraries.
In MobX 2 you needed to use `asFlat` or `asReference` modifiers for this.
In MobX 3, there are factories to directly create non-converting data structures:
* `observable.shallowObject(props, name?)`
* `observable.shallowArray(initialValues, name?)`
* `observable.shallowMap(initialValues, name?)`
* `observable.shallowBox(initialValue, name?)`
So for example, `observable.shallowArray([todo1, todo2])` will create an observable array, but it won't try to convert the
todos inside the array into observables as well.
### Shallow properties
The `@observable` decorator can still be used to introduce observable properties. And like in MobX 2, it will automatically convert
its values.
...
shallowObject = function (props, name) { if (arguments.length > 2) incorrectlyUsedAsDecorator("shallowObject"); var res = {}; asObservableObject(res, name); extendShallowObservable(res, props); return res; }
...
The standard observable factories create observable structures that will try to turn any plain javascript value (arrays, objects
or Maps) into observables.
Allthough this is fine in most cases, in some cases you might want to disable this autoconversion.
For example when storing objects from external libraries.
In MobX 2 you needed to use `asFlat` or `asReference` modifiers for this.
In MobX 3, there are factories to directly create non-converting data structures:
* `observable.shallowObject(props, name?)`
* `observable.shallowArray(initialValues, name?)`
* `observable.shallowMap(initialValues, name?)`
* `observable.shallowBox(initialValue, name?)`
So for example, `observable.shallowArray([todo1, todo2])` will create an observable array, but it won't try to convert the
todos inside the array into observables as well.
### Shallow properties
...
struct = function () { if (arguments.length < 2) { return createModifierDescriptor(deepStructEnhancer, arguments[0]); } else { return deepStructDecorator.apply(null, arguments); } }
...
function asReference(value) {
deprecated("asReference is deprecated, use observable.ref instead");
return observable.ref(value);
}
exports.asReference = asReference;
function asStructure(value) {
deprecated("asStructure is deprecated. Use observable.struct, computed.struct or reaction options instead.");
return observable.struct(value);
}
exports.asStructure = asStructure;
function asFlat(value) {
deprecated("asFlat is deprecated, use observable.shallow instead");
return observable.shallow(value);
}
exports.asFlat = asFlat;
...