mobx-state-tree
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -0,1 +1,5 @@ | ||
# 0.2.1 | ||
* Introduced .Type and .SnapshotType to be used with TypeScript to get the type for a model | ||
# 0.2.0 | ||
@@ -2,0 +6,0 @@ |
@@ -0,0 +0,0 @@ export declare type IActionCall = { |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IType, Type } from "./type"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -6,11 +6,11 @@ import { IType, ITypeChecker } from "./types"; | ||
export interface IFactory<S, T> { | ||
create(snapshot?: S): T & IModel; | ||
(snapshot?: S, env?: Object): T & IModel; | ||
factoryName: string; | ||
is: ITypeChecker<S, T>; | ||
is: ITypeChecker; | ||
isFactory: boolean; | ||
type: IType<S, T>; | ||
type: IType; | ||
} | ||
export declare function isType(value: any): value is IFactory<any, any>; | ||
export declare function getType(object: IModel): IFactory<any, any>; | ||
export declare function getChildType(object: IModel, child: string): IFactory<any, any>; | ||
export declare function isFactory(value: any): value is IFactory<any, any>; | ||
export declare function getFactory(object: IModel): IFactory<any, any>; | ||
export declare function getChildFactory(object: IModel, child: string): IFactory<any, any>; | ||
export declare function isModel(model: any): model is IModel; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var node_1 = require("./node"); | ||
function isType(value) { | ||
return typeof value === "object" && value && value.isFactory === true; | ||
function isFactory(value) { | ||
return typeof value === "function" && value.isFactory === true; | ||
} | ||
exports.isType = isType; | ||
function getType(object) { | ||
exports.isFactory = isFactory; | ||
function getFactory(object) { | ||
return node_1.getNode(object).factory; | ||
} | ||
exports.getType = getType; | ||
function getChildType(object, child) { | ||
return node_1.getNode(object).getChildType(child); | ||
exports.getFactory = getFactory; | ||
function getChildFactory(object, child) { | ||
return node_1.getNode(object).getChildFactory(child); | ||
} | ||
exports.getChildType = getChildType; | ||
exports.getChildFactory = getChildFactory; | ||
// TODO: ambigous function name, remove | ||
@@ -17,0 +17,0 @@ function isModel(model) { |
@@ -0,0 +0,0 @@ export * from "./type"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ export declare type IJsonPatch = { |
@@ -0,0 +0,0 @@ // TODO: move file to utils |
@@ -0,0 +0,0 @@ import { IType } from "./type"; |
@@ -212,6 +212,4 @@ "use strict"; | ||
if (current === null) { | ||
if (failIfResolveFails) { | ||
debugger; | ||
if (failIfResolveFails) | ||
return utils_1.fail("Could not resolve '" + pathParts[i] + "' in '" + json_patch_1.joinJsonPath(pathParts.slice(0, i - 1)) + "', path of the patch does not resolve"); | ||
} | ||
else | ||
@@ -218,0 +216,0 @@ return undefined; |
@@ -0,0 +0,0 @@ import { IType } from "./type"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -1,2 +0,3 @@ | ||
import { ComplexType, IModel, IType } from './type'; | ||
import { ComplexType } from "./types"; | ||
import { IModel, IFactory } from "./factories"; | ||
import { IActionHandler } from "./action"; | ||
@@ -6,11 +7,14 @@ import { IDisposer } from "../utils"; | ||
import { IActionCall } from "./action"; | ||
export declare class MSTAdminisration { | ||
export declare class Node { | ||
readonly target: any; | ||
_parent: MSTAdminisration | null; | ||
readonly type: ComplexType<any, any>; | ||
readonly snapshotSubscribers: ((snapshot: any) => void)[]; | ||
readonly environment: any; | ||
_parent: Node | null; | ||
readonly factory: IFactory<any, any>; | ||
private interceptDisposer; | ||
readonly snapshotSubscribers: ((snapshot) => void)[]; | ||
readonly patchSubscribers: ((patches: IJsonPatch) => void)[]; | ||
readonly actionSubscribers: IActionHandler[]; | ||
_isRunningAction: boolean; | ||
constructor(initialState: any, type: ComplexType<any, any>); | ||
constructor(initialState: any, environment: any, factory: IFactory<any, any>); | ||
readonly type: ComplexType; | ||
readonly pathParts: string[]; | ||
@@ -23,27 +27,27 @@ /** | ||
readonly isRoot: boolean; | ||
readonly parent: MSTAdminisration | null; | ||
readonly root: MSTAdminisration; | ||
readonly parent: Node | null; | ||
readonly snapshot: any; | ||
onSnapshot(onChange: (snapshot: any) => void): IDisposer; | ||
applySnapshot(snapshot: any): void; | ||
onSnapshot(onChange: (snapshot) => void): IDisposer; | ||
applySnapshot(snapshot: any): any; | ||
applyPatch(patch: IJsonPatch): void; | ||
applyPatchLocally(subpath: string, patch: IJsonPatch): void; | ||
applyPatchLocally(subpath: any, patch: IJsonPatch): void; | ||
onPatch(onPatch: (patches: IJsonPatch) => void): IDisposer; | ||
emitPatch(patch: IJsonPatch, source: MSTAdminisration, distance?: number): void; | ||
setParent(newParent: MSTAdminisration | null, subpath?: string | null): void; | ||
emitPatch(patch: IJsonPatch, source: Node, distance?: number): void; | ||
setParent(newParent: Node | null, subpath?: string | null): void; | ||
prepareChild(subpath: string, child: any): any; | ||
detach(): void; | ||
resolve(pathParts: string): MSTAdminisration; | ||
resolve(pathParts: string, failIfResolveFails: boolean): MSTAdminisration | undefined; | ||
resolvePath(pathParts: string[]): MSTAdminisration; | ||
resolvePath(pathParts: string[], failIfResolveFails: boolean): MSTAdminisration | undefined; | ||
resolve(pathParts: string): Node; | ||
resolve(pathParts: string, failIfResolveFails: boolean): Node | undefined; | ||
resolvePath(pathParts: string[]): Node; | ||
resolvePath(pathParts: string[], failIfResolveFails: boolean): Node | undefined; | ||
isRunningAction(): boolean; | ||
applyAction(action: IActionCall): void; | ||
emitAction(instance: MSTAdminisration, action: IActionCall, next: () => any): any; | ||
emitAction(instance: Node, action: IActionCall, next: any): void; | ||
onAction(listener: (action: IActionCall, next: () => void) => void): IDisposer; | ||
getChildMST(subpath: string): MSTAdminisration | null; | ||
getChildMSTs(): [string, MSTAdminisration][]; | ||
getChildType(key: string): IType<any, any>; | ||
getFromEnvironment(key: string): any; | ||
getChildNode(subpath: string): Node | null; | ||
getChildNodes(): [string, Node][]; | ||
getChildFactory(key: string): IFactory<any, any>; | ||
} | ||
export declare function hasMST(value: any): value is IModel; | ||
export declare function hasNode(value: any): value is IModel; | ||
/** | ||
@@ -54,7 +58,8 @@ * Tries to convert a value to a TreeNode. If possible or already done, | ||
*/ | ||
export declare function maybeMST<T, R>(value: T & IModel, asNodeCb: (node: MSTAdminisration, value: T) => R, asPrimitiveCb?: (value: T) => R): R; | ||
export declare function getMST(value: any): MSTAdminisration; | ||
export declare function maybeNode<T, R>(value: T & IModel, asNodeCb: (node: Node, value: T) => R, asPrimitiveCb?: (value: T) => R): R; | ||
export declare function getNode(value: IModel): Node; | ||
export declare function getPath(thing: IModel): string; | ||
export declare function getRelativePath(base: MSTAdminisration, target: MSTAdminisration): string; | ||
export declare function getRelativePath(base: Node, target: Node): string; | ||
export declare function getParent(thing: IModel): IModel; | ||
export declare function getRootNode(node: Node): Node; | ||
export declare function valueToSnapshot(thing: any): any; |
@@ -10,3 +10,3 @@ "use strict"; | ||
var mobx_1 = require("mobx"); | ||
var type_1 = require("./type"); | ||
var types_1 = require("./types"); | ||
var utils_1 = require("../utils"); | ||
@@ -17,6 +17,4 @@ var json_patch_1 = require("./json-patch"); | ||
// TODO: make Node more like a struct, extract the methods to snapshots.js, actions.js etc.. | ||
// TODO: make Node generic? | ||
// TODO: introduce IComplexInstance instead of IModel? | ||
var MSTAdminisration = (function () { | ||
function MSTAdminisration(initialState, type) { | ||
var Node = (function () { | ||
function Node(initialState, environment, factory) { | ||
var _this = this; | ||
@@ -27,7 +25,10 @@ this._parent = null; | ||
this.actionSubscribers = []; | ||
this._isRunningAction = false; // only relevant for root | ||
utils_1.invariant(type instanceof type_1.ComplexType, "Uh oh"); | ||
this._isRunningAction = false; | ||
utils_1.invariant(factory.type instanceof types_1.ComplexType, "Uh oh"); | ||
utils_1.addHiddenFinalProp(initialState, "$treenode", this); | ||
this.type = type; | ||
this.factory = factory; | ||
this.environment = environment; | ||
this.target = initialState; | ||
this.interceptDisposer = mobx_1.intercept(this.target, (function (c) { return _this.type.willChange(_this, c); })); | ||
mobx_1.observe(this.target, function (c) { return _this.type.didChange(_this, c); }); | ||
mobx_1.reaction(function () { return _this.snapshot; }, function (snapshot) { | ||
@@ -38,4 +39,11 @@ _this.snapshotSubscribers.forEach(function (f) { return f(snapshot); }); | ||
} | ||
Object.defineProperty(MSTAdminisration.prototype, "pathParts", { | ||
Object.defineProperty(Node.prototype, "type", { | ||
get: function () { | ||
return this.factory.type; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Node.prototype, "pathParts", { | ||
get: function () { | ||
var _this = this; | ||
@@ -48,3 +56,3 @@ // no parent? you are root! | ||
// optimize: maybe this shouldn't be computed, this is called often and pretty expensive lookup ... | ||
var keys = this._parent.getChildMSTs() | ||
var keys = this._parent.getChildNodes() | ||
.filter(function (entry) { return entry[1] === _this; }); | ||
@@ -60,3 +68,3 @@ if (keys.length > 0) { | ||
}); | ||
Object.defineProperty(MSTAdminisration.prototype, "path", { | ||
Object.defineProperty(Node.prototype, "path", { | ||
/** | ||
@@ -66,3 +74,3 @@ * Returnes (escaped) path representation as string | ||
get: function () { | ||
return "/" + json_patch_1.joinJsonPath(this.pathParts); | ||
return json_patch_1.joinJsonPath(this.pathParts); | ||
}, | ||
@@ -72,3 +80,3 @@ enumerable: true, | ||
}); | ||
Object.defineProperty(MSTAdminisration.prototype, "subpath", { | ||
Object.defineProperty(Node.prototype, "subpath", { | ||
get: function () { | ||
@@ -83,3 +91,3 @@ if (this.isRoot) | ||
}); | ||
Object.defineProperty(MSTAdminisration.prototype, "isRoot", { | ||
Object.defineProperty(Node.prototype, "isRoot", { | ||
get: function () { | ||
@@ -91,3 +99,3 @@ return this._parent === null; | ||
}); | ||
Object.defineProperty(MSTAdminisration.prototype, "parent", { | ||
Object.defineProperty(Node.prototype, "parent", { | ||
get: function () { | ||
@@ -99,15 +107,4 @@ return this._parent; | ||
}); | ||
Object.defineProperty(MSTAdminisration.prototype, "root", { | ||
Object.defineProperty(Node.prototype, "snapshot", { | ||
get: function () { | ||
// future optimization: store root ref in the node and maintain it | ||
var p, r = this; | ||
while (p = r.parent) | ||
r = p; | ||
return r; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(MSTAdminisration.prototype, "snapshot", { | ||
get: function () { | ||
// advantage of using computed for a snapshot is that nicely respects transactions etc. | ||
@@ -119,10 +116,10 @@ return Object.freeze(this.type.serialize(this, this.target)); | ||
}); | ||
MSTAdminisration.prototype.onSnapshot = function (onChange) { | ||
Node.prototype.onSnapshot = function (onChange) { | ||
return utils_1.registerEventHandler(this.snapshotSubscribers, onChange); | ||
}; | ||
MSTAdminisration.prototype.applySnapshot = function (snapshot) { | ||
type_1.typecheck(this.type, snapshot); | ||
Node.prototype.applySnapshot = function (snapshot) { | ||
utils_1.invariant(this.type.is(snapshot), "Snapshot " + JSON.stringify(snapshot) + " is not assignable to type " + this.factory.type.name + ". Expected " + this.factory.type.describe() + " instead."); | ||
return this.type.applySnapshot(this, this.target, snapshot); | ||
}; | ||
MSTAdminisration.prototype.applyPatch = function (patch) { | ||
Node.prototype.applyPatch = function (patch) { | ||
var parts = json_patch_1.splitJsonPath(patch.path); | ||
@@ -132,9 +129,9 @@ var node = this.resolvePath(parts.slice(0, -1)); | ||
}; | ||
MSTAdminisration.prototype.applyPatchLocally = function (subpath, patch) { | ||
Node.prototype.applyPatchLocally = function (subpath, patch) { | ||
this.type.applyPatchLocally(this, this.target, subpath, patch); | ||
}; | ||
MSTAdminisration.prototype.onPatch = function (onPatch) { | ||
Node.prototype.onPatch = function (onPatch) { | ||
return utils_1.registerEventHandler(this.patchSubscribers, onPatch); | ||
}; | ||
MSTAdminisration.prototype.emitPatch = function (patch, source, distance) { | ||
Node.prototype.emitPatch = function (patch, source, distance) { | ||
if (distance === void 0) { distance = 0; } | ||
@@ -154,3 +151,3 @@ if (this.patchSubscribers.length) { | ||
}; | ||
MSTAdminisration.prototype.setParent = function (newParent, subpath) { | ||
Node.prototype.setParent = function (newParent, subpath) { | ||
if (subpath === void 0) { subpath = null; } | ||
@@ -162,3 +159,3 @@ if (this.parent === newParent) | ||
} | ||
if (!this._parent && newParent && newParent.root === this) { | ||
if (!this._parent && newParent && getRootNode(newParent) === this) { | ||
utils_1.invariant(false, "A state tree is not allowed to contain itself. Cannot add root to path '/" + newParent.pathParts.concat(subpath).join("/") + "'"); | ||
@@ -172,7 +169,7 @@ } | ||
}; | ||
MSTAdminisration.prototype.prepareChild = function (subpath, child) { | ||
var childFactory = this.getChildType(subpath); | ||
if (hasMST(child)) { | ||
var node = getMST(child); | ||
if (node.isRoot) { | ||
Node.prototype.prepareChild = function (subpath, child) { | ||
var childFactory = this.getChildFactory(subpath); | ||
if (hasNode(child)) { | ||
var node = getNode(child); | ||
if (node.parent === null) { | ||
// we are adding a node with no parent (first insert in the tree) | ||
@@ -182,7 +179,7 @@ node.setParent(this, subpath); | ||
} | ||
return utils_1.fail("Cannot add an object to a state tree if it is already part of the same or another state tree. Tried to assign an object to '" + this.path + "/" + subpath + "', but it lives already at '" + getPath(child) + "'"); | ||
return utils_1.fail("A node cannot exists twice in the state tree. Failed to add object to path '" + this.path + '/' + subpath + "', it exists already at '" + getPath(child) + "'"); | ||
} | ||
var existingNode = this.getChildMST(subpath); | ||
var newInstance = childFactory.create(child); | ||
if (existingNode && existingNode.type === newInstance.factory) { | ||
var existingNode = this.getChildNode(subpath); | ||
var newInstance = childFactory(child); | ||
if (existingNode && existingNode.factory === newInstance.factory) { | ||
// recycle instance.. | ||
@@ -195,4 +192,4 @@ existingNode.applySnapshot(child); | ||
existingNode.setParent(null); // TODO: or delete / remove / whatever is a more explicit clean up | ||
if (hasMST(newInstance)) { | ||
var node = getMST(newInstance); | ||
if (hasNode(newInstance)) { | ||
var node = getNode(newInstance); | ||
node.setParent(this, subpath); | ||
@@ -203,3 +200,3 @@ } | ||
}; | ||
MSTAdminisration.prototype.detach = function () { | ||
Node.prototype.detach = function () { | ||
// TODO: change to return a clone | ||
@@ -216,13 +213,11 @@ // TODO: detach / remove marks as End of life!... | ||
}; | ||
MSTAdminisration.prototype.resolve = function (path, failIfResolveFails) { | ||
Node.prototype.resolve = function (path, failIfResolveFails) { | ||
if (failIfResolveFails === void 0) { failIfResolveFails = true; } | ||
return this.resolvePath(json_patch_1.splitJsonPath(path), failIfResolveFails); | ||
}; | ||
MSTAdminisration.prototype.resolvePath = function (pathParts, failIfResolveFails) { | ||
Node.prototype.resolvePath = function (pathParts, failIfResolveFails) { | ||
if (failIfResolveFails === void 0) { failIfResolveFails = true; } | ||
var current = this; | ||
for (var i = 0; i < pathParts.length; i++) { | ||
if (pathParts[i] === "") | ||
current = current.root; | ||
else if (pathParts[i] === "..") | ||
if (pathParts[i] === "..") | ||
current = current.parent; | ||
@@ -232,3 +227,3 @@ else if (pathParts[i] === ".") | ||
else | ||
current = current.getChildMST(pathParts[i]); | ||
current = current.getChildNode(pathParts[i]); | ||
if (current === null) { | ||
@@ -243,3 +238,3 @@ if (failIfResolveFails) | ||
}; | ||
MSTAdminisration.prototype.isRunningAction = function () { | ||
Node.prototype.isRunningAction = function () { | ||
if (this._isRunningAction) | ||
@@ -251,3 +246,3 @@ return true; | ||
}; | ||
MSTAdminisration.prototype.applyAction = function (action) { | ||
Node.prototype.applyAction = function (action) { | ||
var targetNode = this.resolve(action.path || ""); | ||
@@ -259,3 +254,3 @@ if (targetNode) | ||
}; | ||
MSTAdminisration.prototype.emitAction = function (instance, action, next) { | ||
Node.prototype.emitAction = function (instance, action, next) { | ||
var _this = this; | ||
@@ -270,3 +265,3 @@ var idx = -1; | ||
if (idx < _this.actionSubscribers.length) { | ||
return _this.actionSubscribers[idx](correctedAction, n); | ||
_this.actionSubscribers[idx](correctedAction, n); | ||
} | ||
@@ -276,47 +271,54 @@ else { | ||
if (parent_1) | ||
return parent_1.emitAction(instance, action, next); | ||
parent_1.emitAction(instance, action, next); | ||
else | ||
return next(); | ||
next(); | ||
} | ||
}; | ||
return n(); | ||
n(); | ||
}; | ||
MSTAdminisration.prototype.onAction = function (listener) { | ||
Node.prototype.onAction = function (listener) { | ||
return utils_1.registerEventHandler(this.actionSubscribers, listener); | ||
}; | ||
MSTAdminisration.prototype.getChildMST = function (subpath) { | ||
return this.type.getChildMST(this, this.target, subpath); | ||
Node.prototype.getFromEnvironment = function (key) { | ||
if (this.environment && this.environment.hasOwnProperty(key)) | ||
return this.environment[key]; | ||
if (this.isRoot) | ||
return utils_1.fail("Undefined environment variable '" + key + "'"); | ||
return this.parent.getFromEnvironment(key); | ||
}; | ||
MSTAdminisration.prototype.getChildMSTs = function () { | ||
return this.type.getChildMSTs(this, this.target); | ||
Node.prototype.getChildNode = function (subpath) { | ||
return this.type.getChildNode(this, this.target, subpath); | ||
}; | ||
MSTAdminisration.prototype.getChildType = function (key) { | ||
return this.type.getChildType(key); | ||
Node.prototype.getChildNodes = function () { | ||
return this.type.getChildNodes(this, this.target); | ||
}; | ||
return MSTAdminisration; | ||
Node.prototype.getChildFactory = function (key) { | ||
return this.type.getChildFactory(key); | ||
}; | ||
return Node; | ||
}()); | ||
__decorate([ | ||
mobx_1.observable | ||
], MSTAdminisration.prototype, "_parent", void 0); | ||
], Node.prototype, "_parent", void 0); | ||
__decorate([ | ||
mobx_1.computed | ||
], MSTAdminisration.prototype, "pathParts", null); | ||
], Node.prototype, "pathParts", null); | ||
__decorate([ | ||
mobx_1.computed | ||
], MSTAdminisration.prototype, "path", null); | ||
], Node.prototype, "path", null); | ||
__decorate([ | ||
mobx_1.computed | ||
], MSTAdminisration.prototype, "subpath", null); | ||
], Node.prototype, "subpath", null); | ||
__decorate([ | ||
mobx_1.computed | ||
], MSTAdminisration.prototype, "snapshot", null); | ||
], Node.prototype, "snapshot", null); | ||
__decorate([ | ||
mobx_1.action | ||
], MSTAdminisration.prototype, "applyPatch", null); | ||
exports.MSTAdminisration = MSTAdminisration; | ||
], Node.prototype, "applyPatch", null); | ||
exports.Node = Node; | ||
// TODO: duplicate with isModel | ||
function hasMST(value) { | ||
function hasNode(value) { | ||
return value && value.$treenode; | ||
} | ||
exports.hasMST = hasMST; | ||
exports.hasNode = hasNode; | ||
/** | ||
@@ -327,6 +329,6 @@ * Tries to convert a value to a TreeNode. If possible or already done, | ||
*/ | ||
function maybeMST(value, asNodeCb, asPrimitiveCb) { | ||
function maybeNode(value, asNodeCb, asPrimitiveCb) { | ||
// Optimization: maybeNode might be quite inefficient runtime wise, might be factored out at expensive places | ||
if (utils_1.isMutable(value)) { | ||
var n = getMST(value); | ||
var n = getNode(value); | ||
return asNodeCb(n, n.target); | ||
@@ -341,5 +343,5 @@ } | ||
} | ||
exports.maybeMST = maybeMST; | ||
function getMST(value) { | ||
if (hasMST(value)) | ||
exports.maybeNode = maybeNode; | ||
function getNode(value) { | ||
if (hasNode(value)) | ||
return value.$treenode; | ||
@@ -349,5 +351,5 @@ else | ||
} | ||
exports.getMST = getMST; | ||
exports.getNode = getNode; | ||
function getPath(thing) { | ||
return getMST(thing).path; | ||
return getNode(thing).path; | ||
} | ||
@@ -357,20 +359,18 @@ exports.getPath = getPath; | ||
// PRE condition target is (a child of) base! | ||
utils_1.invariant(base.root === target.root, "Cannot calculate relative path: objects '" + base + "' and '" + target + "' are not part of the same object tree"); | ||
var baseParts = base.pathParts; | ||
var targetParts = target.pathParts; | ||
var common = 0; | ||
for (; common < baseParts.length; common++) { | ||
if (baseParts[common] !== targetParts[common]) | ||
break; | ||
} | ||
return json_patch_1.joinJsonPath(baseParts | ||
.slice(common).map(function (_) { return ".."; }) | ||
.concat(targetParts.slice(common))); | ||
utils_1.invariant(target.path.length >= base.path.length, 'getRelativePath received a target path "' + target.path + '" shorter than the base path "' + base.path + '".'); | ||
return target.path.substr(base.path.length); | ||
} | ||
exports.getRelativePath = getRelativePath; | ||
function getParent(thing) { | ||
var node = getMST(thing); | ||
var node = getNode(thing); | ||
return node.parent ? node.parent.target : null; | ||
} | ||
exports.getParent = getParent; | ||
function getRootNode(node) { | ||
var p, r = node; | ||
while (p = r.parent) | ||
r = p; | ||
return r; | ||
} | ||
exports.getRootNode = getRootNode; | ||
function valueToSnapshot(thing) { | ||
@@ -383,9 +383,9 @@ if (thing instanceof Date) { | ||
} | ||
if (hasMST(thing)) | ||
return getMST(thing).snapshot; | ||
if (hasNode(thing)) | ||
return getNode(thing).snapshot; | ||
if (utils_1.isSerializable(thing)) | ||
return thing; | ||
utils_1.fail("Unable to convert value to snapshot."); | ||
utils_1.fail('Unable to convert value to snapshot.'); | ||
} | ||
exports.valueToSnapshot = valueToSnapshot; | ||
//# sourceMappingURL=node.js.map |
@@ -8,2 +8,4 @@ export declare function isType(value: any): value is IType<any, any>; | ||
describe(): string; | ||
Type: T; | ||
SnapshotType: S; | ||
} | ||
@@ -17,3 +19,5 @@ export declare abstract class Type<S, T> implements IType<S, T> { | ||
abstract describe(): string; | ||
readonly Type: T; | ||
readonly SnapshotType: S; | ||
} | ||
export declare function typecheck(type: IType<any, any>, snapshot: any): void; |
@@ -13,2 +13,16 @@ "use strict"; | ||
} | ||
Object.defineProperty(Type.prototype, "Type", { | ||
get: function () { | ||
return utils_1.fail("Factory.Type should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.Type`"); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Type.prototype, "SnapshotType", { | ||
get: function () { | ||
return utils_1.fail("Factory.SnapshotType should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.SnapshotType`"); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return Type; | ||
@@ -15,0 +29,0 @@ }()); |
@@ -0,39 +1,35 @@ | ||
import { Node } from "./node"; | ||
import { IJsonPatch } from "../core/json-patch"; | ||
export declare type IModel = { | ||
$treenode: any; | ||
} & Object; | ||
export declare function isType(value: any): value is IType<any, any>; | ||
export declare function getType(object: IModel): IType<any, any>; | ||
export declare function getChildType(object: IModel, child: string): IType<any, any>; | ||
export declare function isModel(model: any): model is IModel; | ||
export interface IType<S, T> { | ||
import { IFactory, IModel } from "./factories"; | ||
export interface IType { | ||
name: string; | ||
is(thing: any): thing is S | T; | ||
create(snapshot?: S): T; | ||
isType: boolean; | ||
is(thing: IModel | any): boolean; | ||
create(snapshot: any, environment?: any): any; | ||
factory: IFactory<any, any>; | ||
describe(): string; | ||
} | ||
export declare abstract class Type<S, T> implements IType<S, T> { | ||
export declare type ITypeChecker = (value: IModel | any) => boolean; | ||
export declare abstract class Type implements IType { | ||
name: string; | ||
isType: boolean; | ||
factory: IFactory<any, any>; | ||
constructor(name: string); | ||
abstract create(snapshot: any): any; | ||
abstract is(thing: any): thing is S | T; | ||
abstract create(snapshot: any, environment?: any): any; | ||
abstract is(thing: any): boolean; | ||
abstract describe(): string; | ||
protected initializeFactory(): IFactory<any, any>; | ||
} | ||
export declare abstract class ComplexType<S, T> extends Type<S, T> { | ||
create(snapshot?: any): any; | ||
export declare abstract class ComplexType extends Type { | ||
create(snapshot: any, environment?: any): any; | ||
abstract createNewInstance(): any; | ||
abstract finalizeNewInstance(target: any, snapshot: any): void; | ||
abstract applySnapshot(node: Node, target: any, snapshot: any): void; | ||
abstract getDefaultSnapshot(): any; | ||
abstract finalizeNewInstance(target: any): any; | ||
abstract applySnapshot(node: Node, target: any, snapshot: any): any; | ||
abstract getChildNodes(node: Node, target: any): [string, Node][]; | ||
abstract getChildNode(node: Node, target: any, key: string): Node | null; | ||
abstract getChildNode(node: Node, target: any, key: any): Node | null; | ||
abstract willChange(node: Node, change: any): Object | null; | ||
abstract didChange(node: Node, change: any): void; | ||
abstract serialize(node: Node, target: any): any; | ||
abstract applyPatchLocally(node: Node, target: any, subpath: string, patch: IJsonPatch): void; | ||
abstract getChildType(key: string): IType<any, any>; | ||
abstract getChildFactory(key: string): IFactory<any, any>; | ||
abstract isValidSnapshot(snapshot: any): boolean; | ||
is(value: any): value is S | T; | ||
is(value: IModel | any): boolean; | ||
} | ||
export declare function typecheck(type: IType<any, any>, snapshot: any): void; | ||
import { Node } from "./node"; |
@@ -14,26 +14,17 @@ "use strict"; | ||
var mobx_1 = require("mobx"); | ||
var utils_1 = require("../utils"); | ||
function isType(value) { | ||
return typeof value === "object" && value && value.isType === true; | ||
} | ||
exports.isType = isType; | ||
function getType(object) { | ||
return node_1.getNode(object).type; | ||
} | ||
exports.getType = getType; | ||
function getChildType(object, child) { | ||
return node_1.getNode(object).getChildType(child); | ||
} | ||
exports.getChildType = getChildType; | ||
// TODO: ambigous function name, remove | ||
function isModel(model) { | ||
return node_1.hasNode(model); | ||
} | ||
exports.isModel = isModel; | ||
var node_1 = require("./node"); | ||
var Type = (function () { | ||
function Type(name) { | ||
this.isType = true; | ||
this.name = name; | ||
this.create = mobx_1.action(this.name, this.create); | ||
this.factory = this.initializeFactory(); | ||
} | ||
Type.prototype.initializeFactory = function () { | ||
var _this = this; | ||
var factory = mobx_1.action(this.name, this.create.bind(this)); | ||
factory.type = this; | ||
factory.isFactory = true; | ||
factory.factoryName = this.name; | ||
factory.is = function (value) { return _this.is(value); }; | ||
return factory; | ||
}; | ||
return Type; | ||
@@ -47,9 +38,8 @@ }()); | ||
} | ||
ComplexType.prototype.create = function (snapshot) { | ||
if (snapshot === void 0) { snapshot = this.getDefaultSnapshot(); } | ||
typecheck(this, snapshot); | ||
ComplexType.prototype.create = function (snapshot, environment) { | ||
var instance = this.createNewInstance(); | ||
// tslint:disable-next-line:no_unused-variable | ||
var node = new node_1.Node(instance, this); | ||
this.finalizeNewInstance(instance, snapshot); | ||
var node = new node_1.Node(instance, environment, this.factory); | ||
this.finalizeNewInstance(instance); | ||
if (arguments.length > 0) | ||
node.applySnapshot(snapshot); | ||
Object.seal(instance); | ||
@@ -68,8 +58,2 @@ return instance; | ||
exports.ComplexType = ComplexType; | ||
function typecheck(type, snapshot) { | ||
if (!type.is(snapshot)) | ||
utils_1.fail("Snapshot " + JSON.stringify(snapshot) + " is not assignable to type " + type.name + ". Expected " + type.describe() + " instead."); | ||
} | ||
exports.typecheck = typecheck; | ||
var node_1 = require("./node"); | ||
//# sourceMappingURL=types.js.map |
@@ -0,0 +0,0 @@ import "./core/type"; |
@@ -0,0 +0,0 @@ "use strict"; |
export declare function connectReduxDevtools(model: any): void; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IActionCall } from "../core"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IJsonPatch } from "./core"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IObservableArray, IArrayWillChange, IArrayWillSplice, IArrayChange, IArraySplice } from "mobx"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -13,1 +13,2 @@ import { IType, Type } from "../core"; | ||
export declare const DatePrimitive: IType<Date, Date>; | ||
export declare function getPrimitiveFactoryFromValue(value: any): IType<any, any>; |
@@ -44,2 +44,17 @@ "use strict"; | ||
exports.DatePrimitive = new CoreType("Date", function (v) { return v instanceof Date; }); | ||
function getPrimitiveFactoryFromValue(value) { | ||
switch (typeof value) { | ||
case "string": | ||
return exports.string; | ||
case "number": | ||
return exports.number; | ||
case "boolean": | ||
return exports.boolean; | ||
case "object": | ||
if (value instanceof Date) | ||
return exports.DatePrimitive; | ||
} | ||
return utils_1.fail("Cannot determine primtive type from value " + value); | ||
} | ||
exports.getPrimitiveFactoryFromValue = getPrimitiveFactoryFromValue; | ||
//# sourceMappingURL=core-types.js.map |
@@ -0,0 +0,0 @@ import { IType, Type } from "../core"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ export interface IIdentifierDescriptor { |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IObservableArray, IAction } from "mobx"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IType } from "../core/type"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { ObservableMap, IMapChange, IMapWillChange } from "mobx"; |
@@ -0,0 +0,0 @@ "use strict"; |
import { IType } from "../core/type"; | ||
export declare function createMaybeFactory<S, T>(type: IType<S, T>): IType<S | null | undefined, T | null>; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IObjectChange, IObjectWillChange, IAction } from "mobx"; |
@@ -24,3 +24,2 @@ "use strict"; | ||
var reference_1 = require("./reference"); | ||
var primitive_1 = require("./primitive"); | ||
var identifier_1 = require("./identifier"); | ||
@@ -34,2 +33,3 @@ var core_2 = require("../core"); | ||
var top_level_api_1 = require("../top-level-api"); | ||
var core_types_1 = require("./core-types"); | ||
var ObjectType = (function (_super) { | ||
@@ -86,3 +86,6 @@ __extends(ObjectType, _super); | ||
var value = descriptor.value; | ||
if (identifier_1.isIdentifierFactory(value)) { | ||
if (value === null || undefined) { | ||
utils_1.fail("The default value of an attribute cannot be null or undefined as the type cannot be inferred. Did you mean `types.maybe(someType)`?"); | ||
} | ||
else if (identifier_1.isIdentifierFactory(value)) { | ||
utils_1.invariant(!this.identifierAttribute, "Cannot define property '" + key + "' as object identifier, property '" + this.identifierAttribute + "' is already defined as identifier property"); | ||
@@ -93,4 +96,4 @@ this.identifierAttribute = key; | ||
else if (utils_1.isPrimitive(value)) { | ||
// TODO: detect exact primitiveFactory! | ||
this.props[key] = new value_property_1.ValueProperty(key, with_default_1.createDefaultValueFactory(primitive_1.primitiveFactory, value)); | ||
var baseType = core_types_1.getPrimitiveFactoryFromValue(value); | ||
this.props[key] = new value_property_1.ValueProperty(key, with_default_1.createDefaultValueFactory(baseType, value)); | ||
} | ||
@@ -153,5 +156,2 @@ else if (core_2.isType(value)) { | ||
return false; | ||
for (var key in snapshot) | ||
if (!(key in this.props)) | ||
return false; | ||
for (var key in this.props) | ||
@@ -158,0 +158,0 @@ if (!this.props[key].isValidSnapshot(snapshot)) |
@@ -0,0 +0,0 @@ import { Type } from "../core/type"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { Property } from "./property"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { Property } from "./property"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IObjectWillChange } from "mobx"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IObjectChange, IObjectWillChange } from "mobx"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { Property } from "./property"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IObjectWillChange, IObjectChange } from "mobx"; |
@@ -0,0 +0,0 @@ "use strict"; |
import { IType } from "../core/type"; | ||
export declare type IRecursiveDef<S, T> = (type: IType<S, T>) => IType<any, any>; | ||
export declare function recursive<S, T>(name: string, def: IRecursiveDef<S, T>): IType<any, any>; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IType } from "../core"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IType } from "../core"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IType } from "../core/type"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IType } from "../core/type"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ export declare type IDisposer = () => void; |
@@ -0,0 +0,0 @@ "use strict"; |
{ | ||
"name": "mobx-state-tree", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Opinionated, transactional, MobX powered state container", | ||
@@ -17,3 +17,3 @@ "main": "lib/index.js", | ||
"lint": "tslint -c tslint.json 'src/**/*.ts'", | ||
"clean": "rm lib && rm test-lib" | ||
"clean": "rm -rf lib && rm -rf test-lib" | ||
}, | ||
@@ -71,2 +71,2 @@ "repository": { | ||
} | ||
} | ||
} |
@@ -267,3 +267,3 @@ # mobx-state-tree | ||
[lib/core/complex-type.js:17-40](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/core/complex-type.js#L17-L40 "Source code on GitHub") | ||
[lib/core/complex-type.js:17-40](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/core/complex-type.js#L17-L40 "Source code on GitHub") | ||
@@ -274,3 +274,3 @@ A complex type produces a MST node (Node in the state tree) | ||
[lib/core/mst-node.js:32-44](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/core/mst-node.js#L32-L44 "Source code on GitHub") | ||
[lib/core/mst-node.js:32-44](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/core/mst-node.js#L32-L44 "Source code on GitHub") | ||
@@ -289,3 +289,3 @@ Tries to convert a value to a TreeNode. If possible or already done, | ||
[lib/core/mst-node-administration.js:58-60](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/core/mst-node-administration.js#L58-L60 "Source code on GitHub") | ||
[lib/core/mst-node-administration.js:58-60](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/core/mst-node-administration.js#L58-L60 "Source code on GitHub") | ||
@@ -296,3 +296,3 @@ Returnes (escaped) path representation as string | ||
[lib/core/json-patch.js:8-10](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/core/json-patch.js#L8-L10 "Source code on GitHub") | ||
[lib/core/json-patch.js:8-10](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/core/json-patch.js#L8-L10 "Source code on GitHub") | ||
@@ -308,3 +308,3 @@ escape slashes and backslashes | ||
[lib/core/json-patch.js:15-17](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/core/json-patch.js#L15-L17 "Source code on GitHub") | ||
[lib/core/json-patch.js:15-17](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/core/json-patch.js#L15-L17 "Source code on GitHub") | ||
@@ -319,3 +319,3 @@ unescape slashes and backslashes | ||
[lib/types/index.js:25-28](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/types/index.js#L25-L28 "Source code on GitHub") | ||
[lib/types/index.js:25-28](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/types/index.js#L25-L28 "Source code on GitHub") | ||
@@ -328,3 +328,3 @@ **Parameters** | ||
[lib/types/index.js:37-40](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/types/index.js#L37-L40 "Source code on GitHub") | ||
[lib/types/index.js:37-40](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/types/index.js#L37-L40 "Source code on GitHub") | ||
@@ -337,3 +337,3 @@ **Parameters** | ||
[lib/types/object.js:41-41](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/types/object.js#L41-L41 "Source code on GitHub") | ||
[lib/types/object.js:41-41](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/types/object.js#L41-L41 "Source code on GitHub") | ||
@@ -344,3 +344,3 @@ Parsed description of all properties | ||
[lib/top-level-api.js:47-49](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L47-L49 "Source code on GitHub") | ||
[lib/top-level-api.js:47-49](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L47-L49 "Source code on GitHub") | ||
@@ -388,3 +388,3 @@ Registers middleware on a model instance that is invoked whenever one of it's actions is called, or an action on one of it's children. | ||
[lib/top-level-api.js:61-63](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L61-L63 "Source code on GitHub") | ||
[lib/top-level-api.js:61-63](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L61-L63 "Source code on GitHub") | ||
@@ -404,3 +404,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/top-level-api.js:74-76](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L74-L76 "Source code on GitHub") | ||
[lib/top-level-api.js:74-76](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L74-L76 "Source code on GitHub") | ||
@@ -419,3 +419,3 @@ Registeres a function that is invoked whenever a new snapshot for the given model instance is available. | ||
[lib/top-level-api.js:86-88](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L86-L88 "Source code on GitHub") | ||
[lib/top-level-api.js:86-88](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L86-L88 "Source code on GitHub") | ||
@@ -431,3 +431,3 @@ Applies a JSON-patch to the given model instance or bails out if the patch couldn't be applied | ||
[lib/top-level-api.js:97-102](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L97-L102 "Source code on GitHub") | ||
[lib/top-level-api.js:97-102](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L97-L102 "Source code on GitHub") | ||
@@ -443,3 +443,3 @@ Applies a number of JSON patches in a single MobX transaction | ||
[lib/top-level-api.js:128-130](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L128-L130 "Source code on GitHub") | ||
[lib/top-level-api.js:128-130](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L128-L130 "Source code on GitHub") | ||
@@ -457,3 +457,3 @@ Dispatches an Action on a model instance. All middlewares will be triggered. | ||
[lib/top-level-api.js:142-147](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L142-L147 "Source code on GitHub") | ||
[lib/top-level-api.js:142-147](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L142-L147 "Source code on GitHub") | ||
@@ -472,3 +472,3 @@ Applies a series of actions in a single MobX transaction. | ||
[lib/top-level-api.js:172-174](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L172-L174 "Source code on GitHub") | ||
[lib/top-level-api.js:172-174](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L172-L174 "Source code on GitHub") | ||
@@ -484,3 +484,3 @@ Applies a snapshot to a given model instances. Patch and snapshot listeners will be invoked as usual. | ||
[lib/top-level-api.js:184-186](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L184-L186 "Source code on GitHub") | ||
[lib/top-level-api.js:184-186](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L184-L186 "Source code on GitHub") | ||
@@ -498,3 +498,3 @@ Calculates a snapshot from the given model instance. The snapshot will always reflect the latest state but use | ||
[lib/top-level-api.js:196-199](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L196-L199 "Source code on GitHub") | ||
[lib/top-level-api.js:196-199](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L196-L199 "Source code on GitHub") | ||
@@ -512,3 +512,3 @@ Given a model instance, returns `true` if the object has a parent, that is, is part of another object, map or array | ||
[lib/top-level-api.js:221-228](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L221-L228 "Source code on GitHub") | ||
[lib/top-level-api.js:221-228](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L221-L228 "Source code on GitHub") | ||
@@ -527,3 +527,3 @@ Returns the immediate parent of this object, or null. Parent can be either an object, map or array | ||
[lib/top-level-api.js:221-228](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L221-L228 "Source code on GitHub") | ||
[lib/top-level-api.js:221-228](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L221-L228 "Source code on GitHub") | ||
@@ -543,3 +543,3 @@ TODO: | ||
[lib/top-level-api.js:250-252](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L250-L252 "Source code on GitHub") | ||
[lib/top-level-api.js:250-252](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L250-L252 "Source code on GitHub") | ||
@@ -557,3 +557,3 @@ TODO: | ||
[lib/top-level-api.js:250-252](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L250-L252 "Source code on GitHub") | ||
[lib/top-level-api.js:250-252](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L250-L252 "Source code on GitHub") | ||
@@ -570,3 +570,3 @@ Given an object in a model tree, returns the root object of that tree | ||
[lib/top-level-api.js:261-263](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L261-L263 "Source code on GitHub") | ||
[lib/top-level-api.js:261-263](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L261-L263 "Source code on GitHub") | ||
@@ -583,3 +583,3 @@ Returns the path of the given object in the model tree | ||
[lib/top-level-api.js:272-274](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L272-L274 "Source code on GitHub") | ||
[lib/top-level-api.js:272-274](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L272-L274 "Source code on GitHub") | ||
@@ -596,3 +596,3 @@ Returns the path of the given object as unescaped string array | ||
[lib/top-level-api.js:283-285](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L283-L285 "Source code on GitHub") | ||
[lib/top-level-api.js:283-285](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L283-L285 "Source code on GitHub") | ||
@@ -609,3 +609,3 @@ Returns true if the given object is the root of a model tree | ||
[lib/top-level-api.js:295-298](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L295-L298 "Source code on GitHub") | ||
[lib/top-level-api.js:295-298](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L295-L298 "Source code on GitHub") | ||
@@ -623,3 +623,3 @@ Resolves a path relatively to a given object. | ||
[lib/top-level-api.js:308-313](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L308-L313 "Source code on GitHub") | ||
[lib/top-level-api.js:308-313](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L308-L313 "Source code on GitHub") | ||
@@ -635,3 +635,3 @@ **Parameters** | ||
[lib/top-level-api.js:323-326](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L323-L326 "Source code on GitHub") | ||
[lib/top-level-api.js:323-326](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L323-L326 "Source code on GitHub") | ||
@@ -646,3 +646,3 @@ **Parameters** | ||
[lib/top-level-api.js:338-340](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L338-L340 "Source code on GitHub") | ||
[lib/top-level-api.js:338-340](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L338-L340 "Source code on GitHub") | ||
@@ -657,3 +657,3 @@ Internal function, use with care! | ||
[lib/top-level-api.js:338-340](https://github.com/mweststrate/mobx-state-tree/blob/12bbe176ee532e8d6ebd9d898bdb41fb64e9100e/lib/top-level-api.js#L338-L340 "Source code on GitHub") | ||
[lib/top-level-api.js:338-340](https://github.com/mobxjs/mobx-state-tree/blob/10d440851923095b35b067194cc1c5eeb81d8b19/lib/top-level-api.js#L338-L340 "Source code on GitHub") | ||
@@ -706,1 +706,17 @@ **Parameters** | ||
- (?) mobx-state-tree is a valid redux store, providing the same api (TODO) | ||
## TypeScript & MST | ||
When using models, you write interface along with it's property types that will be used to perform type checks at runtime. | ||
What about compile time? You can use TypeScript interfaces indeed to perform those checks, but that would require writing again all the properties and their actions! | ||
Good news? You don't need to write it twice! Using the `typeof` operator of TypeScript over the `.Type` property of a MST Type, will result in a valid TypeScript Type! | ||
```typescript | ||
const Todo = types.model({ | ||
title: types.string, | ||
setTitle(v: string) { | ||
this.title = v | ||
} | ||
}) | ||
type ITodo = typeof Todo.Type // => ITodo is now a valid TypeScript type with { title: string; setTitle: (v: string) => void } | ||
``` |
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 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
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
692
397101
113
4230