New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mobx-state-tree

Package Overview
Dependencies
Maintainers
2
Versions
129
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-state-tree - npm Package Compare versions

Comparing version 0.3.2 to 0.3.3

5

changelog.md

@@ -0,1 +1,6 @@

# 0.3.3
* Introduced lifecycle hooks `afterCreate`, `afterAttach`, `beforeDetach`, `beforeDestroy`, implements #76
* Introduced the convenience method `addDisposer(this, cb)` that can be used to easily destruct reactions etc. which are set up in `afterCreate`. See #76
# 0.3.2

@@ -2,0 +7,0 @@

5

lib/core/mst-node-administration.d.ts

@@ -16,6 +16,7 @@ import { IType } from "../types/type";

private _isProtected;
private _isDetaching;
readonly middlewares: IMiddleWareHandler[];
private readonly snapshotSubscribers;
private readonly patchSubscribers;
private readonly snapshotDisposer;
private readonly disposers;
constructor(parent: MSTAdministration | null, subpath: string, initialState: any, type: ComplexType<any, any>, environment: any);

@@ -40,2 +41,3 @@ /**

setParent(newParent: MSTAdministration | null, subpath?: string | null): void;
addDisposer(disposer: () => void): void;
reconcileChildren<T>(childType: IType<any, T>, oldValues: T[], newValues: T[], newPaths: (string | number)[]): T[];

@@ -56,2 +58,3 @@ resolve(pathParts: string): MSTAdministration;

detach(): void;
fireHook(name: string): void;
}

44

lib/core/mst-node-administration.js

@@ -25,7 +25,9 @@ "use strict";

this._isRunningAction = false; // only relevant for root
this._isAlive = true;
this._isAlive = true; // optimization: use binary flags for all these switches
this._isProtected = false;
this._isDetaching = false;
this.middlewares = [];
this.snapshotSubscribers = [];
this.patchSubscribers = [];
this.disposers = [];
utils_1.invariant(type instanceof complex_type_1.ComplexType, "Uh oh");

@@ -38,8 +40,9 @@ utils_1.addHiddenFinalProp(initialState, "$treenode", this);

this._environment = environment;
this.snapshotDisposer = mobx_1.reaction(function () { return _this.snapshot; }, function (snapshot) {
var snapshotDisposer = mobx_1.reaction(function () { return _this.snapshot; }, function (snapshot) {
_this.snapshotSubscribers.forEach(function (f) { return f(snapshot); });
});
this.snapshotDisposer.onError(function (e) {
snapshotDisposer.onError(function (e) {
throw e;
});
this.addDisposer(snapshotDisposer);
}

@@ -91,4 +94,11 @@ Object.defineProperty(MSTAdministration.prototype, "path", {

MSTAdministration.prototype.die = function () {
if (this._isDetaching)
return;
this.type.getChildMSTs(this).forEach(function (_a) {
var _ = _a[0], node = _a[1];
node.die();
});
// TODO: kill $mobx.values
this.snapshotDisposer();
this.fireHook("beforeDestroy");
this.disposers.splice(0).reverse().forEach(function (f) { return f(); });
this.patchSubscribers.splice(0);

@@ -98,6 +108,8 @@ this.snapshotSubscribers.splice(0);

this._isAlive = false;
this._parent = null;
this.subpath = "";
// Post conditions, element will not be in the tree anymore...
};
MSTAdministration.prototype.assertAlive = function () {
if (!this._isAlive || (this.root && !this.root._isAlive))
if (!this._isAlive)
utils_1.fail("The model cannot be used anymore as it has died; it has been removed from a state tree. If you want to remove an element from a tree and let it live on, use 'detach' or 'clone' the value");

@@ -108,2 +120,3 @@ };

// advantage of using computed for a snapshot is that nicely respects transactions etc.
// Optimization: only freeze on dev builds
return Object.freeze(this.type.serialize(this));

@@ -158,8 +171,2 @@ },

if (this.parent && !newParent) {
if (this.patchSubscribers.length > 0 ||
this.snapshotSubscribers.length > 0 ||
(this instanceof object_1.ObjectType && this.middlewares.length > 0)) {
console.warn("An object with active event listeners was removed from the tree. The subscriptions have been disposed.");
}
this._parent = newParent;
this.die();

@@ -170,4 +177,8 @@ }

this.subpath = subpath || ""; // TODO: mweh
this.fireHook("afterAttach");
}
};
MSTAdministration.prototype.addDisposer = function (disposer) {
this.disposers.push(disposer);
};
MSTAdministration.prototype.reconcileChildren = function (childType, oldValues, newValues, newPaths) {

@@ -313,7 +324,16 @@ var _this = this;

else {
this.fireHook("beforeDetach");
this._environment = this.root._environment; // make backup of environment
this._isDetaching = true;
this.parent.removeChild(this.subpath);
this._isAlive = true;
this._parent = null;
this.subpath = "";
this._isDetaching = false;
}
};
MSTAdministration.prototype.fireHook = function (name) {
var fn = this.target[name];
if (typeof fn === "function")
fn.apply(this.target);
};
return MSTAdministration;

@@ -320,0 +340,0 @@ }());

@@ -254,3 +254,4 @@ import { IRawActionCall, ISerializedActionCall } from "./action";

export declare function isAlive(thing: IMSTNode): boolean;
export declare function addDisposer(thing: IMSTNode, disposer: () => void): void;
export declare function getEnv(thing: IMSTNode): any;
export declare function testActions<S, T>(factory: IType<S, IMSTNode>, initialState: S, ...actions: ISerializedActionCall[]): S;

@@ -325,4 +325,6 @@ "use strict";

var node = mst_node_1.getMSTAdministration(thing);
node.detach();
node.die();
if (node.isRoot)
node.die();
else
node.parent.removeChild(node.subpath);
}

@@ -334,2 +336,6 @@ exports.destroy = destroy;

exports.isAlive = isAlive;
function addDisposer(thing, disposer) {
mst_node_1.getMSTAdministration(thing).addDisposer(disposer);
}
exports.addDisposer = addDisposer;
function getEnv(thing) {

@@ -336,0 +342,0 @@ var node = mst_node_1.getMSTAdministration(thing);

@@ -35,3 +35,5 @@ "use strict";

this.finalizeNewInstance(instance, snapshot);
Object.seal(instance);
node.fireHook("afterCreate");
if (parent)
node.fireHook("afterAttach");
return instance;

@@ -38,0 +40,0 @@ };

@@ -46,3 +46,3 @@ "use strict";

};
Object.seal(baseModel); // make sure nobody messes with it
Object.freeze(baseModel); // make sure nobody messes with it
_this.baseModel = baseModel;

@@ -49,0 +49,0 @@ utils_1.invariant(/^\w[\w\d_]*$/.test(name), "Typename should be a valid identifier: " + name);

{
"name": "mobx-state-tree",
"version": "0.3.2",
"version": "0.3.3",
"description": "Opinionated, transactional, MobX powered state container",

@@ -11,3 +11,4 @@ "main": "lib/index.js",

"webpack": "webpack -p",
"test": "tsc && tsc -p test/ && ava",
"build-tests": "tsc && tsc -p test/",
"test": "npm run build-tests && ava",
"watch": "concurrently --kill-others --names 'build,build-tests,test-runner' 'tsc --watch' 'tsc --watch -p test' --raw 'ava --watch'",

@@ -18,3 +19,3 @@ "prepublish": "npm run build && npm run build-docs",

"lint": "tslint -c tslint.json 'src/**/*.ts'",
"clean": "rm -rf lib && rm -rf test-lib"
"clean": "rm -rf lib test-lib .nyc_output coverage"
},

