# All files / lib/client pubsub.js

88.71% Statements 55/62
72.22% Branches 13/18
81.25% Functions 13/16
88.71% Lines 55/62

    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
    146
    147

    1x

    1x
    19x

    19x

    1027x

    1027x
    1025x
    1025x
    1025x

    1027x
    1027x
    3081x

    1027x
    1027x

    1027x

    20020x
    20020x

    20020x

    20020x

    20020x

    20020x
    20020x

    20020x
    20020x

    20020x

    1027x

    1027x

    1027x

    1027x

    1027x
    1025x
    1025x
    1025x

    2x

    1027x
    2054x

    1027x
    1027x
    1027x

    1027x
    1027x
    1027x
    1027x

    1027x

    4x

    19x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    2x'use strict';

    Object.defineProperty(exports, "__esModule", {
      value: true
    });

    exports.default = function (opts) {
      var subs = [
        /*
          {
            topic,      // name of the topic subscribed to
            handler,    // the handler provided by the subscriber - rpc.exposedFn calls this function
            rpc: {      // details of the exposed RPC function created to receive updates
              fnName,   // the RPC function name
              exposedFn // the exposed RPC function created by postmsg-rpc
            }
          }
        */
      ];

      var api = {
        publish: _callbackify2.default.variadic((0, _prepost.pre)((0, _buffer.preBufferToJson)(1), (0, _postmsgRpc.caller)('ipfs.pubsub.publish', opts))),
        subscribe: function subscribe(topic, options, handler, cb) {
          var sub = void 0;

          if (typeof options === 'function') {
            cb = handler;
            handler = options;
            options = {};
          }

          var stub = (0, _prepost.pre)(function () {
            for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
              args[_key] = arguments[_key];
            }

            var handlerIndex = args.length === 3 ? 2 : 1;
            var fnName = 'ipfs.pubsub.subscribe.handler.' + (0, _shortid2.default)();

            sub = {
              topic: topic,
              handler: handler,
              rpc: {
                fnName: fnName,
                exposedFn: (0, _postmsgRpc.expose)(fnName, (0, _prepost.pre)(function () {
                  for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
                    args[_key2] = arguments[_key2];
                  }

                  Iif ((0, _buffer.isBufferJson)(args[0].data)) {
                    args[0].data = (0, _buffer.bufferFromJson)(args[0].data);
                  }

                  Iif ((0, _buffer.isBufferJson)(args[0].seqno)) {
                    args[0].seqno = (0, _buffer.bufferFromJson)(args[0].data);
                  }

                  return args;
                }, function () {
                  for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
                    args[_key3] = arguments[_key3];
                  }

                  process.nextTick(function () {
                    return handler.apply(undefined, args);
                  });
                  return Promise.resolve();
                }), opts)
              }
            };

            subs.push(sub);

            args[handlerIndex] = (0, _function.functionToJson)(fnName);

            return args;
          },
          // If error, then remove subscription handler
          function () {
            return (0, _postmsgRpc.caller)('ipfs.pubsub.subscribe', opts).apply(undefined, arguments).catch(function (err) {
              sub.rpc.exposedFn.close();
              subs.splice(subs.indexOf(sub), 1);
              throw err;
            });
          });

          if (cb) {
            stub(topic, options, handler).then(function (res) {
              return process.nextTick(function () {
                return cb(null, res);
              });
            }).catch(function (err) {
              return process.nextTick(function () {
                return cb(err);
              });
            });
          } else {
            return stub(topic, options, handler);
          }
        },
        unsubscribe: (0, _prepost.pre)(function () {
          for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
            args[_key4] = arguments[_key4];
          }

          var topic = args[0];
          var sub = subs.find(function (s) {
            return s.topic === topic && s.handler === args[1];
          });

          Eif (sub) {
            args[1] = (0, _function.functionToJson)(sub.rpc.fnName);
            sub.rpc.exposedFn.close();
            subs.splice(subs.indexOf(sub), 1);
          }

          return args;
        }, (0, _postmsgRpc.caller)('ipfs.pubsub.unsubscribe', opts)),
        peers: _callbackify2.default.variadic((0, _postmsgRpc.caller)('ipfs.pubsub.peers', opts)),
        ls: _callbackify2.default.variadic((0, _postmsgRpc.caller)('ipfs.pubsub.ls', opts)),
        // interface-ipfs-core tests use this function
        // noop since we're not an EventEmitter
        setMaxListeners: function setMaxListeners() {
          return api;
        }
      };

      return api;
    };

    var _postmsgRpc = require('postmsg-rpc');

    var _callbackify = require('callbackify');

    var _callbackify2 = _interopRequireDefault(_callbackify);

    var _shortid = require('shortid');

    var _shortid2 = _interopRequireDefault(_shortid);

    var _prepost = require('prepost');

    var _function = require('../serialization/function');

    var _buffer = require('../serialization/buffer');

    function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }