Comparing version 0.32.3 to 0.33.0
{ | ||
"name": "cerebral", | ||
"version": "0.32.3", | ||
"version": "0.33.0", | ||
"description": "A state controller with its own debugger", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
var utils = require('./utils.js') | ||
var Devtools = require('./modules/devtools') | ||
module.exports = function (controller, model, allModules) { | ||
@@ -40,49 +42,62 @@ var initialState = {} | ||
controller.on('reset', function () { | ||
model.mutators.merge([], initialState) | ||
}) | ||
return function registerModules (parentModuleName, modules) { | ||
var registerModules = function (parentModuleName, modules) { | ||
if (arguments.length === 1) { | ||
modules = parentModuleName | ||
parentModuleName = null | ||
// TODO: remove after devtools extracted to external module | ||
if (utils.isDeveloping() && !modules.devtools) { | ||
modules.devtools = Devtools() | ||
} | ||
} | ||
Object.keys(modules).forEach(function (moduleName) { | ||
var moduleConstructor = modules[moduleName] | ||
var actualName = moduleName | ||
if (parentModuleName) { | ||
moduleName = parentModuleName + '.' + moduleName | ||
} | ||
var moduleExport = { | ||
name: actualName, | ||
path: moduleName.split('.') | ||
} | ||
var module = { | ||
name: moduleName, | ||
alias: function (alias) { | ||
allModules[alias] = moduleExport | ||
}, | ||
signals: registerSignals.bind(null, moduleName), | ||
signalsSync: registerSignalsSync.bind(null, moduleName), | ||
services: registerServices.bind(null, moduleName), | ||
state: registerInitialState.bind(null, moduleName), | ||
getSignals: function () { | ||
var signals = controller.getSignals() | ||
var path = moduleName.split('.') | ||
return path.reduce(function (signals, key) { | ||
return signals[key] | ||
}, signals) | ||
}, | ||
modules: registerModules.bind(null, moduleName) | ||
} | ||
var constructedModule = moduleConstructor(module, controller) | ||
registerModule(moduleName, parentModuleName, modules) | ||
}) | ||
moduleExport.meta = constructedModule | ||
module.meta = constructedModule | ||
allModules[moduleName] = moduleExport | ||
return moduleExport | ||
}) | ||
return allModules | ||
} | ||
var registerModule = function (moduleName, parentModuleName, modules) { | ||
var moduleConstructor = modules[moduleName] | ||
var actualName = moduleName | ||
if (parentModuleName) { | ||
moduleName = parentModuleName + '.' + moduleName | ||
} | ||
var moduleExport = { | ||
name: actualName, | ||
path: moduleName.split('.') | ||
} | ||
var module = { | ||
name: moduleName, | ||
alias: function (alias) { | ||
allModules[alias] = moduleExport | ||
}, | ||
signals: registerSignals.bind(null, moduleName), | ||
signalsSync: registerSignalsSync.bind(null, moduleName), | ||
services: registerServices.bind(null, moduleName), | ||
state: registerInitialState.bind(null, moduleName), | ||
getSignals: function () { | ||
var signals = controller.getSignals() | ||
var path = moduleName.split('.') | ||
return path.reduce(function (signals, key) { | ||
return signals[key] | ||
}, signals) | ||
}, | ||
modules: registerModules.bind(null, moduleName) | ||
} | ||
var constructedModule = moduleConstructor(module, controller) | ||
moduleExport.meta = constructedModule | ||
module.meta = constructedModule | ||
allModules[moduleName] = moduleExport | ||
return moduleExport | ||
} | ||
controller.on('reset', function () { | ||
model.mutators.merge([], initialState) | ||
}) | ||
return registerModules | ||
} |
@@ -14,3 +14,3 @@ var utils = require('./utils.js') | ||
module.exports = function (signalStore, recorder, devtools, controller, model, services, compute, modules) { | ||
module.exports = function (controller, model, services, compute, modules) { | ||
return function () { | ||
@@ -29,22 +29,8 @@ var args = [].slice.call(arguments) | ||
var signalChain = function (payload, options) { | ||
if ( | ||
utils.isDeveloping() && | ||
!signalStore.isRemembering() && | ||
signalStore.getCurrentIndex() !== -1 && | ||
signalStore.getCurrentIndex() < signalStore.getSignals().length - 1 | ||
) { | ||
console.warn('Cerebral - Looking in the past, ignored signal ' + signalName) | ||
return | ||
} | ||
options = options || {} | ||
if (recorder.isPlaying() && !options.isRecorded) { | ||
return | ||
} | ||
var tree = staticTree(signalChain.chain) | ||
var actions = tree.actions | ||
var runSync = defaultOptions.isSync || options.isSync | ||
var runSync = defaultOptions.isSync || options.isSync || options.branches | ||
@@ -71,9 +57,2 @@ // When remembering, the branches with filled out values will be | ||
var start = Date.now() | ||
var recorderSignal = { | ||
name: signalName, | ||
input: payload, | ||
start: start, | ||
asyncActionPaths: [], | ||
asyncActionResults: [] | ||
} | ||
var signal = { | ||
@@ -83,22 +62,29 @@ name: signalName, | ||
isSync: runSync, | ||
isRouted: options.isRouted || false, | ||
isRouted: options.isRouted || false, // could be removed | ||
isExecuting: true, | ||
isPrevented: false, | ||
branches: branches, | ||
options: options, | ||
duration: 0, | ||
input: payload | ||
input: payload, | ||
preventSignalRun: function () { | ||
if (signal.isExecuting) { | ||
signal.isExecuting = false | ||
signal.isPrevented = true | ||
} | ||
} | ||
} | ||
if (!signalStore.isRemembering() && !recorder.isCatchingUp()) { | ||
if (!options.branches) { | ||
controller.emit('signalStart', {signal: signal}) | ||
} | ||
if (recorder.isRecording()) { | ||
recorder.addSignal(recorderSignal) | ||
if (signal.isPrevented) { | ||
controller.emit('signalEnd', {signal: signal}) | ||
return | ||
} | ||
signalStore.addSignal(signal) | ||
var runBranch = function (branch, index, start) { | ||
var currentBranch = branch[index] | ||
if (!currentBranch && branch === signal.branches && !signalStore.isRemembering() && !recorder.isCatchingUp()) { | ||
if (!currentBranch && branch === signal.branches && !options.branches) { | ||
// Might not be any actions passed | ||
@@ -112,3 +98,2 @@ if (branch[index - 1]) { | ||
controller.emit('change', {signal: signal}) | ||
devtools && devtools.update() | ||
return | ||
@@ -122,3 +107,3 @@ } | ||
if (Array.isArray(currentBranch)) { | ||
if (signalStore.isRemembering()) { | ||
if (options.branches) { | ||
currentBranch.forEach(function (action) { | ||
@@ -144,15 +129,2 @@ // If any signals has run with this action, run them | ||
runBranch(branch, index + 1) | ||
} else if (recorder.isCatchingUp()) { | ||
var currentSignal = recorder.getCurrentSignal() | ||
currentBranch.forEach(function (action) { | ||
var recordedAction = currentSignal.asyncActionResults[currentSignal.asyncActionPaths.indexOf(action.path.join('.'))] | ||
utils.merge(signalArgs, recordedAction.output) | ||
if (action.outputPath) { | ||
runBranch(action.outputs[recordedAction.outputPath], 0) | ||
} | ||
}) | ||
runBranch(branch, index + 1) | ||
} else { | ||
@@ -171,7 +143,5 @@ controller.emit('change', {signal: signal}) | ||
signalStore.addAsyncAction(action) | ||
action.isExecuting = true | ||
action.input = utils.merge({}, inputArg) | ||
var next = createNext.async(actionFunc) | ||
var next = createNext.async(actionFunc, signal.name) | ||
var modulesArg = createModulesArg(modules, actionArgs[1], services) | ||
@@ -194,25 +164,13 @@ actionFunc({ | ||
utils.merge(signalArgs, result.arg) | ||
signalStore.removeAsyncAction(action) | ||
controller.emit('actionEnd', {action: action, signal: signal}) | ||
controller.emit('change', {signal: signal}) | ||
if (recorder.isRecording()) { | ||
recorderSignal.asyncActionPaths.push(action.path.join('.')) | ||
recorderSignal.asyncActionResults.push({ | ||
output: result.arg, | ||
outputPath: result.path | ||
}) | ||
} | ||
if (result.path) { | ||
action.outputPath = result.path | ||
var branchResult = runBranch(action.outputs[result.path], 0, Date.now()) | ||
devtools && devtools.update() | ||
return branchResult | ||
} else { | ||
devtools && devtools.update() | ||
} | ||
}) | ||
}) | ||
devtools && devtools.update() | ||
return Promise.all(promises) | ||
@@ -230,3 +188,3 @@ .then(function () { | ||
var action = currentBranch | ||
if (signalStore.isRemembering()) { | ||
if (options.branches) { | ||
action.mutations.forEach(function (mutation) { | ||
@@ -243,2 +201,3 @@ model.mutators[mutation.name].apply(null, [mutation.path.slice()].concat(mutation.args)) | ||
controller.emit('actionStart', {action: action, signal: signal}) | ||
var actionFunc = actions[action.actionIndex] | ||
@@ -257,2 +216,3 @@ var inputArg = actionFunc.defaultInput ? utils.merge({}, actionFunc.defaultInput, signalArgs) : signalArgs | ||
var modulesArg = createModulesArg(modules, actionArgs[1], services) | ||
actionFunc({ | ||
@@ -295,2 +255,3 @@ input: actionArgs[0], | ||
controller.emit('actionEnd', {action: action, signal: signal}) | ||
return runBranch(branch, index + 1, start) | ||
@@ -300,2 +261,3 @@ }) | ||
controller.emit('actionEnd', {action: action, signal: signal}) | ||
return runBranch(branch, index + 1, start) | ||
@@ -312,3 +274,3 @@ } | ||
if (runSync || signalStore.isRemembering() || recorder.isCatchingUp()) { | ||
if (runSync) { | ||
runSignal() | ||
@@ -315,0 +277,0 @@ } else { |
var CreateSignalFactory = require('./CreateSignalFactory.js') | ||
var CreateSignalStore = require('./CreateSignalStore.js') | ||
var CreateRecorder = require('./CreateRecorder.js') | ||
var CreateRegisterModules = require('./CreateRegisterModules.js') | ||
var Devtools = require('./Devtools.js') | ||
var Compute = require('./Compute.js') | ||
var EventEmitter = require('events').EventEmitter | ||
var Recorder = require('./modules/recorder') | ||
var Controller = function (Model, services) { | ||
@@ -18,13 +17,6 @@ if (services) { | ||
var signals = {} | ||
var devtools = null | ||
var signalStore = CreateSignalStore(controller) | ||
var modules = {} | ||
services = services || {} | ||
if (typeof window !== 'undefined' && typeof window.addEventListener !== 'undefined') { | ||
devtools = Devtools(signalStore, controller) | ||
} | ||
var recorder = CreateRecorder(signalStore, signals, controller, model) | ||
var signalFactory = CreateSignalFactory(signalStore, recorder, devtools, controller, model, services, compute, modules) | ||
var signalFactory = CreateSignalFactory(controller, model, services, compute, modules) | ||
var signal = function () { | ||
@@ -54,7 +46,7 @@ var signalNamePath = arguments[0].split('.') | ||
controller.signal = function () { | ||
console.warn('This method is deprecated, use controller.signals() instead') | ||
console.warn('Cerebral: controller.signal() is deprecated, use controller.signals() instead') | ||
signal.apply(null, arguments) | ||
} | ||
controller.signalSync = function () { | ||
console.warn('This method is deprecated, use controller.signals() instead') | ||
console.warn('Cerebral: controller.signalSync() is deprecated, use controller.signals() instead') | ||
var defaultOptions = arguments[2] || {} | ||
@@ -71,8 +63,2 @@ defaultOptions.isSync = true | ||
} | ||
controller.getStore = function () { | ||
return signalStore | ||
} | ||
controller.getRecorder = function () { | ||
return recorder | ||
} | ||
controller.get = function () { | ||
@@ -85,5 +71,2 @@ if (typeof arguments[0] === 'function') { | ||
} | ||
controller.getDevtools = function () { | ||
return devtools | ||
} | ||
controller.logModel = function () { | ||
@@ -116,2 +99,5 @@ return model.logModel() | ||
// emulate loading recorder | ||
Recorder()({}, controller) | ||
return controller | ||
@@ -118,0 +104,0 @@ } |
@@ -787,2 +787,24 @@ var Controller = require('./../src/index.js') | ||
suite['should not run signal if prevented'] = function (test) { | ||
var ctrl = Controller(Model()) | ||
var signal = [ | ||
function () { test.ok(false) } | ||
] | ||
ctrl.signals({ | ||
'test': signal | ||
}) | ||
ctrl.on('signalStart', function (args) { | ||
args.signal.preventSignalRun() | ||
}) | ||
ctrl.on('signalEnd', function (args) { | ||
test.ok(args.signal.isPrevented) | ||
test.done() | ||
}) | ||
ctrl.getSignals().test() | ||
} | ||
module.exports = { signals: suite } |
var Controller = require('./../src/index.js') | ||
// trick to load devtools module with signalStore | ||
global.window = { | ||
addEventListener: function () {}, | ||
dispatchEvent: function () {} | ||
} | ||
global.CustomEvent = function () {} | ||
var suite = {} | ||
@@ -22,2 +28,3 @@ | ||
var ctrl = Controller(Model()) | ||
ctrl.modules({}) | ||
var signal = [ | ||
@@ -40,2 +47,3 @@ function () {} | ||
var ctrl = Controller(Model()) | ||
ctrl.modules({}) | ||
var signal = [ | ||
@@ -62,2 +70,3 @@ function ActionA () {} | ||
var ctrl = Controller(Model()) | ||
ctrl.modules({}) | ||
var signal = [ | ||
@@ -87,2 +96,3 @@ function ActionA () {} | ||
var ctrl = Controller(Model()) | ||
ctrl.modules({}) | ||
var signal = [ | ||
@@ -108,2 +118,3 @@ function ActionA () {} | ||
var ctrl = Controller(Model()) | ||
ctrl.modules({}) | ||
var signal = [ | ||
@@ -133,2 +144,3 @@ function ActionA (args) { | ||
var ctrl = Controller(Model()) | ||
ctrl.modules({}) | ||
var signalSync = [ | ||
@@ -176,2 +188,3 @@ function ActionA (args) { | ||
var ctrl = Controller(Model()) | ||
ctrl.modules({}) | ||
var signal = [ | ||
@@ -197,2 +210,3 @@ [ | ||
var ctrl = Controller(Model()) | ||
ctrl.modules({}) | ||
var signal = [ | ||
@@ -249,2 +263,3 @@ function (args) { | ||
var ctrl = Controller(Model()) | ||
ctrl.modules({}) | ||
var signal = [ | ||
@@ -303,2 +318,3 @@ function (args) { | ||
var ctrl = Controller(Model()) | ||
ctrl.modules({}) | ||
var signal = [ | ||
@@ -355,2 +371,3 @@ [ | ||
var ctrl = Controller(Model()) | ||
ctrl.modules({}) | ||
@@ -414,2 +431,3 @@ ctrl.signalsSync({ | ||
var ctrl = Controller(Model()) | ||
ctrl.modules({}) | ||
@@ -416,0 +434,0 @@ ctrl.signalsSync({ |
298570
3667