All files / src/domain/createNotificationsMiddleware signalr.js

6.67% Statements 1/15
0% Branches 0/1
0% Functions 0/5
6.67% Lines 1/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 40 41            1x                                                                    
import is from 'is_js';
import invariant from 'invariant';
import { hubConnection } from 'signalr-no-jquery';
import log from 'domain/log';
import Strategy from './strategy';
 
const _log = (line, method = 'info') => {
  log[method](`SignalRStrategy: ${line}`);
};
 
export class SignalRStrategy extends Strategy {
  constructor(config, accessToken) {
    super(config);
 
    invariant(is.string(config.hubUrl), 'hubUrl must be a string in config');
    invariant(is.string(config.hubName), 'hubName must be a string in config');
    invariant(is.string(config.incomingMethodName), 'incomingMethodName must be a string in config');
 
    const hubUrl = config.hubUrl.replace('{token}', accessToken);
 
    this.connection = hubConnection(hubUrl, {
      qs: { bearer_token: accessToken },
    });
 
    this.hubProxy = this.connection.createHubProxy(config.hubName);
    this.hubProxy.on(config.incomingMethodName, () => {
      _log('Push notification received.');
      this.emit('notification');
    });
 
    this.connection.start()
    .done(() => {
      _log('Connected to hub; waiting for notifications.');
    }).fail(() => {
      _log('Unable to connect to the hub!', 'warn');
    });
  }
}
 
export default SignalRStrategy;