A modular and microscopic SAM-patterned container
It draws inspiration from redux
npm install --save @jackalope/core
Jackalope adheres to the SAM pattern, which can be summarized by the formula:
V = S(vm(M.present(A(M))), nap(M, A))
(source)
You can read about SAM here: http://sam.js.org/
// src/jack.js
import Jackalope from '@jackalope/core'
import state from './state'
import actions from './actions'
import model from './model'
const J = Jackalope({ state, actions, model }/*, middleware */)
In Jackalope, state
is a function that receives model as its first parameter. If the function does not specify arguments, or specifies more than 1, it will also receive actions bound automatically during instatiation as the second parameter.
Example:
// src/state.js
const state = (model, actions) => {
const representation = 'Oops, you\'ve stumbled upon an impossible state'
if (model.count > 0) {
representation = view.counting(model, actions)
} else if (model.count < 1) {
representation = view.launching(model, actions)
}
...
view.render(representation)
nap(model, actions)
}
export default state
model
is an object which contains your data. It must have a method, model.present
which can either accept an action and update itself, or reject actions outright.
model.present
is assumed to have curried form state => action => { ... }
.
IMPORTANT : model.present
is the only function allowed to mutate model
in the SAM pattern.
Example:
// src/model.js
const model = {
count: 10,
...
}
model.present = (state) => (action) => {
if (action.started) {
model.started = true
}
if (action.aborted) {
model.aborted = true
}
...
state(model)
}
export default model
actions
should be a function which accepts present
(as in, model.present
) as its only parameter.
It should return an object with functions that present proposals to the model.
// src/actions.js
const actions = present => ({
start: function () {
present({ started: true })
},
abort: function () {
present({ aborted: true })
},
...
})
export default actions
https://github.com/schtauffen/jackalope/tree/master/core
TODO - more information coming
SAM apps are meant to be composable.
TODO - More to come
Jackalope has been created mostly to learn about and play with the SAM pattern.
As is, there are no tests or plans for long term support. This may change pending on how useful this implementation is :)
Jackalope is licensed under the ISC license