diff --git a/index.js b/index.js index e9c3022..4313d42 100644 --- a/index.js +++ b/index.js @@ -262,7 +262,7 @@ class Channel { } module.exports = class Protomux { - constructor(stream, { alloc } = {}) { + constructor(stream, { alloc, slave = false } = {}) { if (stream.userData === null) stream.userData = this; this.isProtomux = true; @@ -287,6 +287,7 @@ module.exports = class Protomux { this._infos = new Map(); this._notify = new Map(); + this._slave = false; this.stream.on("data", this._ondata.bind(this)); this.stream.on("end", this._onend.bind(this)); @@ -574,7 +575,7 @@ module.exports = class Protomux { } } - _onopensession(state) { + async _onopensession(state) { const remoteId = c.uint.decode(state); const protocol = c.string.decode(state); const id = c.buffer.decode(state); @@ -589,6 +590,10 @@ module.exports = class Protomux { const rid = remoteId - 1; const info = this._get(protocol, id); + if (this._slave) { + await this._mux.syncState?.(); + } + // allow the remote to grow the ids by one if (this._remote.length === rid) { this._remote.push(null);