state-tree
Advanced tools
Comparing version 0.2.4 to 0.2.5
12
index.js
@@ -1,5 +0,11 @@ | ||
// keep backward compatibility | ||
// class factories exported as CommonJS module "default" | ||
var StateTree = require('./lib/stateTree').default | ||
StateTree.computed = require('./lib/computed').default | ||
var Computed = require('./lib/computed').default | ||
module.exports = StateTree | ||
module.exports = function (initialState) { | ||
return new StateTree(initialState) | ||
} | ||
module.exports.computed = function (deps, cb) { | ||
return new Computed(deps, cb) | ||
} |
declare class Computed { | ||
static getByPath: any; | ||
private _deps; | ||
private _cb; | ||
static getByPath: any; | ||
static hasChanged: any; | ||
private _value; | ||
private _computedHasChanged; | ||
constructor(_deps: any, _cb: any); | ||
getDepsMap(): any; | ||
get(passedState: any): any; | ||
hasChanged(changes: any): boolean; | ||
} | ||
export default Computed; |
@@ -5,6 +5,6 @@ "use strict"; | ||
function Computed(_deps, _cb) { | ||
if (!(this instanceof Computed)) | ||
return new Computed(_deps, _cb); | ||
this._deps = _deps; | ||
this._cb = _cb; | ||
this._computedHasChanged = true; | ||
this._value = null; | ||
} | ||
@@ -16,12 +16,34 @@ Computed.prototype.getDepsMap = function () { | ||
var _this = this; | ||
return this._cb(Object.keys(this._deps).reduce(function (props, key) { | ||
if (typeof _this._deps[key] === 'string') { | ||
var path = _this._deps[key].split('.'); | ||
props[key] = Computed.getByPath(path, passedState); | ||
if (this._computedHasChanged) { | ||
this._computedHasChanged = false; | ||
this._value = this._cb(Object.keys(this._deps).reduce(function (props, key) { | ||
if (typeof _this._deps[key] === 'string') { | ||
var path = _this._deps[key].split('.'); | ||
props[key] = Computed.getByPath(path, passedState); | ||
} | ||
else { | ||
props[key] = _this._deps[key].get(passedState); | ||
} | ||
return props; | ||
}, {})); | ||
return this._value; | ||
} | ||
else { | ||
return this._value; | ||
} | ||
}; | ||
// Can optimize by remembering the changes in case multiple | ||
// components checks the computed, but very unlikely | ||
Computed.prototype.hasChanged = function (changes) { | ||
if (this._computedHasChanged) { | ||
return true; | ||
} | ||
for (var key in this._deps) { | ||
if ((typeof this._deps[key] === 'string' && Computed.hasChanged(this._deps[key], changes)) || | ||
(typeof this._deps[key] !== 'string' && this._deps[key].hasChanged(changes))) { | ||
this._computedHasChanged = true; | ||
return true; | ||
} | ||
else { | ||
props[key] = _this._deps[key].get(passedState); | ||
} | ||
return props; | ||
}, {})); | ||
} | ||
return false; | ||
}; | ||
@@ -31,4 +53,5 @@ return Computed; | ||
Computed.getByPath = utils_1.getByPath; | ||
Computed.hasChanged = utils_1.hasChanged; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = Computed; | ||
//# sourceMappingURL=computed.js.map |
@@ -0,9 +1,11 @@ | ||
import { setReferences, cleanReferences } from './references'; | ||
import { getByPath } from './utils'; | ||
export declare type Callback = (changes: any) => void; | ||
declare class StateTree { | ||
static setReferences: typeof setReferences; | ||
static cleanReferences: typeof cleanReferences; | ||
static getByPath: typeof getByPath; | ||
private _state; | ||
private _subscribers; | ||
private _changes; | ||
static setReferences: any; | ||
static cleanReferences: any; | ||
static getByPath: any; | ||
constructor(initialState: any); | ||
@@ -10,0 +12,0 @@ private _updateChanges(host, key); |
@@ -6,7 +6,5 @@ "use strict"; | ||
function StateTree(initialState) { | ||
if (!(this instanceof StateTree)) | ||
return new StateTree(initialState); | ||
this._subscribers = []; | ||
this._changes = {}; | ||
this._state = StateTree.setReferences(initialState, []); | ||
this._changes = {}; | ||
this._subscribers = []; | ||
} | ||
@@ -164,9 +162,9 @@ StateTree.prototype._updateChanges = function (host, key) { | ||
}; | ||
StateTree.setReferences = references_1.setReferences; | ||
StateTree.cleanReferences = references_1.cleanReferences; | ||
StateTree.getByPath = utils_1.getByPath; | ||
return StateTree; | ||
}()); | ||
StateTree.setReferences = references_1.setReferences; | ||
StateTree.cleanReferences = references_1.cleanReferences; | ||
StateTree.getByPath = utils_1.getByPath; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = StateTree; | ||
//# sourceMappingURL=stateTree.js.map |
@@ -5,1 +5,2 @@ export declare function getByPath(path: any, state: any, forcePath?: any): any; | ||
}; | ||
export declare function hasChanged(path: any, changes: any): any; |
@@ -61,2 +61,8 @@ "use strict"; | ||
exports.deepmerge = deepmerge; | ||
function hasChanged(path, changes) { | ||
return path.split('.').reduce(function (changes, key) { | ||
return changes ? changes[key] : false; | ||
}, changes); | ||
} | ||
exports.hasChanged = hasChanged; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "state-tree", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "A state tree that handles reference updates and lets you flush a description of changes", | ||
"main": "index.js", | ||
"typings": "index.d.ts", | ||
"directories": { | ||
@@ -7,0 +8,0 @@ "test": "tests" |
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
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
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
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
502
38138
18
2
0