Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}) {
let currentReducer = reducer
const dispatch = createEvent<{+type: string, payload?: any, ...}>('dispatch')
const update = createEvent('update')
const store = createStore(defaultState)
store.on(dispatch, (state, event) => {
try {
return currentReducer(state, event)
} catch (err) {
console.error(err)
}
return state
})
forward({
from: store,
to: update,
})
const wrapper = {}
//$off
wrapper[$$observable] = store[$$observable]
wrapper.getState = store.getState
wrapper.dispatch = dispatch
/**
* NOTE that's mean that symbol.observable subscribers
* will always been called before ones which used .subscribe
*/
wrapper.subscribe = (listener: (data: T) => any) => {
if (typeof listener !== 'function') {
throw new Error('Expected the listener to be a function.')
}
const current = history.map(({ states, head }) => states[head]);
const shouldSave = createStore({
next: initialState,
prev: initialState
})
.on(store, ({ next: prev }, next) => ({ next, prev }))
.map(({ next, prev }) => filter(next, prev));
guard({
source: sample(store, merge(events)),
filter: shouldSave,
target: push
});
forward({
from: current,
to: store
});
return { undo, redo, clear, history };
}