Skip to content

Commit eb4a612

Browse files
committed
Update config options to always be false in prod
Update API docs with new options
1 parent b14be97 commit eb4a612

File tree

6 files changed

+68
-38
lines changed

6 files changed

+68
-38
lines changed

docs/src/docs/07-api.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,38 @@ var reactor = Nuclear.Reactor(config)
1919

2020
**Configuration Options**
2121

22-
`config.debug` Boolean - if true it will log the entire app state for every dispatch.
22+
`config.debug` Boolean - if true it will enabled logging for dispatches and throw Errors in various circumstances described below.
23+
24+
**config.options** (added in 1.3)
25+
26+
If `config.debug` is true then all of the options below will be enabled.
27+
28+
`logDispatches` (default=`false`) console.logs for every action. If disabled `logAppState` and `logDirtyStores` will be ignored, as no dispatch logging is occurring.
29+
30+
`logAppState` (default=`false`) console.logs a snapshot of the entire app state after every dispatch. Disabling this can improve performance.
31+
32+
`logDirtyStores` (default=`false`) console.logs what stores have changed after each dispatched action.
33+
34+
`throwOnUndefinedDispatch` (default=`false`) if true, throws an Error if a store ever returns undefined.
35+
36+
`throwOnNonImmutableStore` (default=`false`) if true, throws an Error if a store returns a non-immutable value. Javascript primitive such as `String`, `Boolean` and `Number` count as immutable.
37+
38+
`throwOnDispatchInDispatch` (default=`true`) if true, throws an Error if a dispatch occurs in a change observer.
39+
40+
**Example**
41+
42+
```javascript
43+
var reactor = new Nuclear.Reactor({
44+
debug: true,
45+
options: {
46+
// do not log entire app state
47+
logAppState: false,
48+
// allow dispatch in dispatch
49+
throwOnDispatchInDispatch: false,
50+
},
51+
})
52+
```
53+
2354

2455
#### `Reactor#dispatch(messageType, messagePayload)`
2556

src/reactor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class Reactor {
110110
*/
111111
dispatch(actionType, payload) {
112112
if (this.__batchDepth === 0) {
113-
if (!fns.getOption(this.reactorState, 'allowDispatchInDispatch')) {
113+
if (fns.getOption(this.reactorState, 'throwOnDispatchInDispatch')) {
114114
if (this.__isDispatching) {
115115
this.__isDispatching = false
116116
throw new Error('Dispatch may not be called while a dispatch is in progress')

src/reactor/fns.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function registerStores(reactorState, stores) {
3434

3535
const initialState = store.getInitialState()
3636

37-
if (!getOption(reactorState, 'allowNonImmutableStores') && !isImmutableValue(initialState)) {
37+
if (getOption(reactorState, 'throwOnNonImmutableStore') && !isImmutableValue(initialState)) {
3838
throw new Error('Store getInitialState() must return an immutable value, did you forget to call toImmutable')
3939
}
4040

@@ -74,7 +74,7 @@ export function dispatch(reactorState, actionType, payload) {
7474
throw e
7575
}
7676

77-
if (!getOption(reactorState, 'allowUndefinedDispatch') && newState === undefined) {
77+
if (getOption(reactorState, 'throwOnUndefinedDispatch') && newState === undefined) {
7878
const errorMsg = 'Store handler must return a value, did you forget a return statement'
7979
logging.dispatchError(reactorState, errorMsg)
8080
throw new Error(errorMsg)
@@ -275,10 +275,10 @@ export function reset(reactorState) {
275275
storeMap.forEach((store, id) => {
276276
const storeState = prevState.get(id)
277277
const resetStoreState = store.handleReset(storeState)
278-
if (!getOption(reactorState, 'allowUndefinedDispatch') && resetStoreState === undefined) {
278+
if (getOption(reactorState, 'throwOnUndefinedDispatch') && resetStoreState === undefined) {
279279
throw new Error('Store handleReset() must return a value, did you forget a return statement')
280280
}
281-
if (!getOption(reactorState, 'allowNonImmutableStores') && !isImmutableValue(resetStoreState)) {
281+
if (getOption(reactorState, 'throwOnNonImmutableStore') && !isImmutableValue(resetStoreState)) {
282282
throw new Error('Store reset state must be an immutable value, did you forget to call toImmutable')
283283
}
284284
reactorState.setIn(['state', id], resetStoreState)

src/reactor/records.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,27 @@ export const PROD_OPTIONS = Map({
77
logAppState: false,
88
// logs what stores changed after a dispatch
99
logDirtyStores: false,
10-
// if false, throw an error if a store returns undefined
11-
allowUndefinedDispatch: true,
12-
// if false, throw an error if a store returns undefined
13-
allowNonImmutableStores: true,
14-
// if false throw when dispatching in dispatch
15-
allowDispatchInDispatch: true,
10+
// if true, throws an error if a store returns undefined
11+
throwOnUndefinedDispatch: false,
12+
// if true, throws an error if a store returns undefined
13+
throwOnNonImmutableStore: false,
14+
// if true, throws when dispatching in dispatch
15+
throwOnDispatchInDispatch: false,
16+
})
17+
18+
export const DEBUG_OPTIONS = Map({
19+
// logs information for each dispatch
20+
logDispatches: true,
21+
// log the entire app state after each dispatch
22+
logAppState: true,
23+
// logs what stores changed after a dispatch
24+
logDirtyStores: true,
25+
// if true, throws an error if a store returns undefined
26+
throwOnUndefinedDispatch: true,
27+
// if true, throws an error if a store returns undefined
28+
throwOnNonImmutableStore: true,
29+
// if true, throws when dispatching in dispatch
30+
throwOnDispatchInDispatch: true,
1631
})
1732

1833
export const ReactorState = Record({
@@ -38,19 +53,3 @@ export const ObserverState = Record({
3853

3954
nextId: 1,
4055
})
41-
42-
export const DEBUG_OPTIONS = Map({
43-
// logs information for each dispatch
44-
logDispatches: true,
45-
// log the entire app state after each dispatch
46-
logAppState: true,
47-
// logs what stores changed after a dispatch
48-
logDirtyStores: true,
49-
// if false, throw an error if a store returns undefined
50-
allowUndefinedDispatch: false,
51-
// if false, throw an error if a store returns undefined
52-
allowNonImmutableStores: false,
53-
// if false throw when dispatching in dispatch
54-
allowDispatchInDispatch: false,
55-
})
56-

tests/reactor-fns-tests.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,18 +505,18 @@ describe('reactor fns', () => {
505505
it('should parse the option value in a reactorState', () => {
506506
const reactorState = new ReactorState({
507507
options: Map({
508-
allowUndefinedDispatch: true,
508+
throwOnUndefinedDispatch: true,
509509
}),
510510
})
511511

512-
const result = fns.getOption(reactorState, 'allowUndefinedDispatch')
512+
const result = fns.getOption(reactorState, 'throwOnUndefinedDispatch')
513513
expect(result).toBe(true)
514514
})
515515

516516
it('should throw an error if the option doesn\'t', () => {
517517
const reactorState = new ReactorState({
518518
options: Map({
519-
allowUndefinedDispatch: true,
519+
throwOnUndefinedDispatch: true,
520520
}),
521521
})
522522

tests/reactor-tests.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ describe('Reactor', () => {
3333
expect(getOption(reactor.reactorState, 'logDispatches')).toBe(true)
3434
expect(getOption(reactor.reactorState, 'logAppState')).toBe(false)
3535
expect(getOption(reactor.reactorState, 'logDirtyStores')).toBe(false)
36-
expect(getOption(reactor.reactorState, 'allowUndefinedDispatch')).toBe(true)
37-
expect(getOption(reactor.reactorState, 'allowNonImmutableStores')).toBe(true)
38-
expect(getOption(reactor.reactorState, 'allowDispatchInDispatch')).toBe(true)
36+
expect(getOption(reactor.reactorState, 'throwOnUndefinedDispatch')).toBe(false)
37+
expect(getOption(reactor.reactorState, 'throwOnNonImmutableStore')).toBe(false)
38+
expect(getOption(reactor.reactorState, 'throwOnDispatchInDispatch')).toBe(false)
3939
})
4040
it('should override DEBUG options', () => {
4141
var reactor = new Reactor({
4242
debug: true,
4343
options: {
4444
logDispatches: false,
45-
allowDispatchInDispatch: true,
45+
throwOnDispatchInDispatch: false,
4646
}
4747
})
4848
expect(getOption(reactor.reactorState, 'logDispatches')).toBe(false)
4949
expect(getOption(reactor.reactorState, 'logAppState')).toBe(true)
5050
expect(getOption(reactor.reactorState, 'logDirtyStores')).toBe(true)
51-
expect(getOption(reactor.reactorState, 'allowUndefinedDispatch')).toBe(false)
52-
expect(getOption(reactor.reactorState, 'allowNonImmutableStores')).toBe(false)
53-
expect(getOption(reactor.reactorState, 'allowDispatchInDispatch')).toBe(true)
51+
expect(getOption(reactor.reactorState, 'throwOnUndefinedDispatch')).toBe(true)
52+
expect(getOption(reactor.reactorState, 'throwOnNonImmutableStore')).toBe(true)
53+
expect(getOption(reactor.reactorState, 'throwOnDispatchInDispatch')).toBe(false)
5454
})
5555
})
5656

0 commit comments

Comments
 (0)