Global

Methods

createState(initialState) → {State}

Creates a State object
Parameters:
Name Type Description
initialState object The initial state of the application
Source:
Returns:
Type
State
Example
const initialState = { foo: { bar: 42 }, foobaz: 'baz' };

const state = createState(initialState);

const updateBar = value => state => ({
  ...state,
  foo: {
    bar: value,
  }
});

const updateFooBaz = (value) => function updateFooBaz(state) = {
  return {
    ...state,
    foobaz: value,
  };
};

const editBarEverySecDuring10sec$ = timer(0, 1000).pipe(
 take(10),
 map(updateBar)
);

const editFoobazWhenBarIsOdd$ = state.update.$('updateBar').pipe(
  filter(({ foo: { bar } }) => bar % 2 === 1),
  map(({ foo: { bar } }) => updateFooBaz(`baz${bar}`))
)


const run = () => state.combineWorkflows(
  editBarEverySec$,
  editFoobazWhenBarIsOdd$,
);

state.select$('foobaz').subscribe(console.log);

run();
// will output : baz1, baz3, baz5, baz7, baz9

Type Definitions

State

Type:
  • Object
Properties:
Name Type Description
$ Rx.Subject The state of the application as a Rxjs hot stream. When subscribed, the last state is returned
select$ StateSelector
value Object The actual value of the application's state
combineWorkflows WorkflowsCombiner
update UpdateDispatcher
Source:

StateSelector(…path)

A function to create a stream of a slice of the state, given a path as strings
Parameters:
Name Type Attributes Description
path string <repeatable>
The string path to the desired value in the state, you way pass a custom compare function as a first argument, this function will be used to compare the previous state with the new one and must return "true" if you want to consider the two states equals
Source:
Example
// given a state shape : { foo: { bar: 'baz' } }, returns a stream of foo.bar value
select$('foo', 'bar')

UpdateDispatcher(Updater)

A function to dispatch Updater or react to updater being dispatched
Parameters:
Name Type Description
Updater function
Properties:
Name Type Description
$ function A function accepting an updater name (its function.name) as argument and returning a stream of its resultint state updates
Source:
Example
const store = createState({ baz: null, foo: null });

const updateBaz = state => ({
  ...state,
  baz: 42
});

const updateFoo = foo => function updateFoo(state) {
  return {
    ...state,
    foo,
  };
};

state.update.$('updateFoo').subscribe(nextState => console.log('updateFoo has been dispatched, next state is', nextState));
);

state.update.$('updateBaz').subscribe(nextState => console.log('updateBaz has been dispatched, next state is', nextState));
);

state.update(updateBaz);
state.update(updateFoo('new foo'));

Updater(state) → {object}

A function use to update the state of the application, this function should remain pure, side-effects should be done in Workflows
Parameters:
Name Type Description
state object the actual state of the application
Source:
Returns:
newState - the new state of the application
Type
object

Workflow

A Rx.Observable that must emit Updaters
Type:
  • Rx.Observable
Source:

WorkflowsCombiner(…workflows) → {Workflow}

A function to combine multiple Workflows together
Parameters:
Name Type Attributes Description
workflows Workflow <repeatable>
the workflows to combine together
Source:
Returns:
the combined workflow
Type
Workflow