Comparing version 0.4.1 to 0.4.2
{ | ||
"name": "cerebral", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "An intelligent react application framework", | ||
@@ -5,0 +5,0 @@ "main": "src/Cerebral.js", |
@@ -45,3 +45,3 @@ 'use strict'; | ||
var description = func(); | ||
map(path, description.deps, description.get); | ||
map(path, description); | ||
return description.value; | ||
@@ -67,3 +67,3 @@ }; | ||
console.warn('Cerebral was unable to remember your state, probably due to an incompatible change in the code. State has been reset!'); | ||
helpers.eventStore.reset(helpers.currentState); | ||
helpers.eventStore.reset(helpers); | ||
} | ||
@@ -145,3 +145,3 @@ | ||
helpers.asyncCallbacks = {}; | ||
helpers.eventStore.reset(helpers.currentState); | ||
helpers.eventStore.reset(helpers); | ||
}; | ||
@@ -157,5 +157,5 @@ | ||
var mapPath = utils.getMapPath(path, maps); | ||
if (mapPath) { | ||
return mapPath(); | ||
var mapValue = utils.getMapPath(path, maps); | ||
if (mapValue) { | ||
return mapValue; | ||
} | ||
@@ -162,0 +162,0 @@ |
@@ -19,5 +19,7 @@ "use strict"; | ||
eventStore: eventStore, | ||
runningSignal: null, | ||
nextRef: 0, | ||
currentSignal: eventStore.currentIndex, | ||
onFunction: null, | ||
mapCallbacks: [], | ||
asyncCallbacks: utils.hasLocalStorage() && localStorage.getItem('cerebral_asyncCallbacks') ? | ||
@@ -24,0 +26,0 @@ JSON.parse(localStorage.getItem('cerebral_asyncCallbacks')) : |
@@ -6,6 +6,8 @@ "use strict"; | ||
return function(path, depPaths, callback) { | ||
return function(path, description) { | ||
var prevResult = null; | ||
var state = null; | ||
var depPaths = description.deps; | ||
var callback = description.get; | ||
var deps = depPaths; | ||
@@ -36,14 +38,21 @@ path = (typeof path === 'string' ? [path] : path).slice(); | ||
prevResult = callback(store, depsState, utils.getPath(path, helpers.currentState)); | ||
return prevResult; | ||
setValue(prevResult); | ||
} else { | ||
return prevResult; | ||
setValue(prevResult); | ||
} | ||
}; | ||
var mapPath = maps; | ||
var pathCopy = path.slice(); | ||
while(pathCopy.length) { | ||
mapPath = mapPath[pathCopy.shift()] = pathCopy.length ? {} : update; | ||
} | ||
var setValue = function(value) { | ||
var mapPath = maps; | ||
var pathCopy = path.slice(); | ||
while (pathCopy.length) { | ||
mapPath = mapPath[pathCopy.shift()] = pathCopy.length ? {} : value; | ||
} | ||
}; | ||
setValue(description.value); | ||
store.on('mapUpdate', update); | ||
helpers.mapCallbacks.push(update); | ||
}; | ||
@@ -50,0 +59,0 @@ |
@@ -43,3 +43,2 @@ "use strict"; | ||
} catch (e) { | ||
console.log('helpers.currentState', e); | ||
throw new Error('Unable to run a "' + mutator + '" on: ' + JSON.stringify(path)); | ||
@@ -46,0 +45,0 @@ } |
@@ -13,3 +13,6 @@ "use strict"; | ||
var executionArray = callbacks.slice(); | ||
var signalIndex = helpers.eventStore.willKeepState ? helpers.eventStore.currentIndex + 1 : 0; | ||
var signalIndex = helpers.eventStore.willKeepState ? | ||
!!helpers.runningSignal ? helpers.eventStore.currentIndex : ++helpers.eventStore.currentIndex : 0; | ||
var initiatedSignal = helpers.runningSignal || name; | ||
var runMutation = function() { | ||
@@ -42,2 +45,3 @@ | ||
signalIndex: signalIndex, | ||
signalName: name, | ||
index: callbacks.indexOf(callback), | ||
@@ -51,10 +55,10 @@ name: utils.getFunctionName(callback), | ||
isAsync = ( | ||
helpers.asyncCallbacks[name] && | ||
helpers.asyncCallbacks[name][signalIndex] && | ||
helpers.asyncCallbacks[name][signalIndex][action.name] | ||
isAsync = !!( | ||
helpers.asyncCallbacks[helpers.runningSignal] && | ||
helpers.asyncCallbacks[helpers.runningSignal][signalIndex] && | ||
(action.name in helpers.asyncCallbacks[helpers.runningSignal][signalIndex]) | ||
); | ||
if (store.isRemembering && isAsync) { | ||
result = helpers.asyncCallbacks[name][signalIndex][action.name]; | ||
result = helpers.asyncCallbacks[helpers.runningSignal][signalIndex][action.name]; | ||
} else if (Array.isArray(callback)) { | ||
@@ -98,5 +102,5 @@ result = Promise.all(callback.map(function(callback) { | ||
helpers.asyncCallbacks[name] = helpers.asyncCallbacks[name] || {}; | ||
helpers.asyncCallbacks[name][signalIndex] = helpers.asyncCallbacks[name][signalIndex] || {}; | ||
helpers.asyncCallbacks[name][signalIndex][action.name] = result; | ||
helpers.asyncCallbacks[initiatedSignal] = helpers.asyncCallbacks[initiatedSignal] || {}; | ||
helpers.asyncCallbacks[initiatedSignal][signalIndex] = helpers.asyncCallbacks[initiatedSignal][signalIndex] || {}; | ||
helpers.asyncCallbacks[initiatedSignal][signalIndex][action.name] = result || null; // JS or Chrome bug causing undefined not to set key | ||
@@ -130,2 +134,4 @@ helpers.eventStore.addAsyncSignal({ | ||
!helpers.eventStore.isSilent && store.emit('update'); | ||
store.emit('mapUpdate'); | ||
helpers.runningSignal = null; | ||
} | ||
@@ -135,3 +141,6 @@ | ||
helpers.eventStore.addSignal(signal); | ||
if (!helpers.runningSignal) { | ||
helpers.runningSignal = helpers.runningSignal || name; | ||
helpers.eventStore.addSignal(signal); | ||
} | ||
execute.apply(null, args); | ||
@@ -141,3 +150,3 @@ | ||
if (store.isRemembering || typeof requestAnimationFrame === 'undefined') { | ||
if (!!helpers.runningSignal || store.isRemembering || typeof requestAnimationFrame === 'undefined') { | ||
runMutation(); | ||
@@ -144,0 +153,0 @@ } else { |
@@ -132,3 +132,7 @@ var React = require('react'); | ||
null | ||
)), | ||
), | ||
DOM.small({ | ||
style: {color: '#888'} | ||
}, action.signalName !== signal.name ? ' (' + action.signalName + ')' : null) | ||
), | ||
DOM.ul({ | ||
@@ -135,0 +139,0 @@ style: ActionStyle |
@@ -103,2 +103,4 @@ "use strict"; | ||
store.removeAllListeners('mapUpdate'); | ||
// First remove all state, as there might have been some | ||
@@ -129,2 +131,3 @@ // additions | ||
store.isRemembering = true; | ||
store.removeAllListeners('mapUpdate'); | ||
@@ -147,2 +150,4 @@ // Make sure we do not trigger any events | ||
// Run through events | ||
this.currentIndex = -1; | ||
for (var x = 0; x <= index; x++) { | ||
@@ -154,2 +159,3 @@ | ||
} | ||
store.signals[signal.name].apply(store, signal.args); | ||
@@ -163,3 +169,3 @@ this.currentIndex = x; | ||
store.isRemembering = false; | ||
store.emit('eventStoreUpdate'); | ||
store.emit('update'); | ||
@@ -166,0 +172,0 @@ return store; |
@@ -64,3 +64,3 @@ var Promise = require('es6-promise').Promise; | ||
exports['should run async actions synchronously'] = function(test) { | ||
exports['should run async actions synchronously when remembering'] = function(test) { | ||
var cerebral = new Cerebral({ | ||
@@ -77,9 +77,14 @@ foo: 'bar' | ||
cerebral.once('update', function() { | ||
cerebral.remember(-1); | ||
test.equals(cerebral.get('foo'), 'bar'); | ||
cerebral.remember(0); | ||
test.equals(cerebral.get('foo'), 'bar2'); | ||
test.done(); | ||
// Can not test this synchronously due to signals being | ||
// able to run after each other | ||
setTimeout(function() { | ||
cerebral.remember(-1); | ||
test.equals(cerebral.get('foo'), 'bar'); | ||
cerebral.remember(0); | ||
test.equals(cerebral.get('foo'), 'bar2'); | ||
test.done(); | ||
}, 0); | ||
}); | ||
cerebral.signals.test('bar2'); | ||
}; |
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
164482
2619