All files / packages/react-tao/src Provider.js

100% Statements 14/14
100% Branches 0/0
100% Functions 6/6
100% Lines 14/14
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        10x   31x 34x   15x     8x     22x         26x               26x         33x 21x 21x       12x 12x 12x              
import React, { Component, createContext } from 'react';
import PropTypes from 'prop-types';
import TAO, { Kernel } from '@tao.js/core';
 
const defaultGlobalDataContexts = new Map();
 
const makeDataContextFunctions = dataCtxMap => {
  return {
    setDataContext(key, ctx) {
      dataCtxMap.set(key, ctx);
    },
    getDataContext(key) {
      return dataCtxMap.get(key);
    },
    removeDataContext(key) {
      dataCtxMap.delete(key);
    }
  };
};
 
const Context = createContext({
  TAO,
  ...makeDataContextFunctions(defaultGlobalDataContexts)
});
 
export { Context };
 
export default class Provider extends Component {
  static propTypes = {
    TAO: PropTypes.instanceOf(Kernel).isRequired
  };
 
  constructor(props) {
    super(props);
    this._dataContexts = new Map();
    this.state = makeDataContextFunctions(this._dataContexts);
  }
 
  render() {
    const { TAO, children } = this.props;
    const dataContextFunctions = this.state;
    return (
      <Context.Provider value={{ TAO, ...dataContextFunctions }}>
        {children}
      </Context.Provider>
    );
  }
}