All files / src createStore.js

98.44% Statements 63/64
88.89% Branches 16/18
100% Functions 18/18
98.31% Lines 58/59
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 1241x 1x 1x 1x 1x           34x 34x   34x   34x 148x 37x 37x 37x             68x 71x 5x 5x               102x         34x 34x 34x 71x   34x 2x 143x   34x 34x     43x 34x         31x   31x 31x   31x 12x       12x   12x 12x 12x         71x 71x 26x 26x       34x 34x 34x   34x     71x 71x   34x   34x       34x   34x 34x 3x             34x       8x 8x 8x      
import Rx from '@evo/rx';
import React from 'react';
import compose from './compose';
import { INIT } from './coreActions';
import { StreamWrapper, dispatch } from './streamWrapper.js';
 
 
function makeStreamState(
  initialState, update, streamWrapper, middlewares,
) {
  const middlewaresWithStream =  middlewares.map(middleware => middleware(streamWrapper));
  const composedMiddlewares = compose.apply(undefined, middlewaresWithStream);
 
  let dispatcher = dispatch(streamWrapper.stream, update, null);
 
  return dispatcher
    .scan((({state}, action) => {
      const appliedMiddleware = composedMiddlewares(action);
      const {state: newState, effects, params} = appliedMiddleware(state);
      return {state: newState, effects, params};
    }), {state: initialState})
    .startWith({state: initialState, effects: []});
}
 
 
function streamWithEffects(stream, streamWrapper) {
    const { Observable, helpers: { isPromise } } = Rx;
    return stream.do(({effects}) =>
        effects.forEach((e) => Observable.spawn(e(streamWrapper))
        .flatMap((obs) => obs instanceof Observable || isPromise(obs) ? obs : [])
        .subscribe(
          Rx.helpers.noop,
          console.error.bind(console)
        ))
      )
}
 
export function createStore(initialState, update, options) {
  let {
    outerEventStream,
    stateSerializer,
    middlewares=[],
  } = options || {};
  let state = initialState;
  const setState = (newState) => {
    state = newState;
  };
  const getState = stateSerializer
    ? () => stateSerializer(state)
    : () => state;
 
  let currentListeners = [];
  let nextListeners = currentListeners;
 
  function ensureCanMutateNextListeners() {
    if (nextListeners === currentListeners) {
      nextListeners = currentListeners.slice();
    }
  }
 
  function subscribe(listener) {
    let isSubscribed = true;
 
    ensureCanMutateNextListeners();
    nextListeners.push(listener);
 
    return function unsubscribe() {
      Iif (!isSubscribed) {
        return
      }
 
      isSubscribed = false;
 
      ensureCanMutateNextListeners();
      const index = nextListeners.indexOf(listener);
      nextListeners.splice(index, 1);
    }
  }
 
  function broadcast() {
    const listeners = currentListeners = nextListeners;
    for (let i = 0; i < listeners.length; i++) {
      const listener = listeners[i];
      listener();
    }
  }
 
  const eventStream = new Rx.Subject();
  const tanokStream = new StreamWrapper(eventStream, null);
  tanokStream.metadata.push(null);
 
  const streamState = makeStreamState(
    initialState, update, tanokStream, middlewares,
  )
  .do(({ state }) => setState(state))
  .do(() => broadcast());
 
  const renderedWithEffects = streamWithEffects(streamState, tanokStream);
 
  tanokStream.disposable = renderedWithEffects.subscribe(
    Rx.helpers.noop,
    console.error.bind(console)
  );
  tanokStream.send(INIT);
 
  let outerEventDisposable;
  if (outerEventStream) {
    outerEventDisposable = outerEventStream.subscribe(
      tanokStream.stream.onNext.bind(tanokStream.stream),
      console.error.bind(console)
    )
  }
 
 
  return [tanokStream, {
    getState,
    subscribe,
    shutdown: () => {
      tanokStream.onShutdown();
      tanokStream.disposable.dispose();
      outerEventDisposable && outerEventDisposable.dispose();
    },
  }];
}