@erickmerchant/framework
Advanced tools
Comparing version 12.2.1 to 13.0.0
33
main.js
@@ -7,30 +7,18 @@ const assert = require('assert') | ||
assert.equal(typeof store, 'function', 'store must be a function') | ||
assert.equal(typeof component, 'function', 'component must be a function') | ||
assert.equal(typeof diff, 'function', 'diff must be a function') | ||
assert.equal(typeof raf, 'function', 'raf must be a function') | ||
let rafCalled = false | ||
let initialized = false | ||
let state | ||
let action = store(function (seed) { | ||
assert.ok(!initialized, 'trying to seed after the app is initialized') | ||
let agent = store(commit) | ||
if (typeof seed === 'function') { | ||
state = seed(commit) | ||
} else { | ||
state = seed | ||
} | ||
}) | ||
return function (init) { | ||
initialized = true | ||
assert.equal(typeof init, 'function', 'init must be a function') | ||
if (init != null) { | ||
assert.equal(typeof init, 'function', 'init must be a function') | ||
init({target, dispatch}) | ||
} else { | ||
dispatch() | ||
} | ||
init(dispatch) | ||
} | ||
@@ -42,3 +30,3 @@ | ||
} else { | ||
action(commit, ...args) | ||
agent(...args) | ||
} | ||
@@ -48,3 +36,2 @@ } | ||
function commit (current) { | ||
assert.ok(initialized, 'trying to commit before the app is initialized') | ||
assert.equal(typeof current, 'function', 'current must be a function') | ||
@@ -66,5 +53,3 @@ | ||
if (element != null) { | ||
diff(target, element) | ||
} | ||
diff(target, element) | ||
} | ||
@@ -75,4 +60,4 @@ | ||
process.nextTick(callback, {target, dispatch}) | ||
process.nextTick(callback, target) | ||
} | ||
} |
{ | ||
"name": "@erickmerchant/framework", | ||
"version": "12.2.1", | ||
"version": "13.0.0", | ||
"description": "A simple data down, actions up framework.", | ||
@@ -5,0 +5,0 @@ "main": "main.js", |
@@ -17,6 +17,6 @@ # @erickmerchant/framework | ||
function store (seed) { | ||
seed(0) | ||
function store (commit) { | ||
commit(() => 0) | ||
return function (commit, action) { | ||
return function (action) { | ||
switch (action) { | ||
@@ -65,15 +65,4 @@ case 'increment': | ||
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. | ||
This is what is passed to [commit](#commit). It can be anything from simply a number like in the example, but a plain object makes sense in most cases. | ||
#### seed | ||
_seed(state|setup)_ | ||
It should be passed the initial state or a setup function | ||
- [state](#state): the initial state | ||
- [setup](#setup): a function to generate the initial state | ||
_Warning: Calling seed after [init](#init) is called will throw an Error_ | ||
#### commit | ||
@@ -88,4 +77,2 @@ | ||
_Warning: Calling commit before [init](#init) is called will throw an Error_ | ||
#### dispatch | ||
@@ -95,3 +82,3 @@ | ||
Initializes a change in state and causes a render. If the length of arguments is 0, the agent is not, but a render is triggered. | ||
Initializes a change in state and causes a render. If the length of arguments is 0, the agent is not called, but a render is triggered. | ||
@@ -102,3 +89,3 @@ - arguments: all get passed to the [agent](#agent) | ||
_next(({target, dispatch}) => { ... })_ | ||
_next((target) => { ... })_ | ||
@@ -108,11 +95,9 @@ A convenient helper to pass a callback to process.nextTick. Can be used to manipulate the page in some way after a render. For example scrolling to a specific element or focusing an input element. | ||
- target: the target passed to [framework](#framework) | ||
- [dispatch](#dispatch) | ||
#### init | ||
_init(({target, dispatch}) => { ... })_ | ||
_init((dispatch) => { ... })_ | ||
Call init to mount your component. The callback is optional. | ||
Call init to do some initial work that may require dispatch. | ||
- target: the target passed to [framework](#framework) | ||
- [dispatch](#dispatch) | ||
@@ -124,14 +109,6 @@ | ||
_store(seed)_ | ||
_store(commit)_ | ||
It should return the [agent](#agent). | ||
- [seed](#seed) | ||
#### setup | ||
_setup(commit)_ | ||
It should return the initial state. | ||
- [commit](#commit) | ||
@@ -141,7 +118,6 @@ | ||
_agent(commit, ...arguments)_ | ||
_agent(..arguments)_ | ||
Anything returned will be ignored | ||
- [commit](#commit) | ||
- arguments: all the arguments passed to [dispatch](#dispatch) | ||
@@ -148,0 +124,0 @@ |
164
test.js
const test = require('tape') | ||
const noop = function () {} | ||
const noopStore = function (seed) { | ||
seed('') | ||
const noopStore = function (commit) { | ||
commit(() => '') | ||
return function (commit) { | ||
return function () { | ||
commit(function () { | ||
@@ -12,20 +12,30 @@ return '' | ||
} | ||
const raf = function (callback) { | ||
process.nextTick(function () { callback() }) | ||
} | ||
const initialElement = Symbol('initial target') | ||
const newElement = Symbol('new target') | ||
const initialState = Symbol('initial state') | ||
const newState = Symbol('new state') | ||
const dispatchArgumnt = Symbol('dispatch argument') | ||
test('init to render', function (t) { | ||
let theTarget = {} | ||
t.plan(14) | ||
t.plan(9) | ||
require('./main.js')({ | ||
target: theTarget, | ||
store: function (seed) { | ||
seed('') | ||
target: initialElement, | ||
store: function (commit) { | ||
t.equal(commit.name, 'commit') | ||
return function (commit, arg) { | ||
t.equal(typeof commit, 'function') | ||
commit(() => initialState) | ||
return function (arg) { | ||
t.equal(arg, dispatchArgumnt) | ||
commit(function (state) { | ||
t.equal(state, '') | ||
t.equal(state, initialState) | ||
t.equal(arg, 123) | ||
return 'test' | ||
return newState | ||
}) | ||
@@ -37,22 +47,26 @@ } | ||
t.equal(app.state, 'test') | ||
t.equal(app.dispatch.name, 'dispatch') | ||
return {} | ||
t.equal(typeof app.dispatch, 'function') | ||
t.equal(app.next.name, 'next') | ||
t.equal(typeof app.next, 'function') | ||
t.equal(app.state, newState) | ||
return newElement | ||
}, | ||
diff: function (target, newTarget) { | ||
t.equal(target, theTarget) | ||
diff: function (target, newElement) { | ||
t.equal(target, initialElement) | ||
t.deepEqual(newTarget, {}) | ||
t.deepEqual(newElement, newElement) | ||
}, | ||
raf: function (callback) { | ||
process.nextTick(function () { callback() }) | ||
} | ||
})(function (app) { | ||
t.deepEqual(Object.keys(app).length, 2) | ||
raf | ||
})(function (dispatch) { | ||
t.equal(dispatch.name, 'dispatch') | ||
t.equal(typeof app.dispatch, 'function') | ||
t.equal(typeof dispatch, 'function') | ||
t.equal(app.target, theTarget) | ||
app.dispatch(123) | ||
dispatch(dispatchArgumnt) | ||
}) | ||
@@ -62,64 +76,64 @@ }) | ||
test('using next', function (t) { | ||
let theTarget = {} | ||
t.plan(1) | ||
t.plan(3) | ||
require('./main.js')({ | ||
target: theTarget, | ||
target: initialElement, | ||
store: noopStore, | ||
component: function ({next}) { | ||
next(function (app) { | ||
t.deepEqual(Object.keys(app).length, 2) | ||
t.equal(typeof app.dispatch, 'function') | ||
t.equal(app.target, theTarget) | ||
next(function (target) { | ||
t.equal(target, initialElement) | ||
}) | ||
return {} | ||
return newElement | ||
}, | ||
diff: noop, | ||
raf: function (callback) { | ||
process.nextTick(function () { callback() }) | ||
} | ||
})() | ||
raf | ||
}) | ||
}) | ||
test('seed with function', function (t) { | ||
let theTarget = {} | ||
test('using dispatch', function (t) { | ||
t.plan(2) | ||
t.plan(1) | ||
require('./main.js')({ | ||
target: theTarget, | ||
store: function (seed) { | ||
seed(function (commit) { | ||
t.equal(typeof commit, 'function') | ||
target: initialElement, | ||
store: function (commit) { | ||
commit(() => initialState) | ||
return '' | ||
}) | ||
return function (arg) { | ||
t.equal(arg, dispatchArgumnt) | ||
return noop | ||
commit(function (state) { | ||
t.equal(state, initialState) | ||
return newState | ||
}) | ||
} | ||
}, | ||
component: noop, | ||
component: function ({dispatch, state}) { | ||
if (state === initialState) { | ||
process.nextTick(function () { | ||
dispatch(dispatchArgumnt) | ||
}) | ||
} | ||
return newElement | ||
}, | ||
diff: noop, | ||
raf: noop | ||
})() | ||
raf | ||
}) | ||
}) | ||
test('dispatch multiple', function (t) { | ||
let theTarget = {} | ||
t.plan(2) | ||
t.plan(4) | ||
require('./main.js')({ | ||
target: theTarget, | ||
store: function (seed) { | ||
seed('') | ||
target: initialElement, | ||
store: function (commit) { | ||
commit(() => '') | ||
return function (commit, arg) { | ||
return function (arg) { | ||
t.equal(arg, dispatchArgumnt) | ||
commit(function (state) { | ||
t.equal(arg, 123) | ||
return 'test' | ||
return newState | ||
}) | ||
@@ -129,17 +143,13 @@ } | ||
component: function (app) { | ||
t.deepEqual(Object.keys(app).length, 3) | ||
t.equal(app.state, newState) | ||
t.equal(app.state, 'test') | ||
return {} | ||
return newElement | ||
}, | ||
diff: noop, | ||
raf: function (callback) { | ||
process.nextTick(function () { callback() }) | ||
} | ||
})(function ({dispatch}) { | ||
dispatch(123) | ||
raf | ||
})(function (dispatch) { | ||
dispatch() | ||
dispatch(123) | ||
dispatch(dispatchArgumnt) | ||
}) | ||
}) |
8984
158
144