Readme
Optice
Like redux, but no reducers
, no actions
Usage
npm install optice
const initialState = user: name: '' email: '' company: name: '' okato: 0 const store = const userLens = Lconst companyLens = L const nameLens = Lconst emailLens = L// same as `L.prop('email')` const userNameLens = Lconst companyNameLens = L const fetchUser = async name: 'Foo Bar' email: 'foo.bar@company.com' const loadUser = async { }
In console should be this output:
BEFORE { name: '', email: '' } { name: '', okato: 0 }
UPDATE { name: 'Foo Bar', email: 'foo.bar@company.com' } { name: '', okato: 0 }
UPDATE { name: 'Foo Bar', email: 'foo.bar@company.com' } { name: 'Company', okato: 0 }
API
createStore
createStore(initialState): Store
Create new store object.
Store.execute
store.execute(command: Function, ...args: any[]): result
Run command function with store methods ({ getState, setState, updateState, readState, execute })
and passed arguments.
Immediatly return result of command.
const command = { // read, update state // or execute another command return 1 + a + b} store === 4
Store.getState
store.getState(): State
Just return current state
Store.readState
store.readState(lens: Lens): Value
Read value from state through lens.
const lens = L const initialState = data: 1 const store = const value = store console
Store.setState
store.setState(newState: State): void
It notify all subscribers. Replace state, returns nothing.
const initial = a: 1 b: 2 const store = storestore
Store.subscribe
store.subscribe(listener: Function): Function
Add listener to subscribers list, returns unsubscribe
function.
const store = const unsubscribe = store store // > update { a: 2 } store // nothing
Store.updateState
store.updateState(lens: Lens, valueOrFunc: any | Function): void
It notify all subscribers. Update piece of state through lens.
If function passed update state with L.over
, else use L.write
to just set value.
const lens = L const store = storeconsole storeconsole
L.create
L.create(getter: Function, setter: Function): Lens
Create new lens.
Getter is just function that received state. Should return piece of state.
Setter is function that received passed value, return function that received state. Should return new version of passed state.
Getter and Setter should be pure functions.
const lens = L
L.view
L.view(state: State, lens: Lens): any
Read value from state through lens.
const lens = L const state = value: 'foo' const value = console
L.write
L.write(state: State, lens: Lens, value: any): State
Immutable update piece of state through lens. Return new version of state.
const state = foo: bar: 1 const fooLens = Lconst barLens = Lconst fooBarLens = L const newState = Lconsole
L.over
L.over(state: State, lens: Lens, fn: (value) => value): State
Like L.write
but use function to update value. Return new version of state.
const state = foo: 100 const fooLens = Lconst updater = value + value const newState = Lconsole
L.compose
L.compose(...lenses: Lens[]): Lens
L.compose can be used only for this lenses
Perform lens composition. Returns one lens.
If passed no lens, returns empty lens L.create(s => s, v => s => s)
.
If passed one lens, returns its.
const a = L const b = L const ab = L // same as const ab2 = L
L.prop
L.prop(name: string): Lens
Makes lens to read/write property.
const fooLens = L// Same asconst fooLens2 = L