How to use the @cycle/state.StateSource function in @cycle/state

To help you get started, we’ve selected a few @cycle/state examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sarimarton / powercycle / src / util / withLocalState.js View on Github external
const wrapper = function WithLocalState(sources) {
    const merger = createDefaultStateMerger()

    // Combine the 2 channels into 1 stream with the merger
    const state$ = xs
      .combine(
        sources[stateChannel].stream.startWith(undefined),
        sources[localChannel].stream.startWith(undefined)
      )
      .map(([g, l]) => merger.merge(g, l))
      .remember()

    const sinks = cmp({
      ...omit([localChannel])(sources),
      [stateChannel]: new StateSource(
        state$.drop(2),
        'withLocalState'
      )
    })

    // Convert the emitted reducers back to state values and run
    // it through extract
    const updated$ = !sinks[stateChannel] ? xs.never() :
      sinks[stateChannel]
        .compose(sampleCombine(state$))
        .map(([reducer, state]) => merger.extract(reducer(state)))

    // Convert the extracted state values back to reducers for the separate
    // channels
    const global$ = updated$.map(extractedState => prevState => {
      return { ...prevState, ...extractedState.global }

@cycle/state

Wraps your Cycle.js main function with reducer-driven state management

MIT
Latest version published 3 years ago

Package Health Score

65 / 100
Full package analysis