All files / lib diagnostics_channel.js

100% Statements 145/145
96.66% Branches 29/30
92.3% Functions 12/13
100% Lines 145/145

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 17x 17x 17x 153x 153x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 153x 153x 12x 12x 153x 153x 13x 14x 14x 14x 14x 1x 1x 1x 1x 14x 13x 153x 153x 153x 153x 22529x 22529x 22529x 153x 153x 4x 4x 4x 4x 153x 153x 16x 16x 16x 16x 153x 153x 2x 2x 153x 153x 41803x 41803x 153x 153x 153x 153x 153x 153x 22543x 22543x 22543x 22543x 22543x 22530x 22543x 1x 1x 22529x 22529x 22529x 22529x 22543x 153x 2x 2x 2x 2x 2x 153x 2x 2x 2x 1x 1x 1x 1x 1x 2x 153x 3x 3x 3x 3x 3x 1x 1x 2x 2x 3x 153x 153x 153x 153x 153x 153x 153x 153x  
'use strict';
 
const {
  ArrayPrototypeIndexOf,
  ArrayPrototypePush,
  ArrayPrototypeSplice,
  ObjectCreate,
  ObjectGetPrototypeOf,
  ObjectSetPrototypeOf,
  SymbolHasInstance,
} = primordials;
 
const {
  codes: {
    ERR_INVALID_ARG_TYPE,
  }
} = require('internal/errors');
const {
  validateFunction,
} = require('internal/validators');
 
const { triggerUncaughtException } = internalBinding('errors');
 
const { WeakReference } = internalBinding('util');
 
// TODO(qard): should there be a C++ channel interface?
class ActiveChannel {
  subscribe(subscription) {
    validateFunction(subscription, 'subscription');
    ArrayPrototypePush(this._subscribers, subscription);
  }
 
  unsubscribe(subscription) {
    const index = ArrayPrototypeIndexOf(this._subscribers, subscription);
    if (index === -1) return false;
 
    ArrayPrototypeSplice(this._subscribers, index, 1);
 
    // When there are no more active subscribers, restore to fast prototype.
    if (!this._subscribers.length) {
      // eslint-disable-next-line no-use-before-define
      ObjectSetPrototypeOf(this, Channel.prototype);
    }
 
    return true;
  }
 
  get hasSubscribers() {
    return true;
  }
 
  publish(data) {
    for (let i = 0; i < this._subscribers.length; i++) {
      try {
        const onMessage = this._subscribers[i];
        onMessage(data, this.name);
      } catch (err) {
        process.nextTick(() => {
          triggerUncaughtException(err, false);
        });
      }
    }
  }
}
 
class Channel {
  constructor(name) {
    this._subscribers = undefined;
    this.name = name;
  }
 
  static [SymbolHasInstance](instance) {
    const prototype = ObjectGetPrototypeOf(instance);
    return prototype === Channel.prototype ||
           prototype === ActiveChannel.prototype;
  }
 
  subscribe(subscription) {
    ObjectSetPrototypeOf(this, ActiveChannel.prototype);
    this._subscribers = [];
    this.subscribe(subscription);
  }
 
  unsubscribe() {
    return false;
  }
 
  get hasSubscribers() {
    return false;
  }
 
  publish() {}
}
 
const channels = ObjectCreate(null);
 
function channel(name) {
  let channel;
  const ref = channels[name];
  if (ref) channel = ref.get();
  if (channel) return channel;
 
  if (typeof name !== 'string' && typeof name !== 'symbol') {
    throw new ERR_INVALID_ARG_TYPE('channel', ['string', 'symbol'], name);
  }
 
  channel = new Channel(name);
  channels[name] = new WeakReference(channel);
  return channel;
}
 
function subscribe(name, subscription) {
  const chan = channel(name);
  channels[name].incRef();
  chan.subscribe(subscription);
}
 
function unsubscribe(name, subscription) {
  const chan = channel(name);
  if (!chan.unsubscribe(subscription)) {
    return false;
  }
 
  channels[name].decRef();
  return true;
}
 
function hasSubscribers(name) {
  let channel;
  const ref = channels[name];
  if (ref) channel = ref.get();
  if (!channel) {
    return false;
  }
 
  return channel.hasSubscribers;
}
 
module.exports = {
  channel,
  hasSubscribers,
  subscribe,
  unsubscribe,
  Channel
};