stent
Advanced tools
Comparing version 6.0.0 to 6.0.1
@@ -1,15 +0,15 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _ = require('../'); | ||
var _ = require("../"); | ||
var _constants = require('../constants'); | ||
var _constants = require("../constants"); | ||
var _helpers = require('../helpers'); | ||
var _helpers = require("../helpers"); | ||
var create = function create() { | ||
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'app'; | ||
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "app"; | ||
return _.Machine.create(name, { | ||
state: { idle: { run: 'running' } }, | ||
state: { idle: { run: "running" } }, | ||
transitions: {} | ||
@@ -19,12 +19,12 @@ }); | ||
describe('Given the Stent library', function () { | ||
describe("Given the Stent library", function () { | ||
beforeEach(function () { | ||
_.Machine.flush(); | ||
}); | ||
describe('when creating a new machine', function () { | ||
it('should have the machine with its name set up', function () { | ||
expect(create('foo').name).to.equal('foo'); | ||
describe("when creating a new machine", function () { | ||
it("should have the machine with its name set up", function () { | ||
expect(create("foo").name).to.equal("foo"); | ||
}); | ||
describe('and we have a middleware attached', function () { | ||
it('should trigger the middleware hook', function () { | ||
describe("and we have a middleware attached", function () { | ||
it("should trigger the middleware hook", function () { | ||
var spy = sinon.spy(); | ||
@@ -35,7 +35,7 @@ | ||
}); | ||
create('xxxa'); | ||
create("xxxa"); | ||
expect(spy).to.be.calledOnce.and.to.be.calledWith(sinon.match({ name: 'xxxa' })); | ||
expect(spy).to.be.calledOnce.and.to.be.calledWith(sinon.match({ name: "xxxa" })); | ||
}); | ||
it('should call the onMiddlewareRegister hook if available', function () { | ||
it("should call the onMiddlewareRegister hook if available", function () { | ||
var spy = sinon.spy(); | ||
@@ -54,33 +54,33 @@ | ||
}); | ||
describe('when `getting a machine', function () { | ||
it('should return the machine if it exists', function () { | ||
create('bar'); | ||
var foo = create('foo'); | ||
describe("when `getting a machine", function () { | ||
it("should return the machine if it exists", function () { | ||
create("bar"); | ||
var foo = create("foo"); | ||
expect(_.Machine.get('bar').name).to.equal('bar'); | ||
expect(_.Machine.get(foo).name).to.equal('foo'); | ||
expect(_.Machine.get("bar").name).to.equal("bar"); | ||
expect(_.Machine.get(foo).name).to.equal("foo"); | ||
}); | ||
it('should throw an error if the machine does not exist', function () { | ||
create('bar'); | ||
it("should throw an error if the machine does not exist", function () { | ||
create("bar"); | ||
expect(_.Machine.get.bind(_.Machine, 'baz')).to.throw((0, _constants.ERROR_MISSING_MACHINE)('baz')); | ||
expect(_.Machine.get.bind(_.Machine, "baz")).to.throw((0, _constants.ERROR_MISSING_MACHINE)("baz")); | ||
}); | ||
}); | ||
describe('when creating a machine without a name', function () { | ||
it('should be possible to fetch it by using the machine itself or the its generated name', function () { | ||
describe("when creating a machine without a name", function () { | ||
it("should be possible to fetch it by using the machine itself or the its generated name", function () { | ||
var machine = _.Machine.create({ | ||
state: { name: 'idle' }, | ||
transitions: { idle: { run: 'running' } } | ||
state: { name: "idle" }, | ||
transitions: { idle: { run: "running" } } | ||
}); | ||
expect(_.Machine.get(machine).state.name).to.equal('idle'); | ||
expect(_.Machine.get(machine.name).state.name).to.equal('idle'); | ||
expect(_.Machine.get(machine).state.name).to.equal("idle"); | ||
expect(_.Machine.get(machine.name).state.name).to.equal("idle"); | ||
}); | ||
}); | ||
describe('when we fire two actions one after each other', function () { | ||
describe('and we use the .latest version of the action', function () { | ||
it('should cancel the first action and only work with the second one', function (done) { | ||
describe("when we fire two actions one after each other", function () { | ||
describe("and we use the .latest version of the action", function () { | ||
it("should cancel the first action and only work with the second one", function (done) { | ||
var backend = sinon.stub(); | ||
backend.withArgs('s').returns('salad'); | ||
backend.withArgs('st').returns('stent'); | ||
backend.withArgs("s").returns("salad"); | ||
backend.withArgs("st").returns("stent"); | ||
@@ -96,3 +96,3 @@ var api = function api(char) { | ||
var machine = _.Machine.create({ | ||
state: { name: 'x' }, | ||
state: { name: "x" }, | ||
transitions: { | ||
@@ -111,6 +111,6 @@ x: { | ||
match = _context.sent; | ||
return _context.abrupt('return', { name: 'y', match: match }); | ||
return _context.abrupt("return", { name: "y", match: match }); | ||
case 4: | ||
case 'end': | ||
case "end": | ||
return _context.stop(); | ||
@@ -123,3 +123,3 @@ } | ||
y: { | ||
'noway': 'x' | ||
noway: "x" | ||
} | ||
@@ -129,7 +129,7 @@ } | ||
machine.type.latest('s'); | ||
machine.type.latest('st'); | ||
machine.type.latest("s"); | ||
machine.type.latest("st"); | ||
setTimeout(function () { | ||
expect(machine.state).to.deep.equal({ name: 'y', match: 'stent' }); | ||
expect(machine.state).to.deep.equal({ name: "y", match: "stent" }); | ||
done(); | ||
@@ -140,18 +140,18 @@ }, 20); | ||
}); | ||
describe('when using the `destroy` method', function () { | ||
it('should delete the machine', function () { | ||
_.Machine.create('foo', { state: {}, transitions: {} }); | ||
var B = _.Machine.create('bar', { state: {}, transitions: {} }); | ||
describe("when using the `destroy` method", function () { | ||
it("should delete the machine", function () { | ||
_.Machine.create("foo", { state: {}, transitions: {} }); | ||
var B = _.Machine.create("bar", { state: {}, transitions: {} }); | ||
expect(_typeof(_.Machine.machines.foo)).to.equal('object'); | ||
_.Machine.destroy('foo'); | ||
expect(_typeof(_.Machine.machines.foo)).to.equal('undefined'); | ||
expect(_typeof(_.Machine.machines.foo)).to.equal("object"); | ||
_.Machine.destroy("foo"); | ||
expect(_typeof(_.Machine.machines.foo)).to.equal("undefined"); | ||
expect(_typeof(_.Machine.machines.bar)).to.equal('object'); | ||
expect(_typeof(_.Machine.machines.bar)).to.equal("object"); | ||
_.Machine.destroy(B); | ||
expect(_typeof(_.Machine.machines.bar)).to.equal('undefined'); | ||
expect(_typeof(_.Machine.machines.bar)).to.equal("undefined"); | ||
}); | ||
describe('and the machine does not exist', function () { | ||
it('should throw an error', function () { | ||
expect(_.Machine.destroy.bind(_.Machine, 'foo')).to.throw('foo'); | ||
describe("and the machine does not exist", function () { | ||
it("should throw an error", function () { | ||
expect(_.Machine.destroy.bind(_.Machine, "foo")).to.throw("foo"); | ||
}); | ||
@@ -158,0 +158,0 @@ }); |
@@ -14,3 +14,3 @@ 'use strict'; | ||
var ERROR_WRONG_STATE_FORMAT = exports.ERROR_WRONG_STATE_FORMAT = function ERROR_WRONG_STATE_FORMAT(state) { | ||
var serialized = (typeof state === 'undefined' ? 'undefined' : _typeof(state)) === 'object' ? JSON.stringify(state, null, 2) : state; | ||
var serialized = (typeof state === 'undefined' ? 'undefined' : _typeof(state)) === "object" ? JSON.stringify(state, null, 2) : state; | ||
@@ -22,21 +22,24 @@ return 'The state should be an object and it should always have at least "name" property. You passed ' + serialized; | ||
}; | ||
var ERROR_NOT_SUPPORTED_HANDLER_TYPE = exports.ERROR_NOT_SUPPORTED_HANDLER_TYPE = 'Wrong handler type passed. Please read the docs https://github.com/krasimir/stent'; | ||
var ERROR_NOT_SUPPORTED_HANDLER_TYPE = exports.ERROR_NOT_SUPPORTED_HANDLER_TYPE = "Wrong handler type passed. Please read the docs https://github.com/krasimir/stent"; | ||
var ERROR_RESERVED_WORD_USED_AS_ACTION = exports.ERROR_RESERVED_WORD_USED_AS_ACTION = function ERROR_RESERVED_WORD_USED_AS_ACTION(word) { | ||
return 'Sorry, you can\'t use ' + word + ' as a name for an action. It is reserved.'; | ||
}; | ||
var ERROR_GENERATOR_FUNC_CALL_FAILED = exports.ERROR_GENERATOR_FUNC_CALL_FAILED = function ERROR_GENERATOR_FUNC_CALL_FAILED(type) { | ||
return 'The argument passed to `call` is falsy (' + type + ')'; | ||
}; | ||
// middlewares | ||
var MIDDLEWARE_PROCESS_ACTION = exports.MIDDLEWARE_PROCESS_ACTION = 'onActionDispatched'; | ||
var MIDDLEWARE_ACTION_PROCESSED = exports.MIDDLEWARE_ACTION_PROCESSED = 'onActionProcessed'; | ||
var MIDDLEWARE_STATE_WILL_CHANGE = exports.MIDDLEWARE_STATE_WILL_CHANGE = 'onStateWillChange'; | ||
var MIDDLEWARE_PROCESS_STATE_CHANGE = exports.MIDDLEWARE_PROCESS_STATE_CHANGE = 'onStateChanged'; | ||
var MIDDLEWARE_GENERATOR_STEP = exports.MIDDLEWARE_GENERATOR_STEP = 'onGeneratorStep'; | ||
var MIDDLEWARE_GENERATOR_END = exports.MIDDLEWARE_GENERATOR_END = 'onGeneratorEnd'; | ||
var MIDDLEWARE_GENERATOR_RESUMED = exports.MIDDLEWARE_GENERATOR_RESUMED = 'onGeneratorResumed'; | ||
var MIDDLEWARE_MACHINE_CREATED = exports.MIDDLEWARE_MACHINE_CREATED = 'onMachineCreated'; | ||
var MIDDLEWARE_MACHINE_CONNECTED = exports.MIDDLEWARE_MACHINE_CONNECTED = 'onMachineConnected'; | ||
var MIDDLEWARE_MACHINE_DISCONNECTED = exports.MIDDLEWARE_MACHINE_DISCONNECTED = 'onMachineDisconnected'; | ||
var MIDDLEWARE_REGISTERED = exports.MIDDLEWARE_REGISTERED = 'onMiddlewareRegister'; | ||
var MIDDLEWARE_PROCESS_ACTION = exports.MIDDLEWARE_PROCESS_ACTION = "onActionDispatched"; | ||
var MIDDLEWARE_ACTION_PROCESSED = exports.MIDDLEWARE_ACTION_PROCESSED = "onActionProcessed"; | ||
var MIDDLEWARE_STATE_WILL_CHANGE = exports.MIDDLEWARE_STATE_WILL_CHANGE = "onStateWillChange"; | ||
var MIDDLEWARE_PROCESS_STATE_CHANGE = exports.MIDDLEWARE_PROCESS_STATE_CHANGE = "onStateChanged"; | ||
var MIDDLEWARE_GENERATOR_STEP = exports.MIDDLEWARE_GENERATOR_STEP = "onGeneratorStep"; | ||
var MIDDLEWARE_GENERATOR_END = exports.MIDDLEWARE_GENERATOR_END = "onGeneratorEnd"; | ||
var MIDDLEWARE_GENERATOR_RESUMED = exports.MIDDLEWARE_GENERATOR_RESUMED = "onGeneratorResumed"; | ||
var MIDDLEWARE_MACHINE_CREATED = exports.MIDDLEWARE_MACHINE_CREATED = "onMachineCreated"; | ||
var MIDDLEWARE_MACHINE_CONNECTED = exports.MIDDLEWARE_MACHINE_CONNECTED = "onMachineConnected"; | ||
var MIDDLEWARE_MACHINE_DISCONNECTED = exports.MIDDLEWARE_MACHINE_DISCONNECTED = "onMachineDisconnected"; | ||
var MIDDLEWARE_REGISTERED = exports.MIDDLEWARE_REGISTERED = "onMiddlewareRegister"; | ||
// misc | ||
var DEVTOOLS_KEY = exports.DEVTOOLS_KEY = '__hello__stent__'; | ||
var DEVTOOLS_KEY = exports.DEVTOOLS_KEY = "__hello__stent__"; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -9,9 +9,9 @@ exports.__esModule = true; | ||
var _handleMiddleware = require('./handleMiddleware'); | ||
var _handleMiddleware = require("./handleMiddleware"); | ||
var _handleMiddleware2 = _interopRequireDefault(_handleMiddleware); | ||
var _constants = require('../constants'); | ||
var _constants = require("../constants"); | ||
var _updateState = require('./updateState'); | ||
var _updateState = require("./updateState"); | ||
@@ -43,3 +43,3 @@ var _updateState2 = _interopRequireDefault(_updateState); | ||
// yield call | ||
if (_typeof(result.value) === 'object' && result.value.__type === 'call') { | ||
if (_typeof(result.value) === "object" && result.value.__type === "call") { | ||
var _result$value = result.value, | ||
@@ -49,2 +49,9 @@ func = _result$value.func, | ||
if (!func) { | ||
var error = (0, _constants.ERROR_GENERATOR_FUNC_CALL_FAILED)(typeof func === "undefined" ? "undefined" : _typeof(func)); | ||
(0, _handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED, machine, error); | ||
return iterate(generatorThrow(generator, new Error(error))); | ||
} | ||
var funcResult = func.apply(undefined, args); | ||
@@ -59,3 +66,3 @@ | ||
// promise | ||
if (typeof funcResult.then !== 'undefined') { | ||
if (typeof funcResult.then !== "undefined") { | ||
funcResult.then(function (result) { | ||
@@ -69,3 +76,3 @@ (0, _handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED, machine, result); | ||
// generator | ||
} else if (typeof funcResult.next === 'function') { | ||
} else if (typeof funcResult.next === "function") { | ||
try { | ||
@@ -72,0 +79,0 @@ cancelInsideGenerator = handleGenerator(machine, funcResult, function (generatorResult) { |
{ | ||
"name": "stent", | ||
"version": "6.0.0", | ||
"version": "6.0.1", | ||
"description": "Stent is combining the ideas of redux with the concept of state machines", | ||
@@ -5,0 +5,0 @@ "main": "lib", |
@@ -1,20 +0,21 @@ | ||
import { Machine } from '../'; | ||
import { ERROR_MISSING_MACHINE } from '../constants'; | ||
import { call } from '../helpers'; | ||
import { Machine } from "../"; | ||
import { ERROR_MISSING_MACHINE } from "../constants"; | ||
import { call } from "../helpers"; | ||
const create = (name = 'app') => Machine.create(name, { | ||
state: { idle: { run: 'running' } }, | ||
transitions: {} | ||
}) | ||
const create = (name = "app") => | ||
Machine.create(name, { | ||
state: { idle: { run: "running" } }, | ||
transitions: {} | ||
}); | ||
describe('Given the Stent library', function () { | ||
describe("Given the Stent library", function() { | ||
beforeEach(() => { | ||
Machine.flush(); | ||
}); | ||
describe('when creating a new machine', function () { | ||
it('should have the machine with its name set up', function () { | ||
expect(create('foo').name).to.equal('foo'); | ||
describe("when creating a new machine", function() { | ||
it("should have the machine with its name set up", function() { | ||
expect(create("foo").name).to.equal("foo"); | ||
}); | ||
describe('and we have a middleware attached', function () { | ||
it('should trigger the middleware hook', function () { | ||
describe("and we have a middleware attached", function() { | ||
it("should trigger the middleware hook", function() { | ||
const spy = sinon.spy(); | ||
@@ -25,9 +26,11 @@ | ||
}); | ||
create('xxxa'); | ||
create("xxxa"); | ||
expect(spy).to.be.calledOnce.and.to.be.calledWith(sinon.match({ name: 'xxxa'})); | ||
expect(spy).to.be.calledOnce.and.to.be.calledWith( | ||
sinon.match({ name: "xxxa" }) | ||
); | ||
}); | ||
it('should call the onMiddlewareRegister hook if available', function () { | ||
it("should call the onMiddlewareRegister hook if available", function() { | ||
const spy = sinon.spy(); | ||
Machine.addMiddleware({ | ||
@@ -44,53 +47,54 @@ onMiddlewareRegister: spy | ||
}); | ||
describe('when `getting a machine', function () { | ||
it('should return the machine if it exists', function () { | ||
create('bar'); | ||
const foo = create('foo'); | ||
describe("when `getting a machine", function() { | ||
it("should return the machine if it exists", function() { | ||
create("bar"); | ||
const foo = create("foo"); | ||
expect(Machine.get('bar').name).to.equal('bar'); | ||
expect(Machine.get(foo).name).to.equal('foo'); | ||
expect(Machine.get("bar").name).to.equal("bar"); | ||
expect(Machine.get(foo).name).to.equal("foo"); | ||
}); | ||
it('should throw an error if the machine does not exist', function () { | ||
create('bar'); | ||
it("should throw an error if the machine does not exist", function() { | ||
create("bar"); | ||
expect(Machine.get.bind(Machine, 'baz')).to.throw(ERROR_MISSING_MACHINE('baz')); | ||
expect(Machine.get.bind(Machine, "baz")).to.throw( | ||
ERROR_MISSING_MACHINE("baz") | ||
); | ||
}); | ||
}); | ||
describe('when creating a machine without a name', function () { | ||
it('should be possible to fetch it by using the machine itself or the its generated name', function () { | ||
describe("when creating a machine without a name", function() { | ||
it("should be possible to fetch it by using the machine itself or the its generated name", function() { | ||
const machine = Machine.create({ | ||
state: { name: 'idle' }, | ||
transitions: { idle: { run: 'running' } } | ||
state: { name: "idle" }, | ||
transitions: { idle: { run: "running" } } | ||
}); | ||
expect(Machine.get(machine).state.name).to.equal('idle'); | ||
expect(Machine.get(machine.name).state.name).to.equal('idle'); | ||
expect(Machine.get(machine).state.name).to.equal("idle"); | ||
expect(Machine.get(machine.name).state.name).to.equal("idle"); | ||
}); | ||
}); | ||
describe('when we fire two actions one after each other', function () { | ||
describe('and we use the .latest version of the action', function () { | ||
it('should cancel the first action and only work with the second one', | ||
function (done) { | ||
describe("when we fire two actions one after each other", function() { | ||
describe("and we use the .latest version of the action", function() { | ||
it("should cancel the first action and only work with the second one", function(done) { | ||
const backend = sinon.stub(); | ||
backend.withArgs('s').returns('salad'); | ||
backend.withArgs('st').returns('stent'); | ||
backend.withArgs("s").returns("salad"); | ||
backend.withArgs("st").returns("stent"); | ||
const api = function (char) { | ||
const api = function(char) { | ||
return new Promise(resolve => { | ||
setTimeout(() => resolve(backend(char)), 10); | ||
}); | ||
} | ||
}; | ||
const machine = Machine.create({ | ||
state: { name: 'x' }, | ||
state: { name: "x" }, | ||
transitions: { | ||
x: { | ||
type: function * (state, letter) { | ||
type: function*(state, letter) { | ||
const match = yield call(api, letter); | ||
return { name: 'y', match }; | ||
return { name: "y", match }; | ||
} | ||
}, | ||
y: { | ||
'noway': 'x' | ||
noway: "x" | ||
} | ||
@@ -100,7 +104,7 @@ } | ||
machine.type.latest('s'); | ||
machine.type.latest('st'); | ||
machine.type.latest("s"); | ||
machine.type.latest("st"); | ||
setTimeout(function () { | ||
expect(machine.state).to.deep.equal({ name: 'y', match: 'stent' }); | ||
setTimeout(function() { | ||
expect(machine.state).to.deep.equal({ name: "y", match: "stent" }); | ||
done(); | ||
@@ -111,18 +115,18 @@ }, 20); | ||
}); | ||
describe('when using the `destroy` method', function () { | ||
it('should delete the machine', function () { | ||
Machine.create('foo', { state: {}, transitions: {} }); | ||
const B = Machine.create('bar', { state: {}, transitions: {} }); | ||
describe("when using the `destroy` method", function() { | ||
it("should delete the machine", function() { | ||
Machine.create("foo", { state: {}, transitions: {} }); | ||
const B = Machine.create("bar", { state: {}, transitions: {} }); | ||
expect(typeof Machine.machines.foo).to.equal('object'); | ||
Machine.destroy('foo'); | ||
expect(typeof Machine.machines.foo).to.equal('undefined'); | ||
expect(typeof Machine.machines.foo).to.equal("object"); | ||
Machine.destroy("foo"); | ||
expect(typeof Machine.machines.foo).to.equal("undefined"); | ||
expect(typeof Machine.machines.bar).to.equal('object'); | ||
expect(typeof Machine.machines.bar).to.equal("object"); | ||
Machine.destroy(B); | ||
expect(typeof Machine.machines.bar).to.equal('undefined'); | ||
expect(typeof Machine.machines.bar).to.equal("undefined"); | ||
}); | ||
describe('and the machine does not exist', function () { | ||
it('should throw an error', function () { | ||
expect(Machine.destroy.bind(Machine, 'foo')).to.throw('foo'); | ||
describe("and the machine does not exist", function() { | ||
it("should throw an error", function() { | ||
expect(Machine.destroy.bind(Machine, "foo")).to.throw("foo"); | ||
}); | ||
@@ -132,2 +136,1 @@ }); | ||
}); | ||
// errors | ||
export const ERROR_MISSING_MACHINE = name => `There's no machine with name ${ name }`; | ||
export const ERROR_MISSING_STATE = 'Configuration error: missing initial "state"'; | ||
export const ERROR_MISSING_TRANSITIONS = 'Configuration error: missing "transitions"'; | ||
export const ERROR_MISSING_MACHINE = name => | ||
`There's no machine with name ${name}`; | ||
export const ERROR_MISSING_STATE = | ||
'Configuration error: missing initial "state"'; | ||
export const ERROR_MISSING_TRANSITIONS = | ||
'Configuration error: missing "transitions"'; | ||
export const ERROR_WRONG_STATE_FORMAT = state => { | ||
const serialized = typeof state === 'object' ? JSON.stringify(state, null, 2) : state; | ||
const serialized = | ||
typeof state === "object" ? JSON.stringify(state, null, 2) : state; | ||
return `The state should be an object and it should always have at least "name" property. You passed ${ serialized }`; | ||
} | ||
export const ERROR_UNCOVERED_STATE = state => `You just transitioned the machine to a state (${ state }) which is not defined or it has no actions. This means that the machine is stuck.`; | ||
export const ERROR_NOT_SUPPORTED_HANDLER_TYPE = 'Wrong handler type passed. Please read the docs https://github.com/krasimir/stent'; | ||
export const ERROR_RESERVED_WORD_USED_AS_ACTION = word => `Sorry, you can't use ${ word } as a name for an action. It is reserved.`; | ||
return `The state should be an object and it should always have at least "name" property. You passed ${serialized}`; | ||
}; | ||
export const ERROR_UNCOVERED_STATE = state => | ||
`You just transitioned the machine to a state (${state}) which is not defined or it has no actions. This means that the machine is stuck.`; | ||
export const ERROR_NOT_SUPPORTED_HANDLER_TYPE = | ||
"Wrong handler type passed. Please read the docs https://github.com/krasimir/stent"; | ||
export const ERROR_RESERVED_WORD_USED_AS_ACTION = word => | ||
`Sorry, you can't use ${word} as a name for an action. It is reserved.`; | ||
export const ERROR_GENERATOR_FUNC_CALL_FAILED = type => | ||
`The argument passed to \`call\` is falsy (${type})`; | ||
// middlewares | ||
export const MIDDLEWARE_PROCESS_ACTION = 'onActionDispatched'; | ||
export const MIDDLEWARE_ACTION_PROCESSED = 'onActionProcessed'; | ||
export const MIDDLEWARE_STATE_WILL_CHANGE = 'onStateWillChange'; | ||
export const MIDDLEWARE_PROCESS_STATE_CHANGE = 'onStateChanged'; | ||
export const MIDDLEWARE_GENERATOR_STEP = 'onGeneratorStep'; | ||
export const MIDDLEWARE_GENERATOR_END = 'onGeneratorEnd'; | ||
export const MIDDLEWARE_GENERATOR_RESUMED = 'onGeneratorResumed'; | ||
export const MIDDLEWARE_MACHINE_CREATED = 'onMachineCreated'; | ||
export const MIDDLEWARE_MACHINE_CONNECTED = 'onMachineConnected'; | ||
export const MIDDLEWARE_MACHINE_DISCONNECTED = 'onMachineDisconnected'; | ||
export const MIDDLEWARE_REGISTERED = 'onMiddlewareRegister'; | ||
export const MIDDLEWARE_PROCESS_ACTION = "onActionDispatched"; | ||
export const MIDDLEWARE_ACTION_PROCESSED = "onActionProcessed"; | ||
export const MIDDLEWARE_STATE_WILL_CHANGE = "onStateWillChange"; | ||
export const MIDDLEWARE_PROCESS_STATE_CHANGE = "onStateChanged"; | ||
export const MIDDLEWARE_GENERATOR_STEP = "onGeneratorStep"; | ||
export const MIDDLEWARE_GENERATOR_END = "onGeneratorEnd"; | ||
export const MIDDLEWARE_GENERATOR_RESUMED = "onGeneratorResumed"; | ||
export const MIDDLEWARE_MACHINE_CREATED = "onMachineCreated"; | ||
export const MIDDLEWARE_MACHINE_CONNECTED = "onMachineConnected"; | ||
export const MIDDLEWARE_MACHINE_DISCONNECTED = "onMachineDisconnected"; | ||
export const MIDDLEWARE_REGISTERED = "onMiddlewareRegister"; | ||
// misc | ||
export const DEVTOOLS_KEY = '__hello__stent__'; | ||
export const DEVTOOLS_KEY = "__hello__stent__"; |
@@ -1,6 +0,16 @@ | ||
import handleMiddleware from './handleMiddleware'; | ||
import { MIDDLEWARE_GENERATOR_STEP, MIDDLEWARE_GENERATOR_END, MIDDLEWARE_GENERATOR_RESUMED } from '../constants'; | ||
import updateState from './updateState'; | ||
import handleMiddleware from "./handleMiddleware"; | ||
import { | ||
MIDDLEWARE_GENERATOR_STEP, | ||
MIDDLEWARE_GENERATOR_END, | ||
MIDDLEWARE_GENERATOR_RESUMED, | ||
ERROR_GENERATOR_FUNC_CALL_FAILED | ||
} from "../constants"; | ||
import updateState from "./updateState"; | ||
export default function handleGenerator(machine, generator, done, resultOfPreviousOperation) { | ||
export default function handleGenerator( | ||
machine, | ||
generator, | ||
done, | ||
resultOfPreviousOperation | ||
) { | ||
const generatorNext = (gen, res) => !canceled && gen.next(res); | ||
@@ -11,9 +21,9 @@ const generatorThrow = (gen, error) => !canceled && gen.throw(error); | ||
canceled = true; | ||
} | ||
}; | ||
var canceled = false; | ||
var cancelInsideGenerator; | ||
const iterate = function (result) { | ||
const iterate = function(result) { | ||
if (canceled) return; | ||
if (!result.done) { | ||
@@ -23,4 +33,11 @@ handleMiddleware(MIDDLEWARE_GENERATOR_STEP, machine, result.value); | ||
// yield call | ||
if (typeof result.value === 'object' && result.value.__type === 'call') { | ||
if (typeof result.value === "object" && result.value.__type === "call") { | ||
const { func, args } = result.value; | ||
if (!func) { | ||
const error = ERROR_GENERATOR_FUNC_CALL_FAILED(typeof func); | ||
handleMiddleware(MIDDLEWARE_GENERATOR_RESUMED, machine, error); | ||
return iterate(generatorThrow(generator, new Error(error))); | ||
} | ||
const funcResult = func(...args); | ||
@@ -33,5 +50,5 @@ | ||
} | ||
// promise | ||
if (typeof funcResult.then !== 'undefined') { | ||
if (typeof funcResult.then !== "undefined") { | ||
funcResult.then( | ||
@@ -47,9 +64,17 @@ result => { | ||
); | ||
// generator | ||
} else if (typeof funcResult.next === 'function') { | ||
// generator | ||
} else if (typeof funcResult.next === "function") { | ||
try { | ||
cancelInsideGenerator = handleGenerator(machine, funcResult, generatorResult => { | ||
handleMiddleware(MIDDLEWARE_GENERATOR_RESUMED, machine, generatorResult); | ||
iterate(generatorNext(generator, generatorResult)); | ||
}); | ||
cancelInsideGenerator = handleGenerator( | ||
machine, | ||
funcResult, | ||
generatorResult => { | ||
handleMiddleware( | ||
MIDDLEWARE_GENERATOR_RESUMED, | ||
machine, | ||
generatorResult | ||
); | ||
iterate(generatorNext(generator, generatorResult)); | ||
} | ||
); | ||
} catch (error) { | ||
@@ -63,3 +88,3 @@ return iterate(generatorThrow(generator, error)); | ||
// a return statement of the normal function | ||
// a return statement of the normal function | ||
} else { | ||
@@ -70,4 +95,4 @@ updateState(machine, result.value); | ||
} | ||
// the end of the generator (return statement) | ||
// the end of the generator (return statement) | ||
} else { | ||
@@ -82,2 +107,2 @@ handleMiddleware(MIDDLEWARE_GENERATOR_END, machine, result.value); | ||
return cancelGenerator; | ||
} | ||
} |
@@ -15,3 +15,3 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.stent = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var ERROR_WRONG_STATE_FORMAT = exports.ERROR_WRONG_STATE_FORMAT = function ERROR_WRONG_STATE_FORMAT(state) { | ||
var serialized = (typeof state === 'undefined' ? 'undefined' : _typeof(state)) === 'object' ? JSON.stringify(state, null, 2) : state; | ||
var serialized = (typeof state === 'undefined' ? 'undefined' : _typeof(state)) === "object" ? JSON.stringify(state, null, 2) : state; | ||
@@ -23,22 +23,25 @@ return 'The state should be an object and it should always have at least "name" property. You passed ' + serialized; | ||
}; | ||
var ERROR_NOT_SUPPORTED_HANDLER_TYPE = exports.ERROR_NOT_SUPPORTED_HANDLER_TYPE = 'Wrong handler type passed. Please read the docs https://github.com/krasimir/stent'; | ||
var ERROR_NOT_SUPPORTED_HANDLER_TYPE = exports.ERROR_NOT_SUPPORTED_HANDLER_TYPE = "Wrong handler type passed. Please read the docs https://github.com/krasimir/stent"; | ||
var ERROR_RESERVED_WORD_USED_AS_ACTION = exports.ERROR_RESERVED_WORD_USED_AS_ACTION = function ERROR_RESERVED_WORD_USED_AS_ACTION(word) { | ||
return 'Sorry, you can\'t use ' + word + ' as a name for an action. It is reserved.'; | ||
}; | ||
var ERROR_GENERATOR_FUNC_CALL_FAILED = exports.ERROR_GENERATOR_FUNC_CALL_FAILED = function ERROR_GENERATOR_FUNC_CALL_FAILED(type) { | ||
return 'The argument passed to `call` is falsy (' + type + ')'; | ||
}; | ||
// middlewares | ||
var MIDDLEWARE_PROCESS_ACTION = exports.MIDDLEWARE_PROCESS_ACTION = 'onActionDispatched'; | ||
var MIDDLEWARE_ACTION_PROCESSED = exports.MIDDLEWARE_ACTION_PROCESSED = 'onActionProcessed'; | ||
var MIDDLEWARE_STATE_WILL_CHANGE = exports.MIDDLEWARE_STATE_WILL_CHANGE = 'onStateWillChange'; | ||
var MIDDLEWARE_PROCESS_STATE_CHANGE = exports.MIDDLEWARE_PROCESS_STATE_CHANGE = 'onStateChanged'; | ||
var MIDDLEWARE_GENERATOR_STEP = exports.MIDDLEWARE_GENERATOR_STEP = 'onGeneratorStep'; | ||
var MIDDLEWARE_GENERATOR_END = exports.MIDDLEWARE_GENERATOR_END = 'onGeneratorEnd'; | ||
var MIDDLEWARE_GENERATOR_RESUMED = exports.MIDDLEWARE_GENERATOR_RESUMED = 'onGeneratorResumed'; | ||
var MIDDLEWARE_MACHINE_CREATED = exports.MIDDLEWARE_MACHINE_CREATED = 'onMachineCreated'; | ||
var MIDDLEWARE_MACHINE_CONNECTED = exports.MIDDLEWARE_MACHINE_CONNECTED = 'onMachineConnected'; | ||
var MIDDLEWARE_MACHINE_DISCONNECTED = exports.MIDDLEWARE_MACHINE_DISCONNECTED = 'onMachineDisconnected'; | ||
var MIDDLEWARE_REGISTERED = exports.MIDDLEWARE_REGISTERED = 'onMiddlewareRegister'; | ||
var MIDDLEWARE_PROCESS_ACTION = exports.MIDDLEWARE_PROCESS_ACTION = "onActionDispatched"; | ||
var MIDDLEWARE_ACTION_PROCESSED = exports.MIDDLEWARE_ACTION_PROCESSED = "onActionProcessed"; | ||
var MIDDLEWARE_STATE_WILL_CHANGE = exports.MIDDLEWARE_STATE_WILL_CHANGE = "onStateWillChange"; | ||
var MIDDLEWARE_PROCESS_STATE_CHANGE = exports.MIDDLEWARE_PROCESS_STATE_CHANGE = "onStateChanged"; | ||
var MIDDLEWARE_GENERATOR_STEP = exports.MIDDLEWARE_GENERATOR_STEP = "onGeneratorStep"; | ||
var MIDDLEWARE_GENERATOR_END = exports.MIDDLEWARE_GENERATOR_END = "onGeneratorEnd"; | ||
var MIDDLEWARE_GENERATOR_RESUMED = exports.MIDDLEWARE_GENERATOR_RESUMED = "onGeneratorResumed"; | ||
var MIDDLEWARE_MACHINE_CREATED = exports.MIDDLEWARE_MACHINE_CREATED = "onMachineCreated"; | ||
var MIDDLEWARE_MACHINE_CONNECTED = exports.MIDDLEWARE_MACHINE_CONNECTED = "onMachineConnected"; | ||
var MIDDLEWARE_MACHINE_DISCONNECTED = exports.MIDDLEWARE_MACHINE_DISCONNECTED = "onMachineDisconnected"; | ||
var MIDDLEWARE_REGISTERED = exports.MIDDLEWARE_REGISTERED = "onMiddlewareRegister"; | ||
// misc | ||
var DEVTOOLS_KEY = exports.DEVTOOLS_KEY = '__hello__stent__'; | ||
var DEVTOOLS_KEY = exports.DEVTOOLS_KEY = "__hello__stent__"; | ||
},{}],2:[function(require,module,exports){ | ||
@@ -343,3 +346,3 @@ 'use strict'; | ||
},{"./handleAction":5}],7:[function(require,module,exports){ | ||
'use strict'; | ||
"use strict"; | ||
@@ -352,9 +355,9 @@ exports.__esModule = true; | ||
var _handleMiddleware = require('./handleMiddleware'); | ||
var _handleMiddleware = require("./handleMiddleware"); | ||
var _handleMiddleware2 = _interopRequireDefault(_handleMiddleware); | ||
var _constants = require('../constants'); | ||
var _constants = require("../constants"); | ||
var _updateState = require('./updateState'); | ||
var _updateState = require("./updateState"); | ||
@@ -386,3 +389,3 @@ var _updateState2 = _interopRequireDefault(_updateState); | ||
// yield call | ||
if (_typeof(result.value) === 'object' && result.value.__type === 'call') { | ||
if (_typeof(result.value) === "object" && result.value.__type === "call") { | ||
var _result$value = result.value, | ||
@@ -392,2 +395,9 @@ func = _result$value.func, | ||
if (!func) { | ||
var error = (0, _constants.ERROR_GENERATOR_FUNC_CALL_FAILED)(typeof func === "undefined" ? "undefined" : _typeof(func)); | ||
(0, _handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED, machine, error); | ||
return iterate(generatorThrow(generator, new Error(error))); | ||
} | ||
var funcResult = func.apply(undefined, args); | ||
@@ -402,3 +412,3 @@ | ||
// promise | ||
if (typeof funcResult.then !== 'undefined') { | ||
if (typeof funcResult.then !== "undefined") { | ||
funcResult.then(function (result) { | ||
@@ -412,3 +422,3 @@ (0, _handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED, machine, result); | ||
// generator | ||
} else if (typeof funcResult.next === 'function') { | ||
} else if (typeof funcResult.next === "function") { | ||
try { | ||
@@ -415,0 +425,0 @@ cancelInsideGenerator = handleGenerator(machine, funcResult, function (generatorResult) { |
@@ -1,1 +0,1 @@ | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.stent=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";exports.__esModule=true;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};var ERROR_MISSING_MACHINE=exports.ERROR_MISSING_MACHINE=function ERROR_MISSING_MACHINE(name){return"There's no machine with name "+name};var ERROR_MISSING_STATE=exports.ERROR_MISSING_STATE='Configuration error: missing initial "state"';var ERROR_MISSING_TRANSITIONS=exports.ERROR_MISSING_TRANSITIONS='Configuration error: missing "transitions"';var ERROR_WRONG_STATE_FORMAT=exports.ERROR_WRONG_STATE_FORMAT=function ERROR_WRONG_STATE_FORMAT(state){var serialized=(typeof state==="undefined"?"undefined":_typeof(state))==="object"?JSON.stringify(state,null,2):state;return'The state should be an object and it should always have at least "name" property. You passed '+serialized};var ERROR_UNCOVERED_STATE=exports.ERROR_UNCOVERED_STATE=function ERROR_UNCOVERED_STATE(state){return"You just transitioned the machine to a state ("+state+") which is not defined or it has no actions. This means that the machine is stuck."};var ERROR_NOT_SUPPORTED_HANDLER_TYPE=exports.ERROR_NOT_SUPPORTED_HANDLER_TYPE="Wrong handler type passed. Please read the docs https://github.com/krasimir/stent";var ERROR_RESERVED_WORD_USED_AS_ACTION=exports.ERROR_RESERVED_WORD_USED_AS_ACTION=function ERROR_RESERVED_WORD_USED_AS_ACTION(word){return"Sorry, you can't use "+word+" as a name for an action. It is reserved."};var MIDDLEWARE_PROCESS_ACTION=exports.MIDDLEWARE_PROCESS_ACTION="onActionDispatched";var MIDDLEWARE_ACTION_PROCESSED=exports.MIDDLEWARE_ACTION_PROCESSED="onActionProcessed";var MIDDLEWARE_STATE_WILL_CHANGE=exports.MIDDLEWARE_STATE_WILL_CHANGE="onStateWillChange";var MIDDLEWARE_PROCESS_STATE_CHANGE=exports.MIDDLEWARE_PROCESS_STATE_CHANGE="onStateChanged";var MIDDLEWARE_GENERATOR_STEP=exports.MIDDLEWARE_GENERATOR_STEP="onGeneratorStep";var MIDDLEWARE_GENERATOR_END=exports.MIDDLEWARE_GENERATOR_END="onGeneratorEnd";var MIDDLEWARE_GENERATOR_RESUMED=exports.MIDDLEWARE_GENERATOR_RESUMED="onGeneratorResumed";var MIDDLEWARE_MACHINE_CREATED=exports.MIDDLEWARE_MACHINE_CREATED="onMachineCreated";var MIDDLEWARE_MACHINE_CONNECTED=exports.MIDDLEWARE_MACHINE_CONNECTED="onMachineConnected";var MIDDLEWARE_MACHINE_DISCONNECTED=exports.MIDDLEWARE_MACHINE_DISCONNECTED="onMachineDisconnected";var MIDDLEWARE_REGISTERED=exports.MIDDLEWARE_REGISTERED="onMiddlewareRegister";var DEVTOOLS_KEY=exports.DEVTOOLS_KEY="__hello__stent__"},{}],2:[function(require,module,exports){"use strict";exports.__esModule=true;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};exports.default=createMachine;var _handleAction=require("./helpers/handleAction");var _handleAction2=_interopRequireDefault(_handleAction);var _handleActionLatest=require("./helpers/handleActionLatest");var _handleActionLatest2=_interopRequireDefault(_handleActionLatest);var _validateConfig=require("./helpers/validateConfig");var _validateConfig2=_interopRequireDefault(_validateConfig);var _registerMethods=require("./helpers/registerMethods");var _registerMethods2=_interopRequireDefault(_registerMethods);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var IDX=0;var getMachineID=function getMachineID(){return"_@@@"+ ++IDX};function createMachine(name,config){if((typeof name==="undefined"?"undefined":_typeof(name))==="object"){if(typeof config==="undefined"){config=name;name=getMachineID()}else{config={state:name,transitions:config};name=getMachineID()}}var machine={name:name};(0,_validateConfig2.default)(config);var _config=config,initialState=_config.state,transitions=_config.transitions;var dispatch=function dispatch(action){for(var _len=arguments.length,payload=Array(_len>1?_len-1:0),_key=1;_key<_len;_key++){payload[_key-1]=arguments[_key]}return _handleAction2.default.apply(undefined,[machine,action].concat(payload))};var dispatchLatest=function dispatchLatest(action){for(var _len2=arguments.length,payload=Array(_len2>1?_len2-1:0),_key2=1;_key2<_len2;_key2++){payload[_key2-1]=arguments[_key2]}return _handleActionLatest2.default.apply(undefined,[machine,action].concat(payload))};machine.state=initialState;machine.transitions=transitions;(0,_registerMethods2.default)(machine,transitions,dispatch,dispatchLatest);return machine}module.exports=exports["default"]},{"./helpers/handleAction":5,"./helpers/handleActionLatest":6,"./helpers/registerMethods":10,"./helpers/validateConfig":13}],3:[function(require,module,exports){"use strict";exports.__esModule=true;exports.flush=flush;exports.getMapping=getMapping;exports.destroy=destroy;exports.default=connect;var _=require("../");var _handleMiddleware=require("./handleMiddleware");var _handleMiddleware2=_interopRequireDefault(_handleMiddleware);var _constants=require("../constants");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var idIndex=0;var mappings=null;var getId=function getId(){return"m"+ ++idIndex};var setup=function setup(){if(mappings!==null)return;mappings={};_.Machine.addMiddleware({onStateChanged:function onStateChanged(){for(var id in mappings){var _mappings$id=mappings[id],done=_mappings$id.done,machines=_mappings$id.machines;if(machines.map(function(m){return m.name}).indexOf(this.name)>=0){done&&done.apply(undefined,machines)}}}})};function flush(){mappings=null}function getMapping(){return mappings}function destroy(machineId){for(var mId in mappings){mappings[mId].machines=mappings[mId].machines.filter(function(_ref){var name=_ref.name;return name!==machineId});(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_MACHINE_DISCONNECTED,null,mappings[mId].machines);if(mappings[mId].machines.length===0){delete mappings[mId]}}}function connect(){var _ref2=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{},meta=_ref2.meta;setup();var withFunc=function withFunc(){for(var _len=arguments.length,names=Array(_len),_key=0;_key<_len;_key++){names[_key]=arguments[_key]}var machines=names.map(function(name){return _.Machine.get(name)});var mapFunc=function mapFunc(done,once,silent){var id=getId();!once&&(mappings[id]={done:done,machines:machines});!silent&&done&&done.apply(undefined,machines);return function disconnect(){(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_MACHINE_DISCONNECTED,null,machines,meta);if(mappings&&mappings[id])delete mappings[id]}};(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_MACHINE_CONNECTED,null,machines,meta);return{map:mapFunc,mapOnce:function mapOnce(done){return mapFunc(done,true)},mapSilent:function mapSilent(done){return mapFunc(done,false,true)}}};return{with:withFunc}}},{"../":15,"../constants":1,"./handleMiddleware":8}],4:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=call;function call(func){for(var _len=arguments.length,args=Array(_len>1?_len-1:0),_key=1;_key<_len;_key++){args[_key-1]=arguments[_key]}return{__type:"call",func:func,args:args}}module.exports=exports["default"]},{}],5:[function(require,module,exports){"use strict";exports.__esModule=true;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};var _extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key]}}}return target};exports.default=handleAction;var _constants=require("../constants");var _updateState=require("./updateState");var _updateState2=_interopRequireDefault(_updateState);var _handleMiddleware=require("./handleMiddleware");var _handleMiddleware2=_interopRequireDefault(_handleMiddleware);var _handleGenerator=require("./handleGenerator");var _handleGenerator2=_interopRequireDefault(_handleGenerator);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function handleAction(machine,action){for(var _len=arguments.length,payload=Array(_len>2?_len-2:0),_key=2;_key<_len;_key++){payload[_key-2]=arguments[_key]}var state=machine.state,transitions=machine.transitions;if(!transitions[state.name])return false;var handler=transitions[state.name][action];if(typeof handler==="undefined")return false;_handleMiddleware2.default.apply(undefined,[_constants.MIDDLEWARE_PROCESS_ACTION,machine,action].concat(payload));if(typeof handler==="string"){(0,_updateState2.default)(machine,_extends({},state,{name:transitions[state.name][action]}))}else if((typeof handler==="undefined"?"undefined":_typeof(handler))==="object"){(0,_updateState2.default)(machine,handler)}else if(typeof handler==="function"){var _transitions$state$na;var response=(_transitions$state$na=transitions[state.name])[action].apply(_transitions$state$na,[machine].concat(payload));if(response&&typeof response.next==="function"){var generator=response;return(0,_handleGenerator2.default)(machine,generator,function(response){(0,_updateState2.default)(machine,response);_handleMiddleware2.default.apply(undefined,[_constants.MIDDLEWARE_ACTION_PROCESSED,machine,action].concat(payload))})}else{(0,_updateState2.default)(machine,response)}}else{throw new Error(_constants.ERROR_NOT_SUPPORTED_HANDLER_TYPE)}_handleMiddleware2.default.apply(undefined,[_constants.MIDDLEWARE_ACTION_PROCESSED,machine,action].concat(payload))}module.exports=exports["default"]},{"../constants":1,"./handleGenerator":7,"./handleMiddleware":8,"./updateState":12}],6:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=handleActionLatest;var _handleAction=require("./handleAction");var _handleAction2=_interopRequireDefault(_handleAction);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var actions={};function handleActionLatest(machine,action){actions[action]&&actions[action]();for(var _len=arguments.length,payload=Array(_len>2?_len-2:0),_key=2;_key<_len;_key++){payload[_key-2]=arguments[_key]}actions[action]=_handleAction2.default.apply(undefined,[machine,action].concat(payload))}module.exports=exports["default"]},{"./handleAction":5}],7:[function(require,module,exports){"use strict";exports.__esModule=true;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};exports.default=handleGenerator;var _handleMiddleware=require("./handleMiddleware");var _handleMiddleware2=_interopRequireDefault(_handleMiddleware);var _constants=require("../constants");var _updateState=require("./updateState");var _updateState2=_interopRequireDefault(_updateState);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function handleGenerator(machine,generator,done,resultOfPreviousOperation){var generatorNext=function generatorNext(gen,res){return!canceled&&gen.next(res)};var generatorThrow=function generatorThrow(gen,error){return!canceled&&gen.throw(error)};var cancelGenerator=function cancelGenerator(){cancelInsideGenerator&&cancelInsideGenerator();canceled=true};var canceled=false;var cancelInsideGenerator;var iterate=function iterate(result){if(canceled)return;if(!result.done){(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_STEP,machine,result.value);if(_typeof(result.value)==="object"&&result.value.__type==="call"){var _result$value=result.value,func=_result$value.func,args=_result$value.args;var funcResult=func.apply(undefined,args);if(!funcResult){(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine);iterate(generatorNext(generator));return}if(typeof funcResult.then!=="undefined"){funcResult.then(function(result){(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine,result);return iterate(generatorNext(generator,result))},function(error){(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine,error);return iterate(generatorThrow(generator,error))})}else if(typeof funcResult.next==="function"){try{cancelInsideGenerator=handleGenerator(machine,funcResult,function(generatorResult){(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine,generatorResult);iterate(generatorNext(generator,generatorResult))})}catch(error){return iterate(generatorThrow(generator,error))}}else{(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine,funcResult);iterate(generatorNext(generator,funcResult))}}else{(0,_updateState2.default)(machine,result.value);(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine);iterate(generatorNext(generator))}}else{(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_END,machine,result.value);done(result.value)}};iterate(generatorNext(generator,resultOfPreviousOperation));return cancelGenerator}module.exports=exports["default"]},{"../constants":1,"./handleMiddleware":8,"./updateState":12}],8:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=handleMiddleware;var _=require("../");function handleMiddleware(hook,machine){for(var _len=arguments.length,args=Array(_len>2?_len-2:0),_key=2;_key<_len;_key++){args[_key-2]=arguments[_key]}var middlewares=_.Machine.middlewares;if(middlewares.length===0){return}var loop=function loop(index,process){return index<middlewares.length-1?process(index+1):null};(function process(index){var middleware=middlewares[index];if(middleware&&typeof middleware[hook]!=="undefined"){middleware[hook].apply(machine,args)}loop(index,process)})(0)}module.exports=exports["default"]},{"../":15}],9:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=isEmptyObject;function isEmptyObject(obj){var name;for(name in obj){if(obj.hasOwnProperty(name))return false}return true}module.exports=exports["default"]},{}],10:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=registerMethods;var _toCamelCase=require("./toCamelCase");var _toCamelCase2=_interopRequireDefault(_toCamelCase);var _constants=require("../constants");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var reserved=["name","transitions","state","destroy"];function registerMethods(machine,transitions,dispatch,dispatchLatest){for(var state in transitions){(function(state){machine[(0,_toCamelCase2.default)("is "+state)]=function(){return machine.state.name===state}})(state);for(var action in transitions[state]){var normalized=(0,_toCamelCase2.default)(action);var normalizedAllowed=(0,_toCamelCase2.default)("is "+action+" allowed");if(reserved.indexOf(normalized)>=0){throw new Error((0,_constants.ERROR_RESERVED_WORD_USED_AS_ACTION)(normalized))}(function(n,na,a){machine[n]=function(){for(var _len=arguments.length,payload=Array(_len),_key=0;_key<_len;_key++){payload[_key]=arguments[_key]}return dispatch.apply(undefined,[a].concat(payload))};machine[n].latest=function(){for(var _len2=arguments.length,payload=Array(_len2),_key2=0;_key2<_len2;_key2++){payload[_key2]=arguments[_key2]}return dispatchLatest.apply(undefined,[a].concat(payload))};machine[na]=function(){return!transitions[machine.state.name]||typeof transitions[machine.state.name][a]!=="undefined"}})(normalized,normalizedAllowed,action)}}}module.exports=exports["default"]},{"../constants":1,"./toCamelCase":11}],11:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=function(text){return text.split(/\W+/g).reduce(function(result,word,idx){if(idx===0){word=word.charAt(0).toLowerCase()+word.substr(1)}else{word=word.charAt(0).toUpperCase()+word.substr(1)}result+=word;return result},"")};module.exports=exports["default"]},{}],12:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=updateState;var _validateState=require("./validateState");var _validateState2=_interopRequireDefault(_validateState);var _isEmptyObject=require("./isEmptyObject");var _isEmptyObject2=_interopRequireDefault(_isEmptyObject);var _handleMiddleware=require("./handleMiddleware");var _handleMiddleware2=_interopRequireDefault(_handleMiddleware);var _constants=require("../constants");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function updateState(machine,state){var newState;if(typeof state==="undefined")return;if(typeof state==="string"||typeof state==="number"){newState={name:state.toString()}}else{newState=(0,_validateState2.default)(state)}if(typeof machine.transitions[newState.name]==="undefined"||(0,_isEmptyObject2.default)(machine.transitions[newState.name])){throw new Error((0,_constants.ERROR_UNCOVERED_STATE)(newState.name))}(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_STATE_WILL_CHANGE,machine);machine.state=newState;(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_PROCESS_STATE_CHANGE,machine)}module.exports=exports["default"]},{"../constants":1,"./handleMiddleware":8,"./isEmptyObject":9,"./validateState":14}],13:[function(require,module,exports){"use strict";exports.__esModule=true;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};exports.default=validateConfig;var _constants=require("../constants");function validateConfig(config){if((typeof config==="undefined"?"undefined":_typeof(config))!=="object")throw new Error(_constants.ERROR_MISSING_STATE);var state=config.state,transitions=config.transitions;if((typeof state==="undefined"?"undefined":_typeof(state))!=="object")throw new Error(_constants.ERROR_MISSING_STATE);if((typeof transitions==="undefined"?"undefined":_typeof(transitions))!=="object")throw new Error(_constants.ERROR_MISSING_TRANSITIONS);return true}module.exports=exports["default"]},{"../constants":1}],14:[function(require,module,exports){"use strict";exports.__esModule=true;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};exports.default=validateState;var _constants=require("../constants");function validateState(state){if(state&&(typeof state==="undefined"?"undefined":_typeof(state))==="object"&&typeof state.name!=="undefined")return state;throw new Error((0,_constants.ERROR_WRONG_STATE_FORMAT)(state))}module.exports=exports["default"]},{"../constants":1}],15:[function(require,module,exports){"use strict";exports.__esModule=true;exports.Machine=undefined;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};var _createMachine=require("./createMachine");var _createMachine2=_interopRequireDefault(_createMachine);var _constants=require("./constants");var _connect=require("./helpers/connect");var _connect2=_interopRequireDefault(_connect);var _call=require("./helpers/generators/call");var _call2=_interopRequireDefault(_call);var _handleMiddleware=require("./helpers/handleMiddleware");var _handleMiddleware2=_interopRequireDefault(_handleMiddleware);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}var MachineFactory=function(){function MachineFactory(){_classCallCheck(this,MachineFactory);this.machines={};this.middlewares=[];this.connect=_connect2.default;this.call=_call2.default}MachineFactory.prototype.create=function create(name,config){var _this=this;var machine=(0,_createMachine2.default)(name,config,this.middlewares);this.machines[machine.name]=machine;(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_MACHINE_CREATED,machine,machine);machine.destroy=function(){return _this.destroy(machine)};return machine};MachineFactory.prototype.get=function get(name){if((typeof name==="undefined"?"undefined":_typeof(name))==="object")name=name.name;if(this.machines[name])return this.machines[name];throw new Error((0,_constants.ERROR_MISSING_MACHINE)(name))};MachineFactory.prototype.flush=function flush(){this.machines={};this.middlewares=[];(0,_connect.flush)()};MachineFactory.prototype.addMiddleware=function addMiddleware(middleware){if(Array.isArray(middleware)){this.middlewares=this.middlewares.concat(middleware)}else{this.middlewares.push(middleware)}if(middleware.__initialize)middleware.__initialize(this);if(middleware[_constants.MIDDLEWARE_REGISTERED])middleware[_constants.MIDDLEWARE_REGISTERED]()};MachineFactory.prototype.destroy=function destroy(machine){var m=machine;if(typeof machine==="string"){m=this.machines[machine];if(!m)throw new Error((0,_constants.ERROR_MISSING_MACHINE)(machine))}delete this.machines[m.name];(0,_connect.destroy)(m.name)};return MachineFactory}();var factory=new MachineFactory;exports.Machine=factory;if(typeof window!=="undefined"){window[_constants.DEVTOOLS_KEY]=factory}},{"./constants":1,"./createMachine":2,"./helpers/connect":3,"./helpers/generators/call":4,"./helpers/handleMiddleware":8}]},{},[15])(15)}); | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.stent=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";exports.__esModule=true;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};var ERROR_MISSING_MACHINE=exports.ERROR_MISSING_MACHINE=function ERROR_MISSING_MACHINE(name){return"There's no machine with name "+name};var ERROR_MISSING_STATE=exports.ERROR_MISSING_STATE='Configuration error: missing initial "state"';var ERROR_MISSING_TRANSITIONS=exports.ERROR_MISSING_TRANSITIONS='Configuration error: missing "transitions"';var ERROR_WRONG_STATE_FORMAT=exports.ERROR_WRONG_STATE_FORMAT=function ERROR_WRONG_STATE_FORMAT(state){var serialized=(typeof state==="undefined"?"undefined":_typeof(state))==="object"?JSON.stringify(state,null,2):state;return'The state should be an object and it should always have at least "name" property. You passed '+serialized};var ERROR_UNCOVERED_STATE=exports.ERROR_UNCOVERED_STATE=function ERROR_UNCOVERED_STATE(state){return"You just transitioned the machine to a state ("+state+") which is not defined or it has no actions. This means that the machine is stuck."};var ERROR_NOT_SUPPORTED_HANDLER_TYPE=exports.ERROR_NOT_SUPPORTED_HANDLER_TYPE="Wrong handler type passed. Please read the docs https://github.com/krasimir/stent";var ERROR_RESERVED_WORD_USED_AS_ACTION=exports.ERROR_RESERVED_WORD_USED_AS_ACTION=function ERROR_RESERVED_WORD_USED_AS_ACTION(word){return"Sorry, you can't use "+word+" as a name for an action. It is reserved."};var ERROR_GENERATOR_FUNC_CALL_FAILED=exports.ERROR_GENERATOR_FUNC_CALL_FAILED=function ERROR_GENERATOR_FUNC_CALL_FAILED(type){return"The argument passed to `call` is falsy ("+type+")"};var MIDDLEWARE_PROCESS_ACTION=exports.MIDDLEWARE_PROCESS_ACTION="onActionDispatched";var MIDDLEWARE_ACTION_PROCESSED=exports.MIDDLEWARE_ACTION_PROCESSED="onActionProcessed";var MIDDLEWARE_STATE_WILL_CHANGE=exports.MIDDLEWARE_STATE_WILL_CHANGE="onStateWillChange";var MIDDLEWARE_PROCESS_STATE_CHANGE=exports.MIDDLEWARE_PROCESS_STATE_CHANGE="onStateChanged";var MIDDLEWARE_GENERATOR_STEP=exports.MIDDLEWARE_GENERATOR_STEP="onGeneratorStep";var MIDDLEWARE_GENERATOR_END=exports.MIDDLEWARE_GENERATOR_END="onGeneratorEnd";var MIDDLEWARE_GENERATOR_RESUMED=exports.MIDDLEWARE_GENERATOR_RESUMED="onGeneratorResumed";var MIDDLEWARE_MACHINE_CREATED=exports.MIDDLEWARE_MACHINE_CREATED="onMachineCreated";var MIDDLEWARE_MACHINE_CONNECTED=exports.MIDDLEWARE_MACHINE_CONNECTED="onMachineConnected";var MIDDLEWARE_MACHINE_DISCONNECTED=exports.MIDDLEWARE_MACHINE_DISCONNECTED="onMachineDisconnected";var MIDDLEWARE_REGISTERED=exports.MIDDLEWARE_REGISTERED="onMiddlewareRegister";var DEVTOOLS_KEY=exports.DEVTOOLS_KEY="__hello__stent__"},{}],2:[function(require,module,exports){"use strict";exports.__esModule=true;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};exports.default=createMachine;var _handleAction=require("./helpers/handleAction");var _handleAction2=_interopRequireDefault(_handleAction);var _handleActionLatest=require("./helpers/handleActionLatest");var _handleActionLatest2=_interopRequireDefault(_handleActionLatest);var _validateConfig=require("./helpers/validateConfig");var _validateConfig2=_interopRequireDefault(_validateConfig);var _registerMethods=require("./helpers/registerMethods");var _registerMethods2=_interopRequireDefault(_registerMethods);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var IDX=0;var getMachineID=function getMachineID(){return"_@@@"+ ++IDX};function createMachine(name,config){if((typeof name==="undefined"?"undefined":_typeof(name))==="object"){if(typeof config==="undefined"){config=name;name=getMachineID()}else{config={state:name,transitions:config};name=getMachineID()}}var machine={name:name};(0,_validateConfig2.default)(config);var _config=config,initialState=_config.state,transitions=_config.transitions;var dispatch=function dispatch(action){for(var _len=arguments.length,payload=Array(_len>1?_len-1:0),_key=1;_key<_len;_key++){payload[_key-1]=arguments[_key]}return _handleAction2.default.apply(undefined,[machine,action].concat(payload))};var dispatchLatest=function dispatchLatest(action){for(var _len2=arguments.length,payload=Array(_len2>1?_len2-1:0),_key2=1;_key2<_len2;_key2++){payload[_key2-1]=arguments[_key2]}return _handleActionLatest2.default.apply(undefined,[machine,action].concat(payload))};machine.state=initialState;machine.transitions=transitions;(0,_registerMethods2.default)(machine,transitions,dispatch,dispatchLatest);return machine}module.exports=exports["default"]},{"./helpers/handleAction":5,"./helpers/handleActionLatest":6,"./helpers/registerMethods":10,"./helpers/validateConfig":13}],3:[function(require,module,exports){"use strict";exports.__esModule=true;exports.flush=flush;exports.getMapping=getMapping;exports.destroy=destroy;exports.default=connect;var _=require("../");var _handleMiddleware=require("./handleMiddleware");var _handleMiddleware2=_interopRequireDefault(_handleMiddleware);var _constants=require("../constants");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var idIndex=0;var mappings=null;var getId=function getId(){return"m"+ ++idIndex};var setup=function setup(){if(mappings!==null)return;mappings={};_.Machine.addMiddleware({onStateChanged:function onStateChanged(){for(var id in mappings){var _mappings$id=mappings[id],done=_mappings$id.done,machines=_mappings$id.machines;if(machines.map(function(m){return m.name}).indexOf(this.name)>=0){done&&done.apply(undefined,machines)}}}})};function flush(){mappings=null}function getMapping(){return mappings}function destroy(machineId){for(var mId in mappings){mappings[mId].machines=mappings[mId].machines.filter(function(_ref){var name=_ref.name;return name!==machineId});(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_MACHINE_DISCONNECTED,null,mappings[mId].machines);if(mappings[mId].machines.length===0){delete mappings[mId]}}}function connect(){var _ref2=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{},meta=_ref2.meta;setup();var withFunc=function withFunc(){for(var _len=arguments.length,names=Array(_len),_key=0;_key<_len;_key++){names[_key]=arguments[_key]}var machines=names.map(function(name){return _.Machine.get(name)});var mapFunc=function mapFunc(done,once,silent){var id=getId();!once&&(mappings[id]={done:done,machines:machines});!silent&&done&&done.apply(undefined,machines);return function disconnect(){(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_MACHINE_DISCONNECTED,null,machines,meta);if(mappings&&mappings[id])delete mappings[id]}};(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_MACHINE_CONNECTED,null,machines,meta);return{map:mapFunc,mapOnce:function mapOnce(done){return mapFunc(done,true)},mapSilent:function mapSilent(done){return mapFunc(done,false,true)}}};return{with:withFunc}}},{"../":15,"../constants":1,"./handleMiddleware":8}],4:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=call;function call(func){for(var _len=arguments.length,args=Array(_len>1?_len-1:0),_key=1;_key<_len;_key++){args[_key-1]=arguments[_key]}return{__type:"call",func:func,args:args}}module.exports=exports["default"]},{}],5:[function(require,module,exports){"use strict";exports.__esModule=true;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};var _extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key]}}}return target};exports.default=handleAction;var _constants=require("../constants");var _updateState=require("./updateState");var _updateState2=_interopRequireDefault(_updateState);var _handleMiddleware=require("./handleMiddleware");var _handleMiddleware2=_interopRequireDefault(_handleMiddleware);var _handleGenerator=require("./handleGenerator");var _handleGenerator2=_interopRequireDefault(_handleGenerator);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function handleAction(machine,action){for(var _len=arguments.length,payload=Array(_len>2?_len-2:0),_key=2;_key<_len;_key++){payload[_key-2]=arguments[_key]}var state=machine.state,transitions=machine.transitions;if(!transitions[state.name])return false;var handler=transitions[state.name][action];if(typeof handler==="undefined")return false;_handleMiddleware2.default.apply(undefined,[_constants.MIDDLEWARE_PROCESS_ACTION,machine,action].concat(payload));if(typeof handler==="string"){(0,_updateState2.default)(machine,_extends({},state,{name:transitions[state.name][action]}))}else if((typeof handler==="undefined"?"undefined":_typeof(handler))==="object"){(0,_updateState2.default)(machine,handler)}else if(typeof handler==="function"){var _transitions$state$na;var response=(_transitions$state$na=transitions[state.name])[action].apply(_transitions$state$na,[machine].concat(payload));if(response&&typeof response.next==="function"){var generator=response;return(0,_handleGenerator2.default)(machine,generator,function(response){(0,_updateState2.default)(machine,response);_handleMiddleware2.default.apply(undefined,[_constants.MIDDLEWARE_ACTION_PROCESSED,machine,action].concat(payload))})}else{(0,_updateState2.default)(machine,response)}}else{throw new Error(_constants.ERROR_NOT_SUPPORTED_HANDLER_TYPE)}_handleMiddleware2.default.apply(undefined,[_constants.MIDDLEWARE_ACTION_PROCESSED,machine,action].concat(payload))}module.exports=exports["default"]},{"../constants":1,"./handleGenerator":7,"./handleMiddleware":8,"./updateState":12}],6:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=handleActionLatest;var _handleAction=require("./handleAction");var _handleAction2=_interopRequireDefault(_handleAction);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var actions={};function handleActionLatest(machine,action){actions[action]&&actions[action]();for(var _len=arguments.length,payload=Array(_len>2?_len-2:0),_key=2;_key<_len;_key++){payload[_key-2]=arguments[_key]}actions[action]=_handleAction2.default.apply(undefined,[machine,action].concat(payload))}module.exports=exports["default"]},{"./handleAction":5}],7:[function(require,module,exports){"use strict";exports.__esModule=true;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};exports.default=handleGenerator;var _handleMiddleware=require("./handleMiddleware");var _handleMiddleware2=_interopRequireDefault(_handleMiddleware);var _constants=require("../constants");var _updateState=require("./updateState");var _updateState2=_interopRequireDefault(_updateState);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function handleGenerator(machine,generator,done,resultOfPreviousOperation){var generatorNext=function generatorNext(gen,res){return!canceled&&gen.next(res)};var generatorThrow=function generatorThrow(gen,error){return!canceled&&gen.throw(error)};var cancelGenerator=function cancelGenerator(){cancelInsideGenerator&&cancelInsideGenerator();canceled=true};var canceled=false;var cancelInsideGenerator;var iterate=function iterate(result){if(canceled)return;if(!result.done){(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_STEP,machine,result.value);if(_typeof(result.value)==="object"&&result.value.__type==="call"){var _result$value=result.value,func=_result$value.func,args=_result$value.args;if(!func){var error=(0,_constants.ERROR_GENERATOR_FUNC_CALL_FAILED)(typeof func==="undefined"?"undefined":_typeof(func));(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine,error);return iterate(generatorThrow(generator,new Error(error)))}var funcResult=func.apply(undefined,args);if(!funcResult){(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine);iterate(generatorNext(generator));return}if(typeof funcResult.then!=="undefined"){funcResult.then(function(result){(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine,result);return iterate(generatorNext(generator,result))},function(error){(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine,error);return iterate(generatorThrow(generator,error))})}else if(typeof funcResult.next==="function"){try{cancelInsideGenerator=handleGenerator(machine,funcResult,function(generatorResult){(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine,generatorResult);iterate(generatorNext(generator,generatorResult))})}catch(error){return iterate(generatorThrow(generator,error))}}else{(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine,funcResult);iterate(generatorNext(generator,funcResult))}}else{(0,_updateState2.default)(machine,result.value);(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_RESUMED,machine);iterate(generatorNext(generator))}}else{(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_GENERATOR_END,machine,result.value);done(result.value)}};iterate(generatorNext(generator,resultOfPreviousOperation));return cancelGenerator}module.exports=exports["default"]},{"../constants":1,"./handleMiddleware":8,"./updateState":12}],8:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=handleMiddleware;var _=require("../");function handleMiddleware(hook,machine){for(var _len=arguments.length,args=Array(_len>2?_len-2:0),_key=2;_key<_len;_key++){args[_key-2]=arguments[_key]}var middlewares=_.Machine.middlewares;if(middlewares.length===0){return}var loop=function loop(index,process){return index<middlewares.length-1?process(index+1):null};(function process(index){var middleware=middlewares[index];if(middleware&&typeof middleware[hook]!=="undefined"){middleware[hook].apply(machine,args)}loop(index,process)})(0)}module.exports=exports["default"]},{"../":15}],9:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=isEmptyObject;function isEmptyObject(obj){var name;for(name in obj){if(obj.hasOwnProperty(name))return false}return true}module.exports=exports["default"]},{}],10:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=registerMethods;var _toCamelCase=require("./toCamelCase");var _toCamelCase2=_interopRequireDefault(_toCamelCase);var _constants=require("../constants");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var reserved=["name","transitions","state","destroy"];function registerMethods(machine,transitions,dispatch,dispatchLatest){for(var state in transitions){(function(state){machine[(0,_toCamelCase2.default)("is "+state)]=function(){return machine.state.name===state}})(state);for(var action in transitions[state]){var normalized=(0,_toCamelCase2.default)(action);var normalizedAllowed=(0,_toCamelCase2.default)("is "+action+" allowed");if(reserved.indexOf(normalized)>=0){throw new Error((0,_constants.ERROR_RESERVED_WORD_USED_AS_ACTION)(normalized))}(function(n,na,a){machine[n]=function(){for(var _len=arguments.length,payload=Array(_len),_key=0;_key<_len;_key++){payload[_key]=arguments[_key]}return dispatch.apply(undefined,[a].concat(payload))};machine[n].latest=function(){for(var _len2=arguments.length,payload=Array(_len2),_key2=0;_key2<_len2;_key2++){payload[_key2]=arguments[_key2]}return dispatchLatest.apply(undefined,[a].concat(payload))};machine[na]=function(){return!transitions[machine.state.name]||typeof transitions[machine.state.name][a]!=="undefined"}})(normalized,normalizedAllowed,action)}}}module.exports=exports["default"]},{"../constants":1,"./toCamelCase":11}],11:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=function(text){return text.split(/\W+/g).reduce(function(result,word,idx){if(idx===0){word=word.charAt(0).toLowerCase()+word.substr(1)}else{word=word.charAt(0).toUpperCase()+word.substr(1)}result+=word;return result},"")};module.exports=exports["default"]},{}],12:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=updateState;var _validateState=require("./validateState");var _validateState2=_interopRequireDefault(_validateState);var _isEmptyObject=require("./isEmptyObject");var _isEmptyObject2=_interopRequireDefault(_isEmptyObject);var _handleMiddleware=require("./handleMiddleware");var _handleMiddleware2=_interopRequireDefault(_handleMiddleware);var _constants=require("../constants");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function updateState(machine,state){var newState;if(typeof state==="undefined")return;if(typeof state==="string"||typeof state==="number"){newState={name:state.toString()}}else{newState=(0,_validateState2.default)(state)}if(typeof machine.transitions[newState.name]==="undefined"||(0,_isEmptyObject2.default)(machine.transitions[newState.name])){throw new Error((0,_constants.ERROR_UNCOVERED_STATE)(newState.name))}(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_STATE_WILL_CHANGE,machine);machine.state=newState;(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_PROCESS_STATE_CHANGE,machine)}module.exports=exports["default"]},{"../constants":1,"./handleMiddleware":8,"./isEmptyObject":9,"./validateState":14}],13:[function(require,module,exports){"use strict";exports.__esModule=true;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};exports.default=validateConfig;var _constants=require("../constants");function validateConfig(config){if((typeof config==="undefined"?"undefined":_typeof(config))!=="object")throw new Error(_constants.ERROR_MISSING_STATE);var state=config.state,transitions=config.transitions;if((typeof state==="undefined"?"undefined":_typeof(state))!=="object")throw new Error(_constants.ERROR_MISSING_STATE);if((typeof transitions==="undefined"?"undefined":_typeof(transitions))!=="object")throw new Error(_constants.ERROR_MISSING_TRANSITIONS);return true}module.exports=exports["default"]},{"../constants":1}],14:[function(require,module,exports){"use strict";exports.__esModule=true;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};exports.default=validateState;var _constants=require("../constants");function validateState(state){if(state&&(typeof state==="undefined"?"undefined":_typeof(state))==="object"&&typeof state.name!=="undefined")return state;throw new Error((0,_constants.ERROR_WRONG_STATE_FORMAT)(state))}module.exports=exports["default"]},{"../constants":1}],15:[function(require,module,exports){"use strict";exports.__esModule=true;exports.Machine=undefined;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};var _createMachine=require("./createMachine");var _createMachine2=_interopRequireDefault(_createMachine);var _constants=require("./constants");var _connect=require("./helpers/connect");var _connect2=_interopRequireDefault(_connect);var _call=require("./helpers/generators/call");var _call2=_interopRequireDefault(_call);var _handleMiddleware=require("./helpers/handleMiddleware");var _handleMiddleware2=_interopRequireDefault(_handleMiddleware);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}var MachineFactory=function(){function MachineFactory(){_classCallCheck(this,MachineFactory);this.machines={};this.middlewares=[];this.connect=_connect2.default;this.call=_call2.default}MachineFactory.prototype.create=function create(name,config){var _this=this;var machine=(0,_createMachine2.default)(name,config,this.middlewares);this.machines[machine.name]=machine;(0,_handleMiddleware2.default)(_constants.MIDDLEWARE_MACHINE_CREATED,machine,machine);machine.destroy=function(){return _this.destroy(machine)};return machine};MachineFactory.prototype.get=function get(name){if((typeof name==="undefined"?"undefined":_typeof(name))==="object")name=name.name;if(this.machines[name])return this.machines[name];throw new Error((0,_constants.ERROR_MISSING_MACHINE)(name))};MachineFactory.prototype.flush=function flush(){this.machines={};this.middlewares=[];(0,_connect.flush)()};MachineFactory.prototype.addMiddleware=function addMiddleware(middleware){if(Array.isArray(middleware)){this.middlewares=this.middlewares.concat(middleware)}else{this.middlewares.push(middleware)}if(middleware.__initialize)middleware.__initialize(this);if(middleware[_constants.MIDDLEWARE_REGISTERED])middleware[_constants.MIDDLEWARE_REGISTERED]()};MachineFactory.prototype.destroy=function destroy(machine){var m=machine;if(typeof machine==="string"){m=this.machines[machine];if(!m)throw new Error((0,_constants.ERROR_MISSING_MACHINE)(machine))}delete this.machines[m.name];(0,_connect.destroy)(m.name)};return MachineFactory}();var factory=new MachineFactory;exports.Machine=factory;if(typeof window!=="undefined"){window[_constants.DEVTOOLS_KEY]=factory}},{"./constants":1,"./createMachine":2,"./helpers/connect":3,"./helpers/generators/call":4,"./helpers/handleMiddleware":8}]},{},[15])(15)}); |
392400
90
5398