*Add concept of being a slave

*on _onopensession, if we are a slave, sync before checking _remote
This commit is contained in:
Derrick Hammer 2023-04-06 14:21:01 -04:00
parent 81eac54453
commit f457294772
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 7 additions and 2 deletions

View File

@ -262,7 +262,7 @@ class Channel {
} }
module.exports = class Protomux { module.exports = class Protomux {
constructor(stream, { alloc } = {}) { constructor(stream, { alloc, slave = false } = {}) {
if (stream.userData === null) stream.userData = this; if (stream.userData === null) stream.userData = this;
this.isProtomux = true; this.isProtomux = true;
@ -287,6 +287,7 @@ module.exports = class Protomux {
this._infos = new Map(); this._infos = new Map();
this._notify = new Map(); this._notify = new Map();
this._slave = false;
this.stream.on("data", this._ondata.bind(this)); this.stream.on("data", this._ondata.bind(this));
this.stream.on("end", this._onend.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 remoteId = c.uint.decode(state);
const protocol = c.string.decode(state); const protocol = c.string.decode(state);
const id = c.buffer.decode(state); const id = c.buffer.decode(state);
@ -589,6 +590,10 @@ module.exports = class Protomux {
const rid = remoteId - 1; const rid = remoteId - 1;
const info = this._get(protocol, id); const info = this._get(protocol, id);
if (this._slave) {
await this._mux.syncState?.();
}
// allow the remote to grow the ids by one // allow the remote to grow the ids by one
if (this._remote.length === rid) { if (this._remote.length === rid) {
this._remote.push(null); this._remote.push(null);