Skip to content

Commit

Permalink
api: invert freeze args
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed Jul 5, 2016
1 parent 0445ae4 commit 23ff5f1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,22 @@ series of private accessors:

### state = store.state(opts)
Get the current state from the store. Opts can take the following values:
- __noFreeze:__ default: false. Don't freeze returned state using
`Object.freeze()`. Useful for optimizing performance in production builds.
- __freeze:__ default: true. Set to false to not freeze state in handlers
using `Object.freeze()`. Useful for optimizing performance in production
builds.
- __state:__ pass in a state object that will be merged with the state returned
from the store. Useful for rendering in Node.

### send = createSend(name) = store.start(opts)
Start the store and get a `createSend(name)` function. Pass a unique `name` to
`createSend()` to get a `send()` function. Opts can take the following values:
- __noSubscriptions:__ default: false. Don't register `subscriptions` when
starting the application. Useful to delay `init` functions until the DOM has
loaded.
- __noEffects:__ default: false. Don't register `effects` when
- __subscriptions:__ default: true. Set to false to not register
`subscriptions` when starting the application. Useful to delay `init`
functions until the DOM has loaded.
- __effects:__ default: true. Set to `false` to not register `effects` when
starting the application. Useful when only wanting the initial `state`
- __noReducers:__ default: false. Don't register `reducers` when
- __reducers:__ default: true. Set to false to not register `reducers` when
starting the application. Useful when only wanting the initial `state`
- __noFreeze:__ default: false. Don't freeze state in handlers using
`Object.freeze()`. Useful for optimizing performance in production builds.

### send(name, data?)
Send a new action to the models with optional data attached. Namespaced models
Expand Down
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function dispatcher (handlers) {
}
})
return xtend(_state, xtend(opts.state, nsState))
} else if (opts.noFreeze) {
} else if (opts.freeze === false) {
return xtend(_state)
} else {
return Object.freeze(xtend(_state))
Expand All @@ -76,16 +76,16 @@ function dispatcher (handlers) {
// register values from the models
models.forEach(function (model) {
const ns = model.namespace
if (!stateCalled && model.state && !opts.noState) {
if (!stateCalled && model.state && opts.state !== false) {
apply(ns, model.state, _state)
}
if (!reducersCalled && model.reducers && !opts.noReducers) {
if (!reducersCalled && model.reducers && opts.reducers !== false) {
apply(ns, model.reducers, reducers)
}
if (!effectsCalled && model.effects && !opts.noEffects) {
if (!effectsCalled && model.effects && opts.effects !== false) {
apply(ns, model.effects, effects)
}
if (!subsCalled && model.subscriptions && !opts.noSubscriptions) {
if (!subsCalled && model.subscriptions && opts.subscriptions !== false) {
apply(ns, model.subscriptions, subscriptions, createSend, onError)
}
})
Expand Down
20 changes: 10 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,38 @@ tape('api: createSend = store.start(opts)', (t) => {
t.doesNotThrow(store.start.bind(null), 'undefined should not throw')
})

t.test('opts.noState should not register state', (t) => {
t.test('opts.state = false should not register state', (t) => {
t.plan(1)
const store = barracks()
store.model({ state: { foo: 'bar' } })
store.start({ noState: true })
store.start({ state: false })
const state = store.state()
t.deepEqual(state, {}, 'no state returned')
})

t.test('opts.noEffects should not register effects', (t) => {
t.test('opts.effects = false should not register effects', (t) => {
t.plan(1)
const store = barracks()
store.model({ effects: { foo: noop } })
store.start({ noEffects: true })
store.start({ effects: false })
const effects = Object.keys(store._effects)
t.deepEqual(effects.length, 0, 'no effects registered')
})

t.test('opts.noReducers should not register effects', (t) => {
t.test('opts.reducers = false should not register reducers', (t) => {
t.plan(1)
const store = barracks()
store.model({ reducers: { foo: noop } })
store.start({ noReducers: true })
store.start({ reducers: false })
const reducers = Object.keys(store._reducers)
t.deepEqual(reducers.length, 0, 'no reducers registered')
})

t.test('opts.noSubscriptions should not register subscriptions', (t) => {
t.test('opts.subscriptions = false should not register subs', (t) => {
t.plan(1)
const store = barracks()
store.model({ subscriptions: { foo: noop } })
store.start({ noSubscriptions: true })
store.start({ subscriptions: false })
const subscriptions = Object.keys(store._subscriptions)
t.deepEqual(subscriptions.length, 0, 'no subscriptions registered')
})
Expand Down Expand Up @@ -119,12 +119,12 @@ tape('api: state = store.state()', (t) => {
t.deepEqual(state, expected, 'state was frozen')
})

t.test('noFreeze should not freeze objects', (t) => {
t.test('freeze = false should not freeze objects', (t) => {
t.plan(1)
const store = barracks()
store.model({ state: { foo: 'bar' } })
store.start()
const state = store.state({ noFreeze: true })
const state = store.state({ freeze: false })
state.baz = 'bin'
const expected = { foo: 'bar', baz: 'bin' }
t.deepEqual(state, expected, 'state was not frozen')
Expand Down

0 comments on commit 23ff5f1

Please sign in to comment.