Comparing version 0.0.5 to 0.0.6
@@ -9,2 +9,4 @@ 'use strict'; | ||
var _helpers = require('../helpers'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -41,14 +43,37 @@ | ||
// describe('when the state is invalid', function () { | ||
// it('should throw an error', function () { | ||
// ... | ||
// }); | ||
// }); | ||
describe('when we transition to a state which has no actions inside or it is not defined', function () { | ||
it('should throw an error if there is no such a state defined', function () { | ||
var machine = { | ||
state: { name: 'idle' }, | ||
transitions: { | ||
idle: { | ||
'run': 'running' | ||
} | ||
} | ||
}; | ||
expect(_handleAction2.default.bind(null, machine, 'run')).to.throw((0, _constants.ERROR_UNCOVERED_STATE)('running')); | ||
}); | ||
it('should throw an error if there the state has no actions inside', function () { | ||
var machine = { | ||
state: { name: 'idle' }, | ||
transitions: { | ||
idle: { | ||
'run': 'running' | ||
}, | ||
running: {} | ||
} | ||
}; | ||
expect(_handleAction2.default.bind(null, machine, 'run')).to.throw((0, _constants.ERROR_UNCOVERED_STATE)('running')); | ||
}); | ||
}); | ||
describe('when the handler is a string', function () { | ||
it('should change the state of the machine to that string', function () { | ||
var machine = { | ||
state: { name: 'foo' }, | ||
state: { name: 'idle' }, | ||
transitions: { | ||
foo: { run: 'running' } | ||
idle: { run: 'running' }, | ||
running: { stop: 'idle' } | ||
} | ||
@@ -66,5 +91,6 @@ }; | ||
var machine = { | ||
state: { name: 'foo' }, | ||
state: { name: 'idle' }, | ||
transitions: { | ||
foo: { run: newState } | ||
idle: { run: newState }, | ||
running: { stop: 'idle' } | ||
} | ||
@@ -85,3 +111,4 @@ }; | ||
transitions: { | ||
foo: { run: handler } | ||
foo: { run: handler }, | ||
running: { stop: 'foo' } | ||
} | ||
@@ -100,3 +127,4 @@ }; | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
bar: { a: 'b' } | ||
} | ||
@@ -110,3 +138,3 @@ }; | ||
var handler = function handler(state, payload) { | ||
return 'new-state'; | ||
return 'bar'; | ||
}; | ||
@@ -116,3 +144,4 @@ var machine = { | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
bar: { a: 'b' } | ||
} | ||
@@ -122,3 +151,3 @@ }; | ||
(0, _handleAction2.default)(machine, 'run'); | ||
expect(machine.state).to.deep.equal({ name: 'new-state' }); | ||
expect(machine.state).to.deep.equal({ name: 'bar' }); | ||
}); | ||
@@ -133,3 +162,4 @@ it('should run the handler with the machine as a context', function () { | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
foo: { a: 'b' } | ||
} | ||
@@ -169,3 +199,6 @@ }; | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
foo: { a: 'b' }, | ||
bar: { a: 'b' }, | ||
running: { a: 'b' } | ||
} | ||
@@ -177,3 +210,3 @@ }; | ||
}); | ||
it('should change the state if we yield a string', function () { | ||
it('should change the state if we yield a primitive', function () { | ||
var handler = /*#__PURE__*/regeneratorRuntime.mark(function handler() { | ||
@@ -185,3 +218,3 @@ return regeneratorRuntime.wrap(function handler$(_context2) { | ||
_context2.next = 2; | ||
return 'running'; | ||
return 100; | ||
@@ -198,3 +231,4 @@ case 2: | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
'100': { a: 'b' } | ||
} | ||
@@ -204,3 +238,3 @@ }; | ||
(0, _handleAction2.default)(machine, 'run'); | ||
expect(machine.state.name).to.equal('running'); | ||
expect(machine.state.name).to.equal('100'); | ||
}); | ||
@@ -230,3 +264,5 @@ it('should change the state if we yield an object', function () { | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
running: { a: 'b' }, | ||
jumping: { a: 'b' } | ||
} | ||
@@ -238,3 +274,187 @@ }; | ||
}); | ||
describe('and we use the call helper', function () { | ||
it('should execute the function and return the result', function () { | ||
var api = function api(name) { | ||
return 'hello ' + name; | ||
}; | ||
var handler = /*#__PURE__*/regeneratorRuntime.mark(function handler() { | ||
var newState; | ||
return regeneratorRuntime.wrap(function handler$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
_context4.next = 2; | ||
return (0, _helpers.call)(api, 'stent'); | ||
case 2: | ||
newState = _context4.sent; | ||
return _context4.abrupt('return', newState); | ||
case 4: | ||
case 'end': | ||
return _context4.stop(); | ||
} | ||
} | ||
}, handler, this); | ||
}); | ||
var machine = { | ||
state: { name: 'idle', data: 42 }, | ||
transitions: { | ||
idle: { run: handler }, | ||
'hello stent': 'a' | ||
} | ||
}; | ||
(0, _handleAction2.default)(machine, 'run'); | ||
expect(machine.state).to.deep.equal({ name: 'hello stent' }); | ||
}); | ||
describe('and when the function returns a promise', function () { | ||
it('should return the value of the resolved promise', function () { | ||
var api = function api(name) { | ||
return Promise.resolve('hello ' + name); | ||
}; | ||
var handler = /*#__PURE__*/regeneratorRuntime.mark(function handler() { | ||
var newState; | ||
return regeneratorRuntime.wrap(function handler$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
_context5.next = 2; | ||
return (0, _helpers.call)(api, 'stent'); | ||
case 2: | ||
newState = _context5.sent; | ||
return _context5.abrupt('return', newState); | ||
case 4: | ||
case 'end': | ||
return _context5.stop(); | ||
} | ||
} | ||
}, handler, this); | ||
}); | ||
var machine = { | ||
state: { name: 'idle', data: 42 }, | ||
transitions: { | ||
idle: { run: handler }, | ||
'hello stent': 'a' | ||
} | ||
}; | ||
(0, _handleAction2.default)(machine, 'run'); | ||
return Promise.resolve().then(function () { | ||
expect(machine.state).to.deep.equal({ name: 'hello stent' }); | ||
}); | ||
}); | ||
it('should throw an error if the promise is rejected', function () { | ||
var api = function api(name) { | ||
return Promise.reject('error ' + name); | ||
}; | ||
var handler = /*#__PURE__*/regeneratorRuntime.mark(function handler() { | ||
var _newState; | ||
return regeneratorRuntime.wrap(function handler$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
_context6.prev = 0; | ||
_context6.next = 3; | ||
return (0, _helpers.call)(api, 'stent'); | ||
case 3: | ||
_newState = _context6.sent; | ||
_context6.next = 9; | ||
break; | ||
case 6: | ||
_context6.prev = 6; | ||
_context6.t0 = _context6['catch'](0); | ||
return _context6.abrupt('return', _context6.t0.message); | ||
case 9: | ||
return _context6.abrupt('return', newState); | ||
case 10: | ||
case 'end': | ||
return _context6.stop(); | ||
} | ||
} | ||
}, handler, this, [[0, 6]]); | ||
}); | ||
var machine = { | ||
state: { name: 'idle', data: 42 }, | ||
transitions: { | ||
idle: { run: handler }, | ||
'error stent': 'a' | ||
} | ||
}; | ||
(0, _handleAction2.default)(machine, 'run'); | ||
return Promise.resolve().then(function () { | ||
expect(machine.state).to.deep.equal({ name: 'error stent' }); | ||
}); | ||
}); | ||
}); | ||
describe('when the function returns another generator', function () { | ||
it('should iterate through that inner generator', function () { | ||
var api = /*#__PURE__*/regeneratorRuntime.mark(function api(name) { | ||
return regeneratorRuntime.wrap(function api$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
_context7.next = 2; | ||
return 42; | ||
case 2: | ||
_context7.next = 4; | ||
return (0, _helpers.call)(function () { | ||
return Promise.resolve(name + ': merry christmas'); | ||
}); | ||
case 4: | ||
return _context7.abrupt('return', _context7.sent); | ||
case 5: | ||
case 'end': | ||
return _context7.stop(); | ||
} | ||
} | ||
}, api, this); | ||
}); | ||
var handler = /*#__PURE__*/regeneratorRuntime.mark(function handler() { | ||
return regeneratorRuntime.wrap(function handler$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
_context8.next = 2; | ||
return (0, _helpers.call)(api, 'stent'); | ||
case 2: | ||
return _context8.abrupt('return', _context8.sent); | ||
case 3: | ||
case 'end': | ||
return _context8.stop(); | ||
} | ||
} | ||
}, handler, this); | ||
}); | ||
var machine = { | ||
state: { name: 'idle', data: 42 }, | ||
transitions: { | ||
idle: { run: handler }, | ||
'42': 'a', | ||
'stent: merry christmas': 'b' | ||
} | ||
}; | ||
(0, _handleAction2.default)(machine, 'run'); | ||
return Promise.resolve().then(function () { | ||
expect(machine.state).to.deep.equal({ name: 'stent: merry christmas' }); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -20,2 +20,5 @@ 'use strict'; | ||
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.'; | ||
}; |
@@ -5,2 +5,4 @@ 'use strict'; | ||
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; }; | ||
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; }; | ||
@@ -18,19 +20,66 @@ | ||
function handleGenerator(machine, generator) { | ||
var result = generator.next(); | ||
if (!result.done) { | ||
updateState(machine, result.value); | ||
return handleGenerator(machine, generator); | ||
function isEmptyObject(obj) { | ||
var name; | ||
for (name in obj) { | ||
if (obj.hasOwnProperty(name)) return false; | ||
} | ||
return result.value; | ||
return true; | ||
} | ||
function handleGenerator(machine, generator, done, resultOfPreviousOperation) { | ||
var iterate = function iterate(result) { | ||
if (!result.done) { | ||
// yield call | ||
if (_typeof(result.value) === 'object' && result.value.__type === 'call') { | ||
var _result$value; | ||
var funcResult = (_result$value = result.value).func.apply(_result$value, result.value.args); | ||
// promise | ||
if (typeof funcResult.then !== 'undefined') { | ||
funcResult.then(function (r) { | ||
return iterate(generator.next(r)); | ||
}, function (error) { | ||
return iterate(generator.throw(new Error(error))); | ||
}); | ||
// generator | ||
} else if (typeof funcResult.next === 'function') { | ||
handleGenerator(machine, funcResult, function (generatorResult) { | ||
iterate(generator.next(generatorResult)); | ||
}); | ||
} else { | ||
iterate(generator.next(funcResult)); | ||
} | ||
// the return statement of the normal function | ||
} else { | ||
updateState(machine, result.value); | ||
iterate(generator.next()); | ||
} | ||
// the end of the generator (return statement) | ||
} else { | ||
done(result.value); | ||
} | ||
}; | ||
iterate(generator.next(resultOfPreviousOperation)); | ||
} | ||
function updateState(machine, response) { | ||
var newState; | ||
if (typeof response === 'undefined') return; | ||
if (typeof response === 'string') { | ||
machine.state = { name: response }; | ||
if (typeof response === 'string' || typeof response === 'number') { | ||
newState = { name: response.toString() }; | ||
} else { | ||
machine.state = (0, _validateState2.default)(response); | ||
newState = (0, _validateState2.default)(response); | ||
} | ||
if (typeof machine.transitions[newState.name] === 'undefined' || isEmptyObject(machine.transitions[newState.name])) { | ||
throw new Error((0, _constants.ERROR_UNCOVERED_STATE)(newState.name)); | ||
} | ||
machine.state = newState; | ||
} | ||
@@ -55,7 +104,7 @@ | ||
if (typeof handler === 'string') { | ||
state.name = transitions[state.name][action]; | ||
updateState(machine, _extends({}, state, { name: transitions[state.name][action] })); | ||
// object as a handler | ||
} else if ((typeof handler === 'undefined' ? 'undefined' : _typeof(handler)) === 'object') { | ||
machine.state = (0, _validateState2.default)(handler); | ||
updateState(machine, (0, _validateState2.default)(handler)); | ||
@@ -67,6 +116,8 @@ // function as a handler | ||
if (response && typeof response.next === 'function') { | ||
response = handleGenerator(machine, response); | ||
handleGenerator(machine, response, function (response) { | ||
updateState(machine, response); | ||
}); | ||
} else { | ||
updateState(machine, response); | ||
} | ||
updateState(machine, response); | ||
} | ||
@@ -73,0 +124,0 @@ |
{ | ||
"name": "stent", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Stent is a state machines container made for UI development", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
import handleAction from '../handleAction'; | ||
import { ERROR_MISSING_ACTION_IN_STATE } from '../constants'; | ||
import { ERROR_MISSING_ACTION_IN_STATE, ERROR_UNCOVERED_STATE } from '../constants'; | ||
import { call } from '../helpers'; | ||
@@ -35,6 +35,28 @@ | ||
describe.skip('when we transition to a state which has no actions inside or it is not defined', function () { | ||
it('should throw an error', function () { | ||
// ... | ||
describe('when we transition to a state which has no actions inside or it is not defined', function () { | ||
it('should throw an error if there is no such a state defined', function () { | ||
const machine = { | ||
state: { name: 'idle' }, | ||
transitions: { | ||
idle: { | ||
'run': 'running' | ||
} | ||
} | ||
}; | ||
expect(handleAction.bind(null, machine, 'run')).to.throw(ERROR_UNCOVERED_STATE('running')); | ||
}); | ||
it('should throw an error if there the state has no actions inside', function () { | ||
const machine = { | ||
state: { name: 'idle' }, | ||
transitions: { | ||
idle: { | ||
'run': 'running' | ||
}, | ||
running: {} | ||
} | ||
}; | ||
expect(handleAction.bind(null, machine, 'run')).to.throw(ERROR_UNCOVERED_STATE('running')); | ||
}); | ||
}); | ||
@@ -45,5 +67,6 @@ | ||
const machine = { | ||
state: { name: 'foo' }, | ||
state: { name: 'idle' }, | ||
transitions: { | ||
foo: { run: 'running' } | ||
idle: { run: 'running' }, | ||
running: { stop: 'idle' } | ||
} | ||
@@ -61,5 +84,6 @@ }; | ||
const machine = { | ||
state: { name: 'foo' }, | ||
state: { name: 'idle' }, | ||
transitions: { | ||
foo: { run: newState } | ||
idle: { run: newState }, | ||
running: { stop: 'idle' } | ||
} | ||
@@ -80,3 +104,4 @@ }; | ||
transitions: { | ||
foo: { run: handler } | ||
foo: { run: handler }, | ||
running: { stop: 'foo' } | ||
} | ||
@@ -93,3 +118,4 @@ }; | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
bar: { a: 'b' } | ||
} | ||
@@ -102,7 +128,8 @@ }; | ||
it('should update the state even if a string is returned', function () { | ||
const handler = (state, payload) => 'new-state'; | ||
const handler = (state, payload) => 'bar'; | ||
const machine = { | ||
state: { name: 'idle', data: 42 }, | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
bar: { a: 'b' } | ||
} | ||
@@ -112,3 +139,3 @@ }; | ||
handleAction(machine, 'run'); | ||
expect(machine.state).to.deep.equal({ name: 'new-state' }); | ||
expect(machine.state).to.deep.equal({ name: 'bar' }); | ||
}); | ||
@@ -123,3 +150,4 @@ it('should run the handler with the machine as a context', function () { | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
foo: { a: 'b' } | ||
} | ||
@@ -142,3 +170,6 @@ }; | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
foo: { a: 'b' }, | ||
bar: { a: 'b' }, | ||
running: { a: 'b' } | ||
} | ||
@@ -157,3 +188,4 @@ }; | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
'100': { a: 'b' } | ||
} | ||
@@ -173,3 +205,5 @@ }; | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
running: { a: 'b' }, | ||
jumping: { a: 'b' } | ||
} | ||
@@ -195,3 +229,4 @@ }; | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
'hello stent': 'a' | ||
} | ||
@@ -216,3 +251,4 @@ }; | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
'hello stent': 'a' | ||
} | ||
@@ -242,3 +278,4 @@ }; | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
'error stent': 'a' | ||
} | ||
@@ -265,3 +302,5 @@ }; | ||
transitions: { | ||
idle: { run: handler } | ||
idle: { run: handler }, | ||
'42': 'a', | ||
'stent: merry christmas': 'b' | ||
} | ||
@@ -268,0 +307,0 @@ }; |
@@ -10,2 +10,3 @@ // errors | ||
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.`; |
@@ -1,4 +0,12 @@ | ||
import { ERROR_MISSING_ACTION_IN_STATE } from './constants'; | ||
import { ERROR_MISSING_ACTION_IN_STATE, ERROR_UNCOVERED_STATE } from './constants'; | ||
import validateState from './helpers/validateState'; | ||
function isEmptyObject(obj) { | ||
var name; | ||
for (name in obj) { | ||
if (obj.hasOwnProperty(name)) return false; | ||
} | ||
return true; | ||
} | ||
function handleGenerator(machine, generator, done, resultOfPreviousOperation) { | ||
@@ -42,9 +50,20 @@ const iterate = function (result) { | ||
function updateState(machine, response) { | ||
function updateState(machine, response) { | ||
var newState; | ||
if (typeof response === 'undefined') return; | ||
if (typeof response === 'string' || typeof response === 'number') { | ||
machine.state = { name: response.toString() }; | ||
newState = { name: response.toString() }; | ||
} else { | ||
machine.state = validateState(response); | ||
newState = validateState(response); | ||
} | ||
if ( | ||
typeof machine.transitions[newState.name] === 'undefined' || | ||
isEmptyObject(machine.transitions[newState.name]) | ||
) { | ||
throw new Error(ERROR_UNCOVERED_STATE(newState.name)); | ||
} | ||
machine.state = newState; | ||
} | ||
@@ -67,7 +86,7 @@ | ||
if (typeof handler === 'string') { | ||
state.name = transitions[state.name][action]; | ||
updateState(machine, { ...state, name: transitions[state.name][action] }); | ||
// object as a handler | ||
} else if (typeof handler === 'object') { | ||
machine.state = validateState(handler); | ||
updateState(machine, validateState(handler)); | ||
@@ -74,0 +93,0 @@ // function as a handler |
@@ -22,2 +22,5 @@ (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_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.'; | ||
}; | ||
},{}],2:[function(require,module,exports){ | ||
@@ -95,2 +98,4 @@ 'use strict'; | ||
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; }; | ||
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; }; | ||
@@ -108,19 +113,66 @@ | ||
function handleGenerator(machine, generator) { | ||
var result = generator.next(); | ||
if (!result.done) { | ||
updateState(machine, result.value); | ||
return handleGenerator(machine, generator); | ||
function isEmptyObject(obj) { | ||
var name; | ||
for (name in obj) { | ||
if (obj.hasOwnProperty(name)) return false; | ||
} | ||
return result.value; | ||
return true; | ||
} | ||
function handleGenerator(machine, generator, done, resultOfPreviousOperation) { | ||
var iterate = function iterate(result) { | ||
if (!result.done) { | ||
// yield call | ||
if (_typeof(result.value) === 'object' && result.value.__type === 'call') { | ||
var _result$value; | ||
var funcResult = (_result$value = result.value).func.apply(_result$value, result.value.args); | ||
// promise | ||
if (typeof funcResult.then !== 'undefined') { | ||
funcResult.then(function (r) { | ||
return iterate(generator.next(r)); | ||
}, function (error) { | ||
return iterate(generator.throw(new Error(error))); | ||
}); | ||
// generator | ||
} else if (typeof funcResult.next === 'function') { | ||
handleGenerator(machine, funcResult, function (generatorResult) { | ||
iterate(generator.next(generatorResult)); | ||
}); | ||
} else { | ||
iterate(generator.next(funcResult)); | ||
} | ||
// the return statement of the normal function | ||
} else { | ||
updateState(machine, result.value); | ||
iterate(generator.next()); | ||
} | ||
// the end of the generator (return statement) | ||
} else { | ||
done(result.value); | ||
} | ||
}; | ||
iterate(generator.next(resultOfPreviousOperation)); | ||
} | ||
function updateState(machine, response) { | ||
var newState; | ||
if (typeof response === 'undefined') return; | ||
if (typeof response === 'string') { | ||
machine.state = { name: response }; | ||
if (typeof response === 'string' || typeof response === 'number') { | ||
newState = { name: response.toString() }; | ||
} else { | ||
machine.state = (0, _validateState2.default)(response); | ||
newState = (0, _validateState2.default)(response); | ||
} | ||
if (typeof machine.transitions[newState.name] === 'undefined' || isEmptyObject(machine.transitions[newState.name])) { | ||
throw new Error((0, _constants.ERROR_UNCOVERED_STATE)(newState.name)); | ||
} | ||
machine.state = newState; | ||
} | ||
@@ -145,7 +197,7 @@ | ||
if (typeof handler === 'string') { | ||
state.name = transitions[state.name][action]; | ||
updateState(machine, _extends({}, state, { name: transitions[state.name][action] })); | ||
// object as a handler | ||
} else if ((typeof handler === 'undefined' ? 'undefined' : _typeof(handler)) === 'object') { | ||
machine.state = (0, _validateState2.default)(handler); | ||
updateState(machine, (0, _validateState2.default)(handler)); | ||
@@ -157,6 +209,8 @@ // function as a handler | ||
if (response && typeof response.next === 'function') { | ||
response = handleGenerator(machine, response); | ||
handleGenerator(machine, response, function (response) { | ||
updateState(machine, response); | ||
}); | ||
} else { | ||
updateState(machine, response); | ||
} | ||
updateState(machine, response); | ||
} | ||
@@ -163,0 +217,0 @@ |
@@ -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_MISSING_ACTION_IN_STATE=exports.ERROR_MISSING_ACTION_IN_STATE=function ERROR_MISSING_ACTION_IN_STATE(action,state){return'"'+action+'" action is not available in "'+state+'" state'};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}},{}],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.registerMethods=registerMethods;exports.validateConfig=validateConfig;exports.default=createMachine;var _toCamelCase=require("./helpers/toCamelCase");var _toCamelCase2=_interopRequireDefault(_toCamelCase);var _constants=require("./constants");var _handleAction=require("./handleAction");var _handleAction2=_interopRequireDefault(_handleAction);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function registerMethods(machine,transitions,dispatch){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]){(function(action){machine[(0,_toCamelCase2.default)(action)]=function(payload){return dispatch(action,payload)}})(action)}}}function validateConfig(_ref){var state=_ref.state,transitions=_ref.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}function createMachine(name,config){var machine={name:name};var initialState=config.state,transitions=config.transitions;machine.state=initialState;if(validateConfig(config)){registerMethods(machine,transitions,function(action,payload){return(0,_handleAction2.default)(machine,action,payload)})}return machine}},{"./constants":1,"./handleAction":3,"./helpers/toCamelCase":4}],3:[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=handleAction;var _constants=require("./constants");var _validateState=require("./helpers/validateState");var _validateState2=_interopRequireDefault(_validateState);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function handleGenerator(machine,generator){var result=generator.next();if(!result.done){updateState(machine,result.value);return handleGenerator(machine,generator)}return result.value}function updateState(machine,response){if(typeof response==="undefined")return;if(typeof response==="string"){machine.state={name:response}}else{machine.state=(0,_validateState2.default)(response)}}function handleAction(machine,action,payload){var state=machine.state,transitions=machine.transitions;if(!transitions[state.name]){return false}var handler=transitions[state.name][action];if(typeof transitions[state.name][action]==="undefined"){throw new Error((0,_constants.ERROR_MISSING_ACTION_IN_STATE)(action,state.name))}if(typeof handler==="string"){state.name=transitions[state.name][action]}else if((typeof handler==="undefined"?"undefined":_typeof(handler))==="object"){machine.state=(0,_validateState2.default)(handler)}else if(typeof handler==="function"){var response=transitions[state.name][action].apply(machine,[machine.state,payload]);if(response&&typeof response.next==="function"){response=handleGenerator(machine,response)}updateState(machine,response)}return true}module.exports=exports["default"]},{"./constants":1,"./helpers/validateState":5}],4:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=function(text){return text.toLowerCase().replace(/\W+(.)/g,function(match,chr){return chr.toUpperCase()})};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};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}],6:[function(require,module,exports){"use strict";exports.__esModule=true;exports.Machine=undefined;var _createMachine=require("./createMachine");var _createMachine2=_interopRequireDefault(_createMachine);var _constants=require("./constants");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={}}MachineFactory.prototype.create=function create(name,config){return this.machines[name]=(0,_createMachine2.default)(name,config)};MachineFactory.prototype.get=function get(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=[]};return MachineFactory}();var factory=new MachineFactory;exports.Machine=factory},{"./constants":1,"./createMachine":2}]},{},[6])(6)}); | ||
(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_MISSING_ACTION_IN_STATE=exports.ERROR_MISSING_ACTION_IN_STATE=function ERROR_MISSING_ACTION_IN_STATE(action,state){return'"'+action+'" action is not available in "'+state+'" state'};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."}},{}],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.registerMethods=registerMethods;exports.validateConfig=validateConfig;exports.default=createMachine;var _toCamelCase=require("./helpers/toCamelCase");var _toCamelCase2=_interopRequireDefault(_toCamelCase);var _constants=require("./constants");var _handleAction=require("./handleAction");var _handleAction2=_interopRequireDefault(_handleAction);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function registerMethods(machine,transitions,dispatch){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]){(function(action){machine[(0,_toCamelCase2.default)(action)]=function(payload){return dispatch(action,payload)}})(action)}}}function validateConfig(_ref){var state=_ref.state,transitions=_ref.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}function createMachine(name,config){var machine={name:name};var initialState=config.state,transitions=config.transitions;machine.state=initialState;if(validateConfig(config)){registerMethods(machine,transitions,function(action,payload){return(0,_handleAction2.default)(machine,action,payload)})}return machine}},{"./constants":1,"./handleAction":3,"./helpers/toCamelCase":4}],3:[function(require,module,exports){"use strict";exports.__esModule=true;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};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=handleAction;var _constants=require("./constants");var _validateState=require("./helpers/validateState");var _validateState2=_interopRequireDefault(_validateState);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function isEmptyObject(obj){var name;for(name in obj){if(obj.hasOwnProperty(name))return false}return true}function handleGenerator(machine,generator,done,resultOfPreviousOperation){var iterate=function iterate(result){if(!result.done){if(_typeof(result.value)==="object"&&result.value.__type==="call"){var _result$value;var funcResult=(_result$value=result.value).func.apply(_result$value,result.value.args);if(typeof funcResult.then!=="undefined"){funcResult.then(function(r){return iterate(generator.next(r))},function(error){return iterate(generator.throw(new Error(error)))})}else if(typeof funcResult.next==="function"){handleGenerator(machine,funcResult,function(generatorResult){iterate(generator.next(generatorResult))})}else{iterate(generator.next(funcResult))}}else{updateState(machine,result.value);iterate(generator.next())}}else{done(result.value)}};iterate(generator.next(resultOfPreviousOperation))}function updateState(machine,response){var newState;if(typeof response==="undefined")return;if(typeof response==="string"||typeof response==="number"){newState={name:response.toString()}}else{newState=(0,_validateState2.default)(response)}if(typeof machine.transitions[newState.name]==="undefined"||isEmptyObject(machine.transitions[newState.name])){throw new Error((0,_constants.ERROR_UNCOVERED_STATE)(newState.name))}machine.state=newState}function handleAction(machine,action,payload){var state=machine.state,transitions=machine.transitions;if(!transitions[state.name]){return false}var handler=transitions[state.name][action];if(typeof transitions[state.name][action]==="undefined"){throw new Error((0,_constants.ERROR_MISSING_ACTION_IN_STATE)(action,state.name))}if(typeof handler==="string"){updateState(machine,_extends({},state,{name:transitions[state.name][action]}))}else if((typeof handler==="undefined"?"undefined":_typeof(handler))==="object"){updateState(machine,(0,_validateState2.default)(handler))}else if(typeof handler==="function"){var response=transitions[state.name][action].apply(machine,[machine.state,payload]);if(response&&typeof response.next==="function"){handleGenerator(machine,response,function(response){updateState(machine,response)})}else{updateState(machine,response)}}return true}module.exports=exports["default"]},{"./constants":1,"./helpers/validateState":5}],4:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=function(text){return text.toLowerCase().replace(/\W+(.)/g,function(match,chr){return chr.toUpperCase()})};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};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}],6:[function(require,module,exports){"use strict";exports.__esModule=true;exports.Machine=undefined;var _createMachine=require("./createMachine");var _createMachine2=_interopRequireDefault(_createMachine);var _constants=require("./constants");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={}}MachineFactory.prototype.create=function create(name,config){return this.machines[name]=(0,_createMachine2.default)(name,config)};MachineFactory.prototype.get=function get(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=[]};return MachineFactory}();var factory=new MachineFactory;exports.Machine=factory},{"./constants":1,"./createMachine":2}]},{},[6])(6)}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
218804
36
1454
0