Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export class List<T> {

push(value: T) {
this.ctx.effect(() => {
this.inner.set(++this.sn, value)
return () => this.inner.delete(this.sn)
const sn = ++this.sn
this.inner.set(sn, value)
return () => this.inner.delete(sn)
}, `${this.trace}.push()`)
}

Expand Down
41 changes: 41 additions & 0 deletions packages/utils/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, it } from 'vitest'
import { Context } from 'cordis'
import assert from 'node:assert'
import { List } from '../src/index.js'

describe('List', () => {
it('supports push and iteration', () => {
const ctx = new Context()
const list = new List(ctx, 'test')
list.push('a')
list.push('b')
assert.strictEqual(list.length, 2)
assert.deepStrictEqual([...list], ['a', 'b'])
})

it('does not leak entries across fiber reloads', async () => {
const ctx = new Context()
let list: List<string> | undefined
const fiber = ctx.plugin((sub) => {
list ??= new List(sub, 'test')
list.push('a')
list.push('b')
})
await fiber
assert.deepStrictEqual([...list!], ['a', 'b'])
assert.strictEqual(list!.length, 2)

// Reloading the fiber unloads every pushed entry (its disposer runs) and
// then re-runs the plugin callback. Each entry's disposer must remove
// exactly the entry it created — otherwise stale entries accumulate.
fiber.update({})
await fiber
assert.deepStrictEqual([...list!], ['a', 'b'])
assert.strictEqual(list!.length, 2)

fiber.update({})
await fiber
assert.deepStrictEqual([...list!], ['a', 'b'])
assert.strictEqual(list!.length, 2)
})
})
3 changes: 3 additions & 0 deletions packages/utils/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../../tsconfig.test",
}