primus / substream

Volatile namespaces on top of Primus streams
MIT License
33 stars 8 forks source link

Compatibility with primus-callbacks #20

Open mauron85 opened 8 years ago

mauron85 commented 8 years ago

Hi there,

wondering if it's possible to make primus-callbacks work on a substream? It currently works on main stream and I believe it's only about writing implementation for primus-callbacks writeAndWait method.

  /**
   * Write a new message to the streams and wait for response
   *
   * @param {Mixed} msg The data that needs to be written.
   * @param {Func} done nodejs style callback (err, response)
   * @returns {Boolean}
   * @api public
   */
SubStream.prototype.writeAndWait = function write(msg, done) {
  // TODO: implementation here
};

I'll try to implement it myself, but I'm bit lost in how transform works. What does this code actually do?

  /**
   * Write a new message to the streams.
   *
   * @param {Mixed} msg The data that needs to be written.
   * @returns {Boolean}
   * @api public
   */
  SubStream.prototype.write = function write(msg) {
    if (!this.stream) return false;

    this.stream.transforms(this.primus, this, 'outgoing', msg);
    return true;
  };

and when this get invoked?


  /**
   * Actually write the message.
   *
   * @param {Mixed} msg The data that needs to be written.
   * @returns {Boolean}
   * @api private
   */
  SubStream.prototype._write = function write(msg) {
    return this.stream._write({
      substream: this.name,
      args: msg
    });
  };

Thanks

lpinca commented 8 years ago

What does this code actually do?

It calls the transforms method on the Spark or the client.

That transforms method runs the registered message transformers and then call SubStream.prototype._write (see the second argument) with the transformed data.