All files / src component.js

94.12% Statements 16/17
68.75% Branches 11/16
100% Functions 3/3
93.33% Lines 14/15
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 401x 1x   1x                             5x 5x   5x   5x 1x       1x 1x     5x 1x 1x     5x    
import React from 'react';
import PropTypes from 'prop-types';
 
import { StreamWrapper } from './streamWrapper.js';
 
/**
 * Decorator used with class-based React components.
 * It provides all the required props and helpers for tanok internals.
 *
 * Usage example:
 *
 * @tanokComponent
 * class MyComponent extends React.Component {
 *    ... your component methods.
 * }
 *
 * */
export function tanokComponent(target) {
  target.propTypes = target.propTypes || {};
  target.propTypes.tanokStream = PropTypes.instanceOf(StreamWrapper);
 
  target.displayName = `TanokComponent(${target.displayName || target.name})`;
 
  target.prototype.send = function send(action, payload, metadata = null) {
    Iif (metadata !== null) {
      console.error('Hey! You no longer can pass metadata `.send()`, use `.sub()`');
    }
 
    const stream = this.props.tanokStream;
    stream.send(action, payload);
  };
 
  target.prototype.sub = function sub(name, metadata = null) {
    const stream = this.props.tanokStream;
    return stream && stream.subWithMeta(name, metadata);
  };
 
  return target;
}