mobx-state-tree
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -0,1 +1,5 @@ | ||
# 0.3.1 | ||
* (re) introduced the concept of environments, which can be passed as second argument to `.create`, and picked up using `getEnv` | ||
# 0.3.0 | ||
@@ -2,0 +6,0 @@ |
@@ -10,5 +10,6 @@ import { IType } from "../types/type"; | ||
readonly type: ComplexType<any, any>; | ||
_environment: any; | ||
_isRunningAction: boolean; | ||
private _isAlive; | ||
private _isProtected; | ||
_isRunningAction: boolean; | ||
readonly middlewares: IMiddleWareHandler[]; | ||
@@ -18,3 +19,3 @@ private readonly snapshotSubscribers; | ||
private readonly snapshotDisposer; | ||
constructor(initialState: any, type: ComplexType<any, any>); | ||
constructor(initialState: any, type: ComplexType<any, any>, environment: any); | ||
readonly pathParts: string[]; | ||
@@ -21,0 +22,0 @@ /** |
@@ -17,8 +17,9 @@ "use strict"; | ||
var MSTAdminisration = (function () { | ||
function MSTAdminisration(initialState, type) { | ||
function MSTAdminisration(initialState, type, environment) { | ||
var _this = this; | ||
this._parent = null; | ||
this._environment = undefined; | ||
this._isRunningAction = false; // only relevant for root | ||
this._isAlive = true; | ||
this._isProtected = false; | ||
this._isRunningAction = false; // only relevant for root | ||
this.middlewares = []; | ||
@@ -31,2 +32,3 @@ this.snapshotSubscribers = []; | ||
this.target = initialState; | ||
this._environment = environment; | ||
this.snapshotDisposer = mobx_1.reaction(function () { return _this.snapshot; }, function (snapshot) { | ||
@@ -164,7 +166,10 @@ _this.snapshotSubscribers.forEach(function (f) { return f(snapshot); }); | ||
if (this._parent && newParent) { | ||
utils_1.invariant(false, "A node cannot exists twice in the state tree. Failed to add object to path '/" + newParent.pathParts.concat(subpath).join("/") + "', it exists already at '" + this.path + "'"); | ||
utils_1.fail("A node cannot exists twice in the state tree. Failed to add object to path '/" + newParent.pathParts.concat(subpath).join("/") + "', it exists already at '" + this.path + "'"); | ||
} | ||
if (!this._parent && newParent && newParent.root === this) { | ||
utils_1.invariant(false, "A state tree is not allowed to contain itself. Cannot add root to path '/" + newParent.pathParts.concat(subpath).join("/") + "'"); | ||
utils_1.fail("A state tree is not allowed to contain itself. Cannot add root to path '/" + newParent.pathParts.concat(subpath).join("/") + "'"); | ||
} | ||
if (!this._parent && !!this._environment) { | ||
utils_1.fail("A state tree that has been initialized with an environment cannot be made part of another state tree."); | ||
} | ||
if (this.parent && !newParent) { | ||
@@ -289,6 +294,10 @@ if (this.patchSubscribers.length > 0 || | ||
MSTAdminisration.prototype.detach = function () { | ||
utils_1.invariant(this._isAlive); | ||
if (this.isRoot) | ||
return; | ||
else | ||
else { | ||
this._environment = this.root._environment; // make backup of environment | ||
this.parent.removeChild(this.subpath); | ||
this._isAlive = true; | ||
} | ||
}; | ||
@@ -295,0 +304,0 @@ return MSTAdminisration; |
@@ -245,3 +245,3 @@ import { IRawActionCall, ISerializedActionCall } from "./action"; | ||
*/ | ||
export declare function clone<T extends IMSTNode>(source: T): T; | ||
export declare function clone<T extends IMSTNode>(source: T, keepEnvironment?: boolean | any): T; | ||
/** | ||
@@ -255,2 +255,3 @@ * Removes a model element from the state tree, and let it live on as a new state tree | ||
export declare function destroy(thing: IMSTNode): void; | ||
export declare function getEnv(thing: IMSTNode): any; | ||
export declare function testActions<S, T>(factory: IType<S, IMSTNode>, initialState: S, ...actions: ISerializedActionCall[]): S; |
@@ -311,5 +311,11 @@ "use strict"; | ||
*/ | ||
function clone(source) { | ||
function clone(source, keepEnvironment) { | ||
if (keepEnvironment === void 0) { keepEnvironment = true; } | ||
var node = mst_node_1.getMSTAdministration(source); | ||
return node.type.create(node.snapshot); | ||
return node.type.create(node.snapshot, keepEnvironment === true | ||
? node.root._environment | ||
: keepEnvironment === false | ||
? undefined | ||
: keepEnvironment // it's an object or something else | ||
); | ||
} | ||
@@ -334,2 +340,9 @@ exports.clone = clone; | ||
exports.destroy = destroy; | ||
function getEnv(thing) { | ||
var node = mst_node_1.getMSTAdministration(thing); | ||
var env = node.root._environment; | ||
utils_1.invariant(!!env, "Node '" + node.path + "' is not part of state tree that was initialized with an environment. Environment can be passed as second argumentt to .create()"); | ||
return env; | ||
} | ||
exports.getEnv = getEnv; | ||
// TODO: remove or to test utils? | ||
@@ -336,0 +349,0 @@ function testActions(factory, initialState) { |
@@ -7,3 +7,3 @@ import { IType, Type } from "../type"; | ||
constructor(name: string); | ||
create(snapshot?: any): any; | ||
create(snapshot?: any, environment?: any): any; | ||
abstract createNewInstance(): any; | ||
@@ -10,0 +10,0 @@ abstract finalizeNewInstance(target: any, snapshot: any): void; |
@@ -25,3 +25,3 @@ "use strict"; | ||
} | ||
ComplexType.prototype.create = function (snapshot) { | ||
ComplexType.prototype.create = function (snapshot, environment) { | ||
if (snapshot === void 0) { snapshot = this.getDefaultSnapshot(); } | ||
@@ -31,3 +31,3 @@ type_1.typecheck(this, snapshot); | ||
// tslint:disable-next-line:no_unused-variable | ||
var node = new mst_node_administration_1.MSTAdminisration(instance, this); | ||
var node = new mst_node_administration_1.MSTAdminisration(instance, this, environment); | ||
this.finalizeNewInstance(instance, snapshot); | ||
@@ -34,0 +34,0 @@ Object.seal(instance); |
@@ -6,3 +6,3 @@ export interface ISnapshottable<S> { | ||
is(thing: any): thing is S | T; | ||
create(snapshot?: S): T; | ||
create(snapshot?: S, environment?: any): T; | ||
isType: boolean; | ||
@@ -9,0 +9,0 @@ describe(): string; |
{ | ||
"name": "mobx-state-tree", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Opinionated, transactional, MobX powered state container", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -296,2 +296,4 @@ # mobx-state-tree | ||
# Environments | ||
# API | ||
@@ -301,3 +303,3 @@ | ||
[lib/core/mst-node.js:27-39](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-node.js#L27-L39 "Source code on GitHub") | ||
[lib/core/mst-node.js:27-39](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-node.js#L27-L39 "Source code on GitHub") | ||
@@ -316,3 +318,3 @@ Tries to convert a value to a TreeNode. If possible or already done, | ||
[lib/types/complex-types/complex-type.js:18-45](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/types/complex-types/complex-type.js#L18-L45 "Source code on GitHub") | ||
[lib/types/complex-types/complex-type.js:18-45](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/types/complex-types/complex-type.js#L18-L45 "Source code on GitHub") | ||
@@ -323,3 +325,3 @@ A complex type produces a MST node (Node in the state tree) | ||
[lib/core/mst-node-administration.js:61-64](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-node-administration.js#L61-L64 "Source code on GitHub") | ||
[lib/core/mst-node-administration.js:63-66](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-node-administration.js#L63-L66 "Source code on GitHub") | ||
@@ -330,3 +332,3 @@ Returnes (escaped) path representation as string | ||
[lib/types/index.js:24-26](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/types/index.js#L24-L26 "Source code on GitHub") | ||
[lib/types/index.js:24-26](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/types/index.js#L24-L26 "Source code on GitHub") | ||
@@ -339,3 +341,3 @@ **Parameters** | ||
[lib/types/index.js:34-36](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/types/index.js#L34-L36 "Source code on GitHub") | ||
[lib/types/index.js:34-36](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/types/index.js#L34-L36 "Source code on GitHub") | ||
@@ -348,3 +350,3 @@ **Parameters** | ||
[lib/types/complex-types/object.js:41-41](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/types/complex-types/object.js#L41-L41 "Source code on GitHub") | ||
[lib/types/complex-types/object.js:41-41](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/types/complex-types/object.js#L41-L41 "Source code on GitHub") | ||
@@ -355,3 +357,3 @@ Parsed description of all properties | ||
[lib/core/mst-operations.js:50-55](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L50-L55 "Source code on GitHub") | ||
[lib/core/mst-operations.js:50-55](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L50-L55 "Source code on GitHub") | ||
@@ -400,3 +402,3 @@ TODO: update docs | ||
[lib/core/mst-operations.js:67-69](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L67-L69 "Source code on GitHub") | ||
[lib/core/mst-operations.js:67-69](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L67-L69 "Source code on GitHub") | ||
@@ -416,3 +418,3 @@ Registers a function that will be invoked for each that as made to the provided model instance, or any of it's children. | ||
[lib/core/mst-operations.js:83-85](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L83-L85 "Source code on GitHub") | ||
[lib/core/mst-operations.js:83-85](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L83-L85 "Source code on GitHub") | ||
@@ -428,3 +430,3 @@ Applies a JSON-patch to the given model instance or bails out if the patch couldn't be applied | ||
[lib/core/mst-operations.js:94-99](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L94-L99 "Source code on GitHub") | ||
[lib/core/mst-operations.js:94-99](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L94-L99 "Source code on GitHub") | ||
@@ -440,3 +442,3 @@ Applies a number of JSON patches in a single MobX transaction | ||
[lib/core/mst-operations.js:125-129](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L125-L129 "Source code on GitHub") | ||
[lib/core/mst-operations.js:125-129](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L125-L129 "Source code on GitHub") | ||
@@ -455,3 +457,3 @@ Applies a series of actions in a single MobX transaction. | ||
[lib/core/mst-operations.js:163-165](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L163-L165 "Source code on GitHub") | ||
[lib/core/mst-operations.js:163-165](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L163-L165 "Source code on GitHub") | ||
@@ -486,3 +488,3 @@ By default it is allowed to both directly modify a model or through an action. | ||
[lib/core/mst-operations.js:170-172](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L170-L172 "Source code on GitHub") | ||
[lib/core/mst-operations.js:170-172](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L170-L172 "Source code on GitHub") | ||
@@ -497,3 +499,3 @@ Returns true if the object is in protected mode, @see protect | ||
[lib/core/mst-operations.js:182-184](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L182-L184 "Source code on GitHub") | ||
[lib/core/mst-operations.js:182-184](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L182-L184 "Source code on GitHub") | ||
@@ -509,3 +511,3 @@ Applies a snapshot to a given model instances. Patch and snapshot listeners will be invoked as usual. | ||
[lib/core/mst-operations.js:198-208](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L198-L208 "Source code on GitHub") | ||
[lib/core/mst-operations.js:198-208](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L198-L208 "Source code on GitHub") | ||
@@ -523,3 +525,3 @@ Given a model instance, returns `true` if the object has a parent, that is, is part of another object, map or array | ||
[lib/core/mst-operations.js:234-236](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L234-L236 "Source code on GitHub") | ||
[lib/core/mst-operations.js:234-236](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L234-L236 "Source code on GitHub") | ||
@@ -536,3 +538,3 @@ Returns the path of the given object in the model tree | ||
[lib/core/mst-operations.js:245-247](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L245-L247 "Source code on GitHub") | ||
[lib/core/mst-operations.js:245-247](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L245-L247 "Source code on GitHub") | ||
@@ -549,3 +551,3 @@ Returns the path of the given object as unescaped string array | ||
[lib/core/mst-operations.js:271-273](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L271-L273 "Source code on GitHub") | ||
[lib/core/mst-operations.js:271-273](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L271-L273 "Source code on GitHub") | ||
@@ -562,3 +564,3 @@ Returns true if the given object is the root of a model tree | ||
[lib/core/mst-operations.js:283-286](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L283-L286 "Source code on GitHub") | ||
[lib/core/mst-operations.js:283-286](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L283-L286 "Source code on GitHub") | ||
@@ -576,3 +578,3 @@ Resolves a path relatively to a given object. | ||
[lib/core/mst-operations.js:296-301](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L296-L301 "Source code on GitHub") | ||
[lib/core/mst-operations.js:296-301](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L296-L301 "Source code on GitHub") | ||
@@ -588,3 +590,3 @@ **Parameters** | ||
[lib/core/mst-operations.js:311-314](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L311-L314 "Source code on GitHub") | ||
[lib/core/mst-operations.js:311-320](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L311-L320 "Source code on GitHub") | ||
@@ -594,2 +596,3 @@ **Parameters** | ||
- `source` **T** | ||
- `keepEnvironment` | ||
@@ -600,3 +603,3 @@ Returns **T** | ||
[lib/core/mst-operations.js:319-322](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L319-L322 "Source code on GitHub") | ||
[lib/core/mst-operations.js:325-328](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L325-L328 "Source code on GitHub") | ||
@@ -611,3 +614,3 @@ Removes a model element from the state tree, and let it live on as a new state tree | ||
[lib/core/mst-operations.js:327-331](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/mst-operations.js#L327-L331 "Source code on GitHub") | ||
[lib/core/mst-operations.js:333-337](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/mst-operations.js#L333-L337 "Source code on GitHub") | ||
@@ -622,3 +625,3 @@ Removes a model element from the state tree, and mark it as end-of-life; the element should not be used anymore | ||
[lib/core/action.js:107-114](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/action.js#L107-L114 "Source code on GitHub") | ||
[lib/core/action.js:107-114](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/action.js#L107-L114 "Source code on GitHub") | ||
@@ -636,3 +639,3 @@ Dispatches an Action on a model instance. All middlewares will be triggered. | ||
[lib/core/json-patch.js:9-11](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/json-patch.js#L9-L11 "Source code on GitHub") | ||
[lib/core/json-patch.js:9-11](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/json-patch.js#L9-L11 "Source code on GitHub") | ||
@@ -648,3 +651,3 @@ escape slashes and backslashes | ||
[lib/core/json-patch.js:16-18](https://github.com/mweststrate/mobx-state-tree/blob/e4ace0a5b415489d11f33d6078345a8388f29af1/lib/core/json-patch.js#L16-L18 "Source code on GitHub") | ||
[lib/core/json-patch.js:16-18](https://github.com/mweststrate/mobx-state-tree/blob/2395e1b7309388a3e8782026cdb86e20b6b09161/lib/core/json-patch.js#L16-L18 "Source code on GitHub") | ||
@@ -651,0 +654,0 @@ unescape slashes and backslashes |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
508983
5943
688