Comparing version 1.1.4 to 1.2.0
@@ -23,3 +23,3 @@ "use strict"; | ||
var slots2 = new _slots2["default"](_dataRules2["default"], {}); | ||
slots2.onChange(cb2); | ||
slots2.onDidSet(cb2); | ||
it("should call callback if state has changed", function () { | ||
@@ -32,5 +32,5 @@ slots2.set("route", { name: "page", params: { id: 1 } }).commit(); | ||
var slots4 = new _slots2["default"](_dataRules2["default"], slots2.getState().toJS()); | ||
slots4.onChange(cb4); | ||
slots4.onDidSet(cb4); | ||
slots4.set("route", { name: "page", params: { id: 1 } }).commit(); | ||
expect(cb4).not.toHaveBeenCalled(); | ||
expect(cb4).toHaveBeenCalled(); | ||
}); | ||
@@ -37,0 +37,0 @@ |
@@ -96,3 +96,3 @@ "use strict"; | ||
_this.ctx.promises.splice(_this.ctx.promises.indexOf(branch), 1); | ||
_this.ctx.commit(); | ||
_this.ctx.slots._checkPromises(_this); | ||
}); | ||
@@ -115,3 +115,2 @@ } else { | ||
this.state = result; | ||
this.ctx.slots._fireOnSet(this); | ||
return this; | ||
@@ -118,0 +117,0 @@ } |
@@ -63,5 +63,10 @@ "use strict"; | ||
this.path = path; | ||
var prevState = this.state; | ||
var branch = new _branch2["default"](this.rules, this.state, this); | ||
this.branches.push(branch); | ||
this.state = branch.set(path, value).getState(); | ||
this.slots._fire("beforeSet", prevState, this); | ||
var newState = branch.set(path, value).getState(); | ||
this.slots._fire("willSet", newState, this); //TODO: return false == do nothing | ||
this.state = newState; | ||
this.slots._fire("didSet", prevState, this); | ||
this.branches.splice(this.branches.indexOf(branch), 1); | ||
@@ -68,0 +73,0 @@ return this; |
168
lib/slots.js
@@ -40,8 +40,3 @@ "use strict"; | ||
this.contexts = []; | ||
this.promises = []; | ||
this.onChangeListeners = []; | ||
this.onPromisesAreMadeListeners = []; | ||
this.onPromiseErrorListeners = []; | ||
this.onSetListeners = []; | ||
this.onCommitListeners = []; | ||
this.listeners = {}; | ||
} | ||
@@ -53,6 +48,3 @@ | ||
this.state = (0, _immutable.fromJS)({}); | ||
this.promises = []; | ||
this.onChangeListeners = []; | ||
this.onPromisesAreMadeListeners = []; | ||
this.onPromiseErrorListeners = []; | ||
this.listeners = {}; | ||
} | ||
@@ -78,4 +70,4 @@ }, { | ||
value: function commit(ctx) { | ||
var _this = this; | ||
var prevState = this.state; | ||
this._fire("willCommit", ctx.state); | ||
if (!ctx.promises.length) { | ||
@@ -91,11 +83,4 @@ log("NO PROMISES LEFT FOR CONTEXT %s", (0, _utils.insp)(ctx.path)); | ||
this.state = ctx.state; | ||
if (!this.promises.length) { | ||
this.onPromisesAreMadeListeners.forEach(function (f) { | ||
return f(_this.state.toJS()); | ||
}); | ||
} | ||
this.onChangeListeners.forEach(function (f) { | ||
return f(_this.state.toJS()); | ||
}); | ||
this._fireOnCommit(ctx); | ||
this._fire("didCommit", prevState); | ||
this._checkPromises(); | ||
log("LISTENERS DONE", (0, _utils.insp)(ctx.state)); | ||
@@ -105,2 +90,67 @@ return ctx; | ||
}, { | ||
key: "on", | ||
value: function on(eventName, fn) { | ||
if (this.listeners[eventName] === undefined) { | ||
this.listeners[eventName] = []; | ||
} | ||
if (this.listeners[eventName].filter(function (f) { | ||
return f.toString() === fn.toString(); | ||
}).length) { | ||
return this.listeners[eventName].length; | ||
} | ||
return this.listeners[eventName].push(fn); | ||
} | ||
}, { | ||
key: "_fire", | ||
value: function _fire(eventName) { | ||
var _this = this; | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
var listeners = this.listeners[eventName]; | ||
if (!listeners) { | ||
return; | ||
} | ||
listeners.forEach(function (fn) { | ||
return fn.apply(_this, args); | ||
}); | ||
} | ||
}, { | ||
key: "_checkPromises", | ||
value: function _checkPromises() { | ||
if (this.contexts.filter(function (context) { | ||
return context.promises.length; | ||
}).length) { | ||
return; | ||
} | ||
this._fire("allPromisesDone"); | ||
} | ||
}, { | ||
key: "onWillSet", | ||
value: function onWillSet(fn) { | ||
return this.on("willSet", fn); | ||
} | ||
}, { | ||
key: "onDidSet", | ||
value: function onDidSet(fn) { | ||
return this.on("didSet", fn); | ||
} | ||
}, { | ||
key: "onWillCommit", | ||
value: function onWillCommit(fn) { | ||
return this.on("willCommit", fn); | ||
} | ||
}, { | ||
key: "onDidCommit", | ||
value: function onDidCommit(fn) { | ||
return this.on("didCommit", fn); | ||
} | ||
}, { | ||
key: "onAllPromisesDone", | ||
value: function onAllPromisesDone(fn) { | ||
return this.on("allPromisesDone", fn); | ||
} | ||
}, { | ||
key: "getContexts", | ||
@@ -111,5 +161,2 @@ value: function getContexts() { | ||
}, { | ||
key: "toString", | ||
value: function toString() {} | ||
}, { | ||
key: "getState", | ||
@@ -139,36 +186,2 @@ value: function getState() { | ||
}, { | ||
key: "getContext", | ||
value: function getContext(state) { | ||
var _this2 = this; | ||
return { | ||
set: function set(path, value) { | ||
return _this2.set(path, value, state, false, false); | ||
}, | ||
get: function get(path) { | ||
return _this2.get(path, state); | ||
}, | ||
getState: function getState() { | ||
return state; | ||
} | ||
}; | ||
} | ||
}, { | ||
key: "reducePathAndValue", | ||
value: function reducePathAndValue(path, value) { | ||
var i = path.length; | ||
var v = value; | ||
while (i--) { | ||
var p = path.slice(0, i); | ||
var tmp = {}; | ||
tmp[path.slice(i)] = v; | ||
v = tmp; | ||
if (this.rules.get(p.join("."))) { | ||
path = p; | ||
value = v; | ||
} | ||
} | ||
return { path: path, value: value }; | ||
} | ||
}, { | ||
key: "getRule", | ||
@@ -180,41 +193,2 @@ value: function getRule(path) { | ||
}, { | ||
key: "onChange", | ||
value: function onChange(fn) { | ||
this.onChangeListeners.push(fn); | ||
} | ||
}, { | ||
key: "onSet", | ||
value: function onSet(fn) { | ||
this.onSetListeners.push(fn); | ||
} | ||
}, { | ||
key: "_fireOnSet", | ||
value: function _fireOnSet(branch) { | ||
this.onSetListeners.forEach(function (fn) { | ||
return fn(branch); | ||
}); | ||
} | ||
}, { | ||
key: "onCommit", | ||
value: function onCommit(fn) { | ||
this.onCommitListeners.push(fn); | ||
} | ||
}, { | ||
key: "_fireOnCommit", | ||
value: function _fireOnCommit(context) { | ||
this.onCommitListeners.forEach(function (fn) { | ||
return fn(context); | ||
}); | ||
} | ||
}, { | ||
key: "onPromisesAreMade", | ||
value: function onPromisesAreMade(fn) { | ||
this.onPromisesAreMadeListeners.push(fn); | ||
} | ||
}, { | ||
key: "onPromiseError", | ||
value: function onPromiseError(fn) { | ||
this.onPromiseErrorListeners.push(fn); | ||
} | ||
}, { | ||
key: "isEqual", | ||
@@ -221,0 +195,0 @@ value: function isEqual(state) { |
{ | ||
"name": "slt", | ||
"version": "1.1.4", | ||
"version": "1.2.0", | ||
"description": "Take care of your state", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -39,3 +39,3 @@ ## DISCLAIMER | ||
###Rules example | ||
###Rules example (v0.*) | ||
Rules is an object which is following the state structure. Say if you want to apply rule for some property in the state map (we call it Slot), you only need to declare function in the Rules object which key with the same state property name. (or path, e.g. it could be `"request.url": (url, context) {}`). Rule should return `context`. `context` has the same `set` method (which returns `context` with a new state). | ||
@@ -42,0 +42,0 @@ In example below we set "request" to the state, rule on key `request` sets active `route` and `session` (for session there is no rule) from request. When `route` state property is changed the `route` rule fires. It sets Promise to key `users.{id}`. When Promise will be resolved `users.{id}` will substituted with actual value of that Promise. |
@@ -14,3 +14,3 @@ import Slots from "../slots"; | ||
const slots2 = new Slots(rules, {}); | ||
slots2.onChange(cb2); | ||
slots2.onDidSet(cb2); | ||
it ('should call callback if state has changed', () => { | ||
@@ -23,5 +23,5 @@ slots2.set('route', {name: 'page', params: {id: 1}}).commit(); | ||
const slots4 = new Slots(rules, slots2.getState().toJS()); | ||
slots4.onChange(cb4); | ||
slots4.onDidSet(cb4); | ||
slots4.set('route', {name: 'page', params: {id: 1}}).commit(); | ||
expect(cb4).not.toHaveBeenCalled(); | ||
expect(cb4).toHaveBeenCalled(); | ||
}); | ||
@@ -28,0 +28,0 @@ |
@@ -55,3 +55,3 @@ import { fromJS, is, Map, List} from "immutable"; | ||
this.ctx.promises.splice(this.ctx.promises.indexOf(branch), 1); | ||
this.ctx.commit(); | ||
this.ctx.slots._checkPromises(this); | ||
}); | ||
@@ -71,3 +71,2 @@ } else { | ||
this.state = result; | ||
this.ctx.slots._fireOnSet(this); | ||
return this; | ||
@@ -74,0 +73,0 @@ } |
@@ -31,5 +31,10 @@ import { fromJS, is, Map, List} from "immutable"; | ||
this.path = path; | ||
let prevState = this.state; | ||
let branch = new Branch(this.rules, this.state, this); | ||
this.branches.push(branch); | ||
this.state = branch.set(path, value).getState(); | ||
this.slots._fire("beforeSet", prevState, this); | ||
let newState = branch.set(path, value).getState(); | ||
this.slots._fire("willSet", newState, this); //TODO: return false == do nothing | ||
this.state = newState; | ||
this.slots._fire("didSet", prevState, this); | ||
this.branches.splice(this.branches.indexOf(branch), 1); | ||
@@ -36,0 +41,0 @@ return this; |
127
src/slots.js
@@ -17,8 +17,3 @@ import { fromJS, is, Map, List} from "immutable"; | ||
this.contexts = []; | ||
this.promises = []; | ||
this.onChangeListeners = []; | ||
this.onPromisesAreMadeListeners = []; | ||
this.onPromiseErrorListeners = []; | ||
this.onSetListeners = []; | ||
this.onCommitListeners = []; | ||
this.listeners = {}; | ||
} | ||
@@ -28,6 +23,3 @@ | ||
this.state = fromJS({}); | ||
this.promises = []; | ||
this.onChangeListeners = []; | ||
this.onPromisesAreMadeListeners = []; | ||
this.onPromiseErrorListeners = []; | ||
this.listeners = {}; | ||
} | ||
@@ -47,2 +39,4 @@ | ||
commit (ctx) { | ||
let prevState = this.state; | ||
this._fire("willCommit", ctx.state); | ||
if (!ctx.promises.length) { | ||
@@ -58,7 +52,4 @@ log("NO PROMISES LEFT FOR CONTEXT %s", insp(ctx.path)); | ||
this.state = ctx.state; | ||
if (!this.promises.length) { | ||
this.onPromisesAreMadeListeners.forEach(f => f(this.state.toJS())); | ||
} | ||
this.onChangeListeners.forEach(f => f(this.state.toJS())); | ||
this._fireOnCommit(ctx); | ||
this._fire("didCommit", prevState); | ||
this._checkPromises(); | ||
log("LISTENERS DONE", insp(ctx.state)); | ||
@@ -68,10 +59,52 @@ return ctx; | ||
getContexts() { | ||
return this.contexts; | ||
on(eventName, fn) { | ||
if (this.listeners[eventName] === undefined) { | ||
this.listeners[eventName] = []; | ||
} | ||
if (this.listeners[eventName].filter(f => f.toString() === fn.toString()).length) { | ||
return this.listeners[eventName].length; | ||
} | ||
return this.listeners[eventName].push(fn); | ||
} | ||
toString() { | ||
_fire(eventName, ...args) { | ||
let listeners = this.listeners[eventName]; | ||
if (!listeners) { | ||
return; | ||
} | ||
listeners.forEach(fn => fn.apply(this, args)); | ||
} | ||
_checkPromises() { | ||
if (this.contexts.filter((context) => context.promises.length).length) { | ||
return; | ||
} | ||
this._fire("allPromisesDone"); | ||
} | ||
onWillSet(fn) { | ||
return this.on("willSet", fn); | ||
} | ||
onDidSet(fn) { | ||
return this.on("didSet", fn); | ||
} | ||
onWillCommit(fn) { | ||
return this.on("willCommit", fn); | ||
} | ||
onDidCommit(fn) { | ||
return this.on("didCommit", fn); | ||
} | ||
onAllPromisesDone(fn) { | ||
return this.on("allPromisesDone", fn); | ||
} | ||
getContexts() { | ||
return this.contexts; | ||
} | ||
getState() { | ||
@@ -95,32 +128,2 @@ return this.state; | ||
getContext(state) { | ||
return { | ||
set: (path, value) => { | ||
return this.set(path, value, state, false, false); | ||
}, | ||
get: (path) => { | ||
return this.get(path, state); | ||
}, | ||
getState: () => { | ||
return state; | ||
} | ||
} | ||
} | ||
reducePathAndValue(path, value) { | ||
let i = path.length; | ||
let v = value; | ||
while (i--) { | ||
let p = path.slice(0, i); | ||
let tmp = {}; | ||
tmp[path.slice(i)] = v; | ||
v = tmp; | ||
if (this.rules.get(p.join("."))) { | ||
path = p; | ||
value = v; | ||
} | ||
} | ||
return { path, value } | ||
} | ||
getRule(path) { | ||
@@ -131,30 +134,2 @@ path = Slots.makePath(path); | ||
onChange(fn) { | ||
this.onChangeListeners.push(fn); | ||
} | ||
onSet(fn) { | ||
this.onSetListeners.push(fn); | ||
} | ||
_fireOnSet(branch) { | ||
this.onSetListeners.forEach(fn => fn(branch)); | ||
} | ||
onCommit(fn) { | ||
this.onCommitListeners.push(fn); | ||
} | ||
_fireOnCommit(context) { | ||
this.onCommitListeners.forEach(fn => fn(context)); | ||
} | ||
onPromisesAreMade(fn) { | ||
this.onPromisesAreMadeListeners.push(fn); | ||
} | ||
onPromiseError(fn) { | ||
this.onPromiseErrorListeners.push(fn); | ||
} | ||
isEqual(state) { | ||
@@ -161,0 +136,0 @@ return is(fromJS(state), this.state); |
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
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
95749
1223