Socket
Socket
Sign inDemoInstall

stent

Package Overview
Dependencies
Maintainers
1
Versions
82
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stent - npm Package Compare versions

Comparing version 0.0.8 to 0.1.0

5

lib/index.js

@@ -12,2 +12,6 @@ 'use strict';

var _connect = require('./helpers/connect');
var _connect2 = _interopRequireDefault(_connect);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -23,2 +27,3 @@

this.middlewares = [];
this.connect = _connect2.default;
}

@@ -25,0 +30,0 @@

12

package.json
{
"name": "stent",
"version": "0.0.8",
"description": "Stent is a state machines container made for UI development",
"version": "0.1.0",
"description": "Stent is combining the ideas of redux with the concept of state machines",
"main": "lib/index.js",

@@ -22,3 +22,9 @@ "scripts": {

"machine",
"stent"
"stent",
"state machine",
"mealy",
"redux",
"react",
"connect",
"transitions"
],

@@ -25,0 +31,0 @@ "author": "Krasimir Tsonev",

# Stent
Container for finite state machines or in other words - a tool for state management based on state machines.
Stent is combining the ideas of redux with the concept of state machines.
---
* [A few words about state machines](#a-few-words-about-state-machines)
* [Installation](#installation)
* [A few words about state machines](#a-few-words-about-state-machines)
* [Getting started](##getting-started)

@@ -20,11 +20,11 @@ * [API](#api)

## Installation
## A few words about state machines
The library is available as a [npm module](https://www.npmjs.com/package/stent) so `npm install stent` will do the job. There's also a standalone version [here](./standalone/stent.js) (only core functionalities) which you can directly add to your page.
State machine is a mathematical model of computation. It's an abstract concept where the machine may have different states but at a given time fulfills only one of them. It accepts input and based on that (plus its current state) transitions to another state. Isn't it sounds familiar? Yes, it sounds like a front-end application. That's why this model/concept applies nicely to UI development.
## A few words about state machines
*Disclaimer: there are different types of state machines. I think the one that makes sense for front-end development is [Mealy state machine](https://en.wikipedia.org/wiki/Mealy_machine).*
State machine is a mathematical model of computation. It's an abstract concept where the machine has different states. It accepts input and based on which (plus its current state) transitions into another state. Isn't it sounds familiar? Yes, it is sounds like a front-end application. That's why that model/concept applies nicely to UI development.
## Installation
*Disclaimer: there are different types of state machines. The one that fits in my use case is [Mealy finite state machine](https://en.wikipedia.org/wiki/Mealy_machine).*
The library is available as a [npm module](https://www.npmjs.com/package/stent) so `npm install stent` or `yarn add stent` will do the job. There's also a standalone version [here](./standalone/stent.js) (only core functionalities) which you can directly add to your page.

@@ -149,3 +149,3 @@ ## Getting started

### `Machine.<create|get|flush>`
### `Machine.<create|get|flush|connect>`

@@ -188,2 +188,4 @@ The `Machine` object is used for creating and fetching machines.

`Machine.connect()` is the same as the [connect](#connect-and-disconnect) helper.
### `<action handler>`

@@ -190,0 +192,0 @@

import createMachine from './createMachine';
import { ERROR_MISSING_MACHINE } from './constants';
import connect from './helpers/connect';

@@ -8,2 +9,3 @@ class MachineFactory {

this.middlewares = [];
this.connect = connect;
}

@@ -10,0 +12,0 @@ create(name, config) {

@@ -101,3 +101,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){

}
},{"./constants":1,"./handleAction":3,"./helpers/toCamelCase":4}],3:[function(require,module,exports){
},{"./constants":1,"./handleAction":3,"./helpers/toCamelCase":5}],3:[function(require,module,exports){
'use strict';

@@ -305,3 +305,59 @@

module.exports = exports['default'];
},{"./constants":1,"./helpers/validateState":5}],4:[function(require,module,exports){
},{"./constants":1,"./helpers/validateState":6}],4:[function(require,module,exports){
'use strict';
exports.__esModule = true;
exports.default = connect;
var _ = require('../');
var idIndex = 0;
var getId = function getId() {
return 'm' + ++idIndex;
};
function connect() {
var mappings = {};
_.Machine.addMiddleware({
onStateChange: function onStateChange(next) {
next();
for (var id in mappings) {
var _mappings$id;
(_mappings$id = mappings[id]).done.apply(_mappings$id, mappings[id].machines);
}
}
});
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) {
var id = getId();
!once && (mappings[id] = { done: done, machines: machines });
done.apply(undefined, machines);
return function () {
if (mappings && mappings[id]) delete mappings[id];
};
};
return {
'map': mapFunc,
'mapOnce': function mapOnce(done) {
return mapFunc(done, true);
}
};
};
return { 'with': withFunc };
}
module.exports = exports['default'];
},{"../":7}],5:[function(require,module,exports){
"use strict";

@@ -318,3 +374,3 @@

module.exports = exports['default'];
},{}],5:[function(require,module,exports){
},{}],6:[function(require,module,exports){
'use strict';

@@ -335,3 +391,3 @@

module.exports = exports['default'];
},{"../constants":1}],6:[function(require,module,exports){
},{"../constants":1}],7:[function(require,module,exports){
'use strict';

@@ -348,2 +404,6 @@

var _connect = require('./helpers/connect');
var _connect2 = _interopRequireDefault(_connect);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -359,2 +419,3 @@

this.middlewares = [];
this.connect = _connect2.default;
}

@@ -386,3 +447,3 @@

exports.Machine = factory;
},{"./constants":1,"./createMachine":2}]},{},[6])(6)
},{"./constants":1,"./createMachine":2,"./helpers/connect":4}]},{},[7])(7)
});