@@ -21,0 +22,0 @@ "repository": {

@@ -288,2 +288,13 @@ # mobx-state-tree

TODO: document
## LifeCycle hooks
| Hook | Meaning |
| --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `afterCreate` | Immediately after an instance is created and initial values are applied. Children will fire this event before parents |
| `afterAttach` | As soon as the _direct_ parent is assigned (this node is attached to an other node) |
| `beforeDetach` | As soon as the node is removed from the _direct_ parent, but only if the node is _not_ destroyed. In other words, when `detach(node)` is used |
| `beforeDestroy` | Before the node is destroyed as a result of calling `destroy` or removing or replacing the node from the tree. Child destructors will fire before parents |
## Single or multiple state

@@ -303,3 +314,3 @@

[lib/core/mst-node.js:28-40](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/lib/core/mst-node.js#L28-L40 "Source code on GitHub")
[lib/core/mst-node.js:28-40](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-node.js#L28-L40 "Source code on GitHub")

@@ -318,3 +329,3 @@ Tries to convert a value to a TreeNode. If possible or already done,

[lib/types/complex-types/complex-type.js:18-48](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/lib/types/complex-types/complex-type.js#L18-L48 "Source code on GitHub")
[lib/types/complex-types/complex-type.js:18-50](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/types/complex-types/complex-type.js#L18-L50 "Source code on GitHub")

@@ -325,3 +336,3 @@ A complex type produces a MST node (Node in the state tree)

[lib/core/mst-node-administration.js:48-52](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/lib/core/mst-node-administration.js#L48-L52 "Source code on GitHub")
[lib/core/mst-node-administration.js:51-55](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-node-administration.js#L51-L55 "Source code on GitHub")

@@ -332,3 +343,3 @@ Returnes (escaped) path representation as string

[lib/types/index.js:24-26](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/lib/types/index.js#L24-L26 "Source code on GitHub")
[lib/types/index.js:24-26](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/types/index.js#L24-L26 "Source code on GitHub")

@@ -341,3 +352,3 @@ **Parameters**

[lib/types/index.js:34-36](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/lib/types/index.js#L34-L36 "Source code on GitHub")
[lib/types/index.js:34-36](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/types/index.js#L34-L36 "Source code on GitHub")

@@ -350,3 +361,3 @@ **Parameters**

[lib/types/complex-types/object.js:41-41](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/types/complex-types/object.js#L41-L41 "Source code on GitHub")

@@ -357,3 +368,3 @@ Parsed description of all properties

[lib/core/mst-operations.js:50-55](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L50-L55 "Source code on GitHub")

@@ -402,3 +413,3 @@ TODO: update docs

[lib/core/mst-operations.js:67-69](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L67-L69 "Source code on GitHub")

@@ -418,3 +429,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/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L83-L85 "Source code on GitHub")

@@ -430,3 +441,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/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L94-L99 "Source code on GitHub")

@@ -442,3 +453,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/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L125-L129 "Source code on GitHub")

@@ -457,3 +468,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/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L163-L165 "Source code on GitHub")

@@ -488,3 +499,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/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L170-L172 "Source code on GitHub")

@@ -499,3 +510,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/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L182-L184 "Source code on GitHub")

@@ -511,3 +522,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/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L198-L208 "Source code on GitHub")

@@ -525,3 +536,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/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L234-L236 "Source code on GitHub")

@@ -538,3 +549,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/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L245-L247 "Source code on GitHub")

@@ -551,3 +562,3 @@ Returns the path of the given object as unescaped string array

[lib/core/mst-operations.js:256-258](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/lib/core/mst-operations.js#L256-L258 "Source code on GitHub")
[lib/core/mst-operations.js:256-258](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L256-L258 "Source code on GitHub")

@@ -564,3 +575,3 @@ Returns true if the given object is the root of a model tree

[lib/core/mst-operations.js:268-272](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/lib/core/mst-operations.js#L268-L272 "Source code on GitHub")
[lib/core/mst-operations.js:268-272](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L268-L272 "Source code on GitHub")

@@ -578,3 +589,3 @@ Resolves a path relatively to a given object.

[lib/core/mst-operations.js:282-287](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/lib/core/mst-operations.js#L282-L287 "Source code on GitHub")
[lib/core/mst-operations.js:282-287](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L282-L287 "Source code on GitHub")

@@ -590,3 +601,3 @@ **Parameters**

[lib/core/mst-operations.js:301-310](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/lib/core/mst-operations.js#L301-L310 "Source code on GitHub")
[lib/core/mst-operations.js:301-310](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L301-L310 "Source code on GitHub")

@@ -602,3 +613,3 @@ **Parameters**

[lib/core/mst-operations.js:315-318](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/lib/core/mst-operations.js#L315-L318 "Source code on GitHub")
[lib/core/mst-operations.js:315-318](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L315-L318 "Source code on GitHub")

@@ -613,3 +624,3 @@ Removes a model element from the state tree, and let it live on as a new state tree

[lib/core/mst-operations.js:323-327](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/lib/core/mst-operations.js#L323-L327 "Source code on GitHub")
[lib/core/mst-operations.js:323-329](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L323-L329 "Source code on GitHub")

@@ -624,3 +635,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/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/lib/core/action.js#L107-L114 "Source code on GitHub")
[lib/core/action.js:107-114](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/action.js#L107-L114 "Source code on GitHub")

@@ -638,3 +649,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/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/json-patch.js#L9-L11 "Source code on GitHub")

@@ -650,3 +661,3 @@ escape slashes and backslashes

[lib/core/json-patch.js:16-18](https://github.com/mweststrate/mobx-state-tree/blob/1ee69d1bec57566bfdd3cad1fb1b00b30a380333/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/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/json-patch.js#L16-L18 "Source code on GitHub")

@@ -653,0 +664,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 not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc