diff --git a/index.js b/index.js index 80bd67d..6185d6c 100644 --- a/index.js +++ b/index.js @@ -83,7 +83,7 @@ export class AutobaseManager { const streamRecord = { stream, inputAnnouncer, outputAnnouncer } this._streams.push(streamRecord) stream.once('close', () => { - this._streams.slice(this._streams.indexOf(streamRecord), 1) + this._streams.splice(this._streams.indexOf(streamRecord), 1) }) if (this.base.localInput || this.base.inputs || this.base.outputs || this.base.localOutput) this.announce(streamRecord) diff --git a/tests/basics.js b/tests/basics.js index 5028a07..c516b64 100644 --- a/tests/basics.js +++ b/tests/basics.js @@ -196,4 +196,39 @@ test('full replicate', (t) => { [baseA.localOutput, baseB.localOutput].map((core) => core.key), 'baseA got baseB\'s outputs & not denied cores') }) + + t.test('removes stream on close', async (t) => { + t.plan(4) + const [storeA, baseA] = await create() + const [storeB, baseB] = await create() + + const streamA = storeA.replicate(true) + const streamB = storeB.replicate(false) + + const managerA = new AutobaseManager(baseA, () => true, + storeA.get.bind(storeA), storeA.storage) + managerA.attachStream(streamA.noiseStream) + const managerB = new AutobaseManager(baseB, () => true, + storeB.get.bind(storeB), storeB.storage) + managerB.attachStream(streamB.noiseStream) + + pipeline([ + streamA, + streamB, + streamA + ]) + + await new Promise((resolve) => { setTimeout(resolve, 100) }) + + t.equal(managerA._streams[0].stream, streamA.noiseStream, 'adds stream') + t.equal(managerB._streams[0].stream, streamB.noiseStream, 'adds stream') + + streamA.destroy() + streamB.destroy() + + await new Promise((resolve) => { setTimeout(resolve, 100) }) + + t.deepEqual(managerA._streams, [], 'removes all streams') + t.deepEqual(managerB._streams, [], 'removes all streams') + }) })