@@ -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};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 WAIT_LISTENERS_STORAGE=exports.WAIT_LISTENERS_STORAGE="___@wait";var MIDDLEWARE_STORAGE=exports.MIDDLEWARE_STORAGE="___@middlewares"},{}],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,middlewares){var _machine;var machine=(_machine={name:name},_machine[_constants.MIDDLEWARE_STORAGE]=middlewares,_machine);var initialState=config.state,transitions=config.transitions;machine.state=initialState;machine.transitions=transitions;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}}var MIDDLEWARE_PROCESS_ACTION="onActionDispatched";var MIDDLEWARE_PROCESS_STATE_CHANGE="onStateChange";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(result){return iterate(generator.next(result))},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 if(_typeof(result.value)==="object"&&result.value.__type==="wait"){waitFor(machine,result.value.actions,function(result){return iterate(generator.next(result))})}else{updateState(machine,result.value);iterate(generator.next())}}else{done(result.value)}};iterate(generator.next(resultOfPreviousOperation))}function waitFor(machine,actions,done){if(!machine[_constants.WAIT_LISTENERS_STORAGE])machine[_constants.WAIT_LISTENERS_STORAGE]=[];machine[_constants.WAIT_LISTENERS_STORAGE].push({actions:actions,done:done,result:[].concat(actions)})}function flushListeners(machine,action,payload){if(!machine[_constants.WAIT_LISTENERS_STORAGE]||machine[_constants.WAIT_LISTENERS_STORAGE].length===0)return;var callbacks=[];machine[_constants.WAIT_LISTENERS_STORAGE]=machine[_constants.WAIT_LISTENERS_STORAGE].filter(function(_ref){var actions=_ref.actions,done=_ref.done,result=_ref.result;var actionIndex=actions.indexOf(action);if(actionIndex===-1)return true;result[result.indexOf(action)]=payload;actions.splice(actionIndex,1);if(actions.length===0){result.length===1?callbacks.push(done.bind(null,result[0])):callbacks.push(done.bind(null,result));return false}return true});callbacks.forEach(function(c){return c()});if(machine[_constants.WAIT_LISTENERS_STORAGE].length===0)delete machine[_constants.WAIT_LISTENERS_STORAGE]}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))}handleMiddleware(function(){machine.state=newState},MIDDLEWARE_PROCESS_STATE_CHANGE,machine)}function handleMiddleware(done,hook,machine){for(var _len=arguments.length,args=Array(_len>3?_len-3:0),_key=3;_key<_len;_key++){args[_key-3]=arguments[_key]}if(!machine[_constants.MIDDLEWARE_STORAGE])return done();var middlewares=machine[_constants.MIDDLEWARE_STORAGE];var loop=function loop(index,process){return index<middlewares.length-1?process(index+1):done()};(function process(index){var middleware=middlewares[index];if(middleware&&typeof middleware[hook]!=="undefined"){middleware[hook].apply(machine,[function(){return loop(index,process)}].concat(args))}else{loop(index,process)}})(0)}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))}handleMiddleware(function(){flushListeners(machine,action,payload);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)}}},MIDDLEWARE_PROCESS_ACTION,machine,action,payload);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={};this.middlewares=[]}MachineFactory.prototype.create=function create(name,config){return this.machines[name]=(0,_createMachine2.default)(name,config,this.middlewares)};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=[];this.middlewares=[]};MachineFactory.prototype.addMiddleware=function addMiddleware(middleware){this.middlewares.push(middleware)};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."};var WAIT_LISTENERS_STORAGE=exports.WAIT_LISTENERS_STORAGE="___@wait";var MIDDLEWARE_STORAGE=exports.MIDDLEWARE_STORAGE="___@middlewares"},{}],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,middlewares){var _machine;var machine=(_machine={name:name},_machine[_constants.MIDDLEWARE_STORAGE]=middlewares,_machine);var initialState=config.state,transitions=config.transitions;machine.state=initialState;machine.transitions=transitions;if(validateConfig(config)){registerMethods(machine,transitions,function(action,payload){return(0,_handleAction2.default)(machine,action,payload)})}return machine}},{"./constants":1,"./handleAction":3,"./helpers/toCamelCase":5}],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}}var MIDDLEWARE_PROCESS_ACTION="onActionDispatched";var MIDDLEWARE_PROCESS_STATE_CHANGE="onStateChange";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(result){return iterate(generator.next(result))},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 if(_typeof(result.value)==="object"&&result.value.__type==="wait"){waitFor(machine,result.value.actions,function(result){return iterate(generator.next(result))})}else{updateState(machine,result.value);iterate(generator.next())}}else{done(result.value)}};iterate(generator.next(resultOfPreviousOperation))}function waitFor(machine,actions,done){if(!machine[_constants.WAIT_LISTENERS_STORAGE])machine[_constants.WAIT_LISTENERS_STORAGE]=[];machine[_constants.WAIT_LISTENERS_STORAGE].push({actions:actions,done:done,result:[].concat(actions)})}function flushListeners(machine,action,payload){if(!machine[_constants.WAIT_LISTENERS_STORAGE]||machine[_constants.WAIT_LISTENERS_STORAGE].length===0)return;var callbacks=[];machine[_constants.WAIT_LISTENERS_STORAGE]=machine[_constants.WAIT_LISTENERS_STORAGE].filter(function(_ref){var actions=_ref.actions,done=_ref.done,result=_ref.result;var actionIndex=actions.indexOf(action);if(actionIndex===-1)return true;result[result.indexOf(action)]=payload;actions.splice(actionIndex,1);if(actions.length===0){result.length===1?callbacks.push(done.bind(null,result[0])):callbacks.push(done.bind(null,result));return false}return true});callbacks.forEach(function(c){return c()});if(machine[_constants.WAIT_LISTENERS_STORAGE].length===0)delete machine[_constants.WAIT_LISTENERS_STORAGE]}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))}handleMiddleware(function(){machine.state=newState},MIDDLEWARE_PROCESS_STATE_CHANGE,machine)}function handleMiddleware(done,hook,machine){for(var _len=arguments.length,args=Array(_len>3?_len-3:0),_key=3;_key<_len;_key++){args[_key-3]=arguments[_key]}if(!machine[_constants.MIDDLEWARE_STORAGE])return done();var middlewares=machine[_constants.MIDDLEWARE_STORAGE];var loop=function loop(index,process){return index<middlewares.length-1?process(index+1):done()};(function process(index){var middleware=middlewares[index];if(middleware&&typeof middleware[hook]!=="undefined"){middleware[hook].apply(machine,[function(){return loop(index,process)}].concat(args))}else{loop(index,process)}})(0)}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))}handleMiddleware(function(){flushListeners(machine,action,payload);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)}}},MIDDLEWARE_PROCESS_ACTION,machine,action,payload);return true}module.exports=exports["default"]},{"./constants":1,"./helpers/validateState":6}],4:[function(require,module,exports){"use strict";exports.__esModule=true;exports.default=connect;var _=require("../");var idIndex=0;var getId=function getId(){return"m"+ ++idIndex};function connect(){var mappings={};_.Machine.addMiddleware({onStateChange:function onStateChange(next){next();for(var id in mappings){var _mappings$id;(_mappings$id=mappings[id]).done.apply(_mappings$id,mappings[id].machines)}}});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){var id=getId();!once&&(mappings[id]={done:done,machines:machines});done.apply(undefined,machines);return function(){if(mappings&&mappings[id])delete mappings[id]}};return{map:mapFunc,mapOnce:function mapOnce(done){return mapFunc(done,true)}}};return{with:withFunc}}module.exports=exports["default"]},{"../":7}],5:[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"]},{}],6:[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}],7:[function(require,module,exports){"use strict";exports.__esModule=true;exports.Machine=undefined;var _createMachine=require("./createMachine");var _createMachine2=_interopRequireDefault(_createMachine);var _constants=require("./constants");var _connect=require("./helpers/connect");var _connect2=_interopRequireDefault(_connect);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}MachineFactory.prototype.create=function create(name,config){return this.machines[name]=(0,_createMachine2.default)(name,config,this.middlewares)};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=[];this.middlewares=[]};MachineFactory.prototype.addMiddleware=function addMiddleware(middleware){this.middlewares.push(middleware)};return MachineFactory}();var factory=new MachineFactory;exports.Machine=factory},{"./constants":1,"./createMachine":2,"./helpers/connect":4}]},{},[7])(7)});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc