fluorine-lib
Version:
Reactive state and side effect management for React using a single stream of actions
45 lines (36 loc) • 923 B
JavaScript
var _state = {
doNext: function doNext(action) {
var reducer = this.reducer;
var nextState = reducer(this.state, action);
if (nextState === this.state) {
return this;
}
this.next = Object.assign(Object.create(_state), {
state: nextState,
action: action,
reducer: reducer
});
return this.next;
}
};
export function createState(reducer, init) {
return Object.assign(Object.create(_state), {
reducer: reducer,
state: init
});
}
export function filterActions(start, filter) {
var reducer = start.reducer;
var anchor = start;
while (anchor.next) {
if (filter(anchor.next.action)) {
// Recompute state
anchor.next.state = reducer(anchor.state, anchor.next.action);
} else {
// Erase filtered action from history
anchor.next.state = anchor.state;
anchor.next.action = {};
}
anchor = anchor.next;
}
}