All files / src tanokInReact.js

95.24% Statements 20/21
70% Branches 7/10
100% Functions 3/3
94.74% Lines 18/19
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 531x   1x 1x     3x     21x               21x   3x       3x     3x 3x 3x 3x     3x 3x       3x 3x 3x       5x                  
import React from 'react';
 
import { createStore } from './createStore';
import { Root } from "./components/root";
 
 
const identity = (x) => x;
 
export class TanokInReact extends React.Component {
  constructor(props) {
    super(props);
    const {
      initialState, update, view,
      middlewares = [],
      onNewState,
      outerEventStream,
      stateSerializer,
    } = props;
 
    const [tanokStream, store] = createStore(initialState, update, {
      middlewares,
      outerEventStream,
    });
    Iif (onNewState) {
      this.storeSubOnNewState = store.subscribe(onNewState);
    }
    this.view = view;
    this.tanokStream = tanokStream;
    this.store = store;
    this.stateSerializer = stateSerializer || identity;
  }
 
  componentDidMount() {
    this.storeSubOnStoreUpd = this.store.subscribe(() => this.forceUpdate());
  }
 
  componentWillUnmount() {
    this.storeSubOnNewState && this.storeSubOnNewState();
    this.storeSubOnStoreUpd();
    this.store.shutdown();
  }
 
  render() {
    return (
      <Root store={this.store} tanokStream={this.tanokStream}>
          <this.view
            tanokStream={this.tanokStream}
            {...this.stateSerializer(this.store.getState())} />
      </Root>
    )
  }
}