All files / src core.js

100% Statements 20/20
75% Branches 3/4
100% Functions 3/3
100% Lines 20/20
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 431x 1x   1x 1x   9x 3x 3x 3x 3x   3x   3x 3x 6x         6x             3x 3x   3x         3x 3x 3x        
import React from 'react';
import ReactDOM from 'react-dom';
 
import { Root } from './components/root';
import { createStore } from './createStore';
 
export function tanok(initialState, update, view, options) {
  let container = options.container;
  Eif (!container) {
    container = document.createElement('div');
    document.body.appendChild(container);
  }
  const [tanokStream, store] = createStore(initialState, update, options);
 
  let component;
  const render = () => {
    const createdView = React.createElement(
      view, {
        tanokStream,
        ...store.getState(),
      });
    component = ReactDOM.render(
      <Root store={store} tanokStream={tanokStream}>
        {createdView}
      </Root>,
      container
    );
  };
  render();
  const sub = store.subscribe(render);
 
  return {
    component,
    tanokStream,
    store,
    shutdown: () => {
      sub();
      store.shutdown();
      container && ReactDOM.unmountComponentAtNode(container);
    },
  };
}