Skip to content

Commit

Permalink
fix: Splice not slice streams when closing
Browse files Browse the repository at this point in the history
Thanks @railgun on discord for finding this.
  • Loading branch information
lejeunerenard committed Feb 25, 2023
1 parent 95eb6bb commit f3d7912
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions tests/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

0 comments on commit f3d7912

Please sign in to comment.