@erickmerchant/framework
Advanced tools
Comparing version 11.0.1 to 12.0.0
19
main.js
module.exports = function ({target, store, component, diff, raf}) { | ||
raf = raf != null ? raf : window.requestAnimationFrame | ||
let state = store() | ||
let state | ||
let rafCalled = false | ||
let action | ||
action = store(function (seed) { | ||
if (!action) { | ||
state = seed | ||
} | ||
}) | ||
return function (init) { | ||
@@ -11,9 +18,9 @@ init({target, dispatch}) | ||
function dispatch () { | ||
const args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)) | ||
function dispatch (...args) { | ||
action(commit, ...args) | ||
} | ||
args.unshift(state) | ||
function commit (then) { | ||
state = then(state) | ||
state = store.apply(null, args) | ||
if (!rafCalled) { | ||
@@ -20,0 +27,0 @@ rafCalled = true |
{ | ||
"name": "@erickmerchant/framework", | ||
"version": "11.0.1", | ||
"version": "12.0.0", | ||
"description": "A simple data down, actions up framework.", | ||
@@ -5,0 +5,0 @@ "main": "main.js", |
# @erickmerchant/framework | ||
This scoped package is my personal framework. I wrote it to understand how modern javascript frameworks do things. It's meant to be used with browserify. | ||
This scoped package is my personal framework. I use it on a number of small projects. | ||
@@ -21,9 +21,11 @@ ## An Example | ||
function store (state = 0, action) { | ||
switch (action) { | ||
case 'increment': return state + 1 | ||
case 'decrement': return state - 1 | ||
function store (seed) { | ||
seed(0) | ||
return function (commit, action) { | ||
switch (action) { | ||
case 'increment': commit((state) => state + 1) | ||
case 'decrement': commit((state) => state - 1) | ||
} | ||
} | ||
return state | ||
} | ||
@@ -70,4 +72,21 @@ | ||
This is what is returned from the [store](#store). It can be anything from simply a number like in the example, to a complex object. | ||
This is what is initially passed to [seed](#seed) and subsequently returned from [commit](#commit) callbacks. It can be anything from simply a number like in the example, to a complex object. | ||
#### seed | ||
_seed(state)_ | ||
It should be passed the initial state | ||
- [state](#state): the initial state | ||
#### commit | ||
_commit((current) => next)_ | ||
It's passed a callback that receives the current state and should return the new state. | ||
- [current](#state): the current state | ||
- [next](#state): the next state | ||
#### dispatch | ||
@@ -79,3 +98,3 @@ | ||
- arguments: all get passed to the store | ||
- arguments: all get passed to the [agent](#agent) | ||
@@ -102,7 +121,15 @@ #### next | ||
_store(state, ...arguments)_ | ||
_store(seed)_ | ||
Called initially with zero arguments, it should return the default/initial state. | ||
It should return the [agent](#agent). | ||
- state: the previous thing returned from calling store | ||
- [seed](#seed) | ||
#### agent | ||
_agent(commit, ...arguments)_ | ||
Anything returned will be ignored | ||
- [commit](#commit) | ||
- arguments: all the arguments passed to [dispatch](#dispatch) | ||
@@ -132,3 +159,3 @@ | ||
A module to create a store from other stores where each is responsible for one property in the state. | ||
A module to create a store from other stores where each is responsible for one property in the state. __Does not currently work with 12__. | ||
@@ -135,0 +162,0 @@ - [@erickmerchant/router](https://github.com/erickmerchant/router) |
37
test.js
const test = require('tape') | ||
const noop = function () {} | ||
const noopStore = function (seed) { | ||
seed('') | ||
return function (commit) { | ||
commit(function () { | ||
return '' | ||
}) | ||
} | ||
} | ||
test('test main - init', function (t) { | ||
@@ -11,5 +20,3 @@ let theTarget = {} | ||
target: theTarget, | ||
store: function () { | ||
return {} | ||
}, | ||
store: noopStore, | ||
component: noop, | ||
@@ -27,8 +34,18 @@ diff: noop, | ||
t.plan(3) | ||
t.plan(6) | ||
require('./main.js')({ | ||
target: theTarget, | ||
store: function (state = {}) { | ||
return state | ||
store: function (seed) { | ||
seed('') | ||
return function (commit, arg) { | ||
commit(function (state) { | ||
t.equal(state, '') | ||
t.equal(arg, 123) | ||
return 'test' | ||
}) | ||
} | ||
}, | ||
@@ -38,2 +55,4 @@ component: function (app) { | ||
t.equal(app.state, 'test') | ||
return {} | ||
@@ -50,3 +69,3 @@ }, | ||
})(function ({target, dispatch}) { | ||
dispatch() | ||
dispatch(123) | ||
}) | ||
@@ -63,5 +82,3 @@ }) | ||
target: theTarget, | ||
store: function (state = {}) { | ||
return {} | ||
}, | ||
store: noopStore, | ||
component: function (app) { | ||
@@ -68,0 +85,0 @@ if (!nextRun) { |
7832
114
164