-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store management with
createStore
(#71)
* store: `createStore` function. * store (in progress) * tests for `createStore`. * full test coverage for `createStore`. * backwards compatibility for `createStore`. * Store should catch and print errors from reducers, if any. * tests for `createStore` with combined reducers. * fix for alex -_- * `combineReducers` with tests. * initialize Store by firing first init action. * implement `createStore` everywhere. * delete now-redundant code. * drop `redux`. * append `appName` to dispatched action payloads like before. * padded pretty date in logs.
- Loading branch information
Showing
20 changed files
with
770 additions
and
357 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
src/createStore.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"allow": [ | ||
"destroy" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,40 @@ | ||
import { combineReducers } from 'redux'; | ||
/* eslint-disable prefer-template */ | ||
export default function combineReducers(reducers, options = {}) { | ||
const keys = Object.keys(reducers); | ||
const opts = { | ||
console: console, | ||
...options, | ||
}; | ||
|
||
export default combineReducers; | ||
return function rootReducer(state = {}, action) { | ||
let changed = false; | ||
|
||
const fullStateTree = {}; | ||
keys.forEach(function processReducer(key) { | ||
const reducer = reducers[key]; | ||
const previousState = state[key]; | ||
let updatedState; | ||
|
||
try { | ||
updatedState = reducer(previousState, action); | ||
} catch (reducerError) { | ||
opts.console.error('Reducer for key `' + key + '` threw an error:'); | ||
throw reducerError; | ||
} | ||
|
||
if (typeof updatedState === 'undefined') { | ||
throw new Error('Reducer for key `' + key + '` returned `undefined`'); | ||
} | ||
|
||
fullStateTree[key] = updatedState; | ||
|
||
if (changed === true || updatedState !== previousState) { | ||
changed = true; | ||
} | ||
}); | ||
|
||
return changed | ||
? fullStateTree | ||
: state; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* eslint-disable no-console */ | ||
import _ from 'lodash'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
class BaseStore { | ||
constructor(options = {}) { | ||
this.options = { | ||
initialState: null, | ||
thunkArgument: null, | ||
appendAction: false, | ||
reducer: state => state, | ||
enableLogger: true, | ||
console: console, | ||
...options, | ||
}; | ||
|
||
this.internalState$ = new BehaviorSubject(this.options.initialState) | ||
.scan((previousState, action) => { | ||
let updatedState; | ||
const d = new Date(); | ||
const prettyDate = [ | ||
_.padStart(d.getHours(), 2, 0), | ||
':', | ||
_.padStart(d.getMinutes(), 2, 0), | ||
':', | ||
_.padStart(d.getSeconds(), 2, 0), | ||
'.', | ||
_.padStart(d.getMilliseconds(), 3, 0) | ||
].join(''); | ||
|
||
try { | ||
updatedState = this.options.reducer(previousState, action); | ||
} catch (error) { | ||
if (action && action.type) { | ||
this.options.console.error(`Error processing @ ${prettyDate} ${action.type}:`); | ||
} | ||
this.options.console.error(error); | ||
|
||
return previousState; | ||
} | ||
|
||
// logger in non-production mode only | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (this.options.enableLogger === true) { | ||
const groupName = `action @ ${prettyDate} ${action.type}`; | ||
|
||
if (typeof this.options.console.group === 'function') { | ||
this.options.console.group(groupName); | ||
} | ||
|
||
this.options.console.log('%cprevious state', 'color: #9e9e9e; font-weight: bold;', previousState); | ||
this.options.console.log('%caction', 'color: #33c3f0; font-weight: bold;', action); | ||
this.options.console.log('%ccurrent state', 'color: #4cAf50; font-weight: bold;', updatedState); | ||
|
||
if (typeof this.options.console.groupEnd === 'function') { | ||
this.options.console.groupEnd(); | ||
} | ||
} | ||
} | ||
|
||
return updatedState; | ||
}); | ||
this.exposedState$ = new BehaviorSubject(); | ||
|
||
this.cachedState = Object.assign({}, this.options.initialState); | ||
this.subscription = this.internalState$ | ||
.subscribe((state) => { | ||
this.cachedState = state; | ||
this.exposedState$.next(state); | ||
}); | ||
|
||
this.dispatch({ type: '__FRINT_INIT__' }); | ||
} | ||
|
||
getState$() { | ||
return this.exposedState$; | ||
} | ||
|
||
getState = () => { | ||
this.options.console.warn('[DEPRECATED] `Store.getState` has been deprecated, and kept for consistency purpose only with v0.x'); | ||
|
||
return this.cachedState; | ||
} | ||
|
||
dispatch = (action) => { | ||
if (typeof action === 'function') { | ||
return action( | ||
this.dispatch, | ||
this.getState, | ||
this.options.thunkArgument | ||
); | ||
} | ||
|
||
const payload = ( | ||
this.options.appendAction && | ||
_.isPlainObject(this.options.appendAction) | ||
) | ||
? { ...this.options.appendAction, ...action } | ||
: action; | ||
|
||
return this.internalState$.next(payload); | ||
} | ||
|
||
subscribe(callback) { | ||
this.options.console.warn('[DEPRECATED] `Store.subscribe` has been deprecated, and kept for consistency purpose only with v0.x'); | ||
|
||
const subscription = this.getState$() | ||
.subscribe((state) => { | ||
callback(state); | ||
}); | ||
|
||
return function unsubscribe() { | ||
subscription.unsubscribe(); | ||
}; | ||
} | ||
|
||
destroy() { | ||
if (this.subscription) { | ||
this.subscription.unsubscribe(); | ||
} | ||
} | ||
} | ||
|
||
export default function createStore(options = {}) { | ||
class Store extends BaseStore { | ||
constructor(opts = {}) { | ||
super(_.merge( | ||
options, | ||
opts | ||
)); | ||
} | ||
} | ||
|
||
return Store; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.