- Opinionated state management
- Type safety by design. Type inference works for both Update and Selector functions.
- Logging. Inspect where Update function was called.
- Pause observable stream, if needed.
- Always receive the whole state, if needed.
const state = new Subjective(
productState,
productStateFns,
);
const state = new Subjective(
productState,
productStateFns,
// use default Logger (dev only)
true
);
const state = new Subjective(
productState,
productStateFns,
// use custom Logger (dev only)
(updateFnName: string, payload: any, updateFnRef: Function) => {
// LOG
const data = JSON.stringify(payload);
const dataTrimmed = data.substring(0, 80);
// logging to console
console.groupCollapsed(
`%c${fnName}: %c${dataTrimmed}${
dataTrimmed.length < data.length ? '…' : ''
}`,
`color: green; font-weight: 300;`,
`color: gray; font-weight: 100;`,
);
console.log(payload);
console.log(updateFnRef);
console.groupEnd();
}
);
state.select(s => s.filter.type).subscribe();
state.select(s => s.filter.type, true).subscribe();
state.update(f => f.updateFilterType, type);
state.update(f => f.updateFilterType, type, false);
const updatedState = state.update(f => f.updateFilterType, type);
Always use immutable pattern otherwise it will not work. We can't rely on mutations since object reference is always the same.
Types are always inferred either from state class or payload parameter of the update function.
I've been thinking of this new syntax...check it out.