Last active
September 29, 2016 22:22
-
-
Save m3g4p0p/3868275f3f4410fa2bb651c15a4be5e4 to your computer and use it in GitHub Desktop.
A state container that allows calling actions directly on the container instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function init (global) { | |
'use strict' | |
// Constructor function, initialise states and actions | |
global.Container = function constructor (initialState) { | |
this._states = initialState === undefined ? [] : [initialState] | |
this._actions = {} | |
} | |
// Get the current state | |
Container.prototype.$get = function get () { | |
return this._states[this._states.length - 1] | |
} | |
// Reset the states array | |
Container.prototype.$clear = function clear () { | |
this._states = [] | |
} | |
// Revert the last state change | |
Container.prototype.$undo = function undo () { | |
return this._states.pop() | |
} | |
// Register a dedicated reducer to an action, which again is | |
// specified by the function name | |
Container.prototype.$register = function register (reducer) { | |
const name = reducer.name | |
// Push the new reducer to the corresponding action | |
this._actions[name] = this._actions[name] || [] | |
this._actions[name].push(reducer) | |
// The action can be dispatched by calling it directly on | |
// the container instance | |
this[name] = this[name] || function action (...args) { | |
// Reduce the state by passing it along the reducers | |
const newState = this | |
._actions[name] | |
.reduce((currentState, currentReducer) => | |
currentReducer.call(this, currentState, ...args), | |
this.$get() | |
) | |
// Push and return the new state | |
this._states.push(newState) | |
return newState | |
} | |
// When registering a new reducer, another function to | |
// unregister it gets returned | |
return function unregister () { | |
this._actions[name].splice(this._actions[name].indexOf(reducer), 1) | |
}.bind(this) | |
} | |
// Shorthand factory | |
global.contain = function contain (initialState) { | |
return new Container(initialState) | |
} | |
})(typeof global === 'undefined' ? window : global) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ez-state
Idea
Basically just a convenient shorthand for dispatching actions.
Usage
License
MIT