Skip to content

Commit e936240

Browse files
committed
fix: change subscriptions from set to array
1 parent 855b44b commit e936240

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

packages/cassiopeia/src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type Variables,
2020
} from './types'
2121
import { append } from './utilities/append'
22+
import { filter } from './utilities/filter'
2223

2324
export function createCassiopeia(): Cassiopeia {
2425
const store: Store = {
@@ -28,7 +29,7 @@ export function createCassiopeia(): Cassiopeia {
2829
log: [],
2930
matcher: undefined,
3031
state: TypeState.Locked,
31-
subscriptions: new Set(),
32+
subscriptions: [],
3233
}
3334

3435
const plugins: Plugin[] = []
@@ -63,9 +64,13 @@ export function createCassiopeia(): Cassiopeia {
6364
store.state = TypeState.None
6465

6566
const subscribe = (subscription: Subscription): Unsubscribe => {
66-
store.subscriptions.add(subscription)
67+
if (!store.subscriptions.includes(subscription)) {
68+
store.subscriptions.push(subscription)
69+
}
6770

68-
return () => store.subscriptions.delete(subscription)
71+
return () => {
72+
filter(store.subscriptions, (value) => value !== subscription)
73+
}
6974
}
7075

7176
const cassiopeia: Cassiopeia = {

packages/cassiopeia/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export interface Store {
7070
iterators: Iterators
7171
log: Action[]
7272
state: TypeState
73-
subscriptions: Set<Subscription>
73+
subscriptions: Subscription[]
7474
matcher?: Matcher
7575
}
7676

packages/cassiopeia/src/utilities/filter.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* Filters an array in-place by removing elements that don't match the predicate.
3+
* Iterates backwards through the array to avoid index shifting issues when removing elements.
4+
* @param array - The array to filter (mutated in place)
5+
* @param predicate - Function that returns true for elements to keep
6+
*/
17
export const filter = <T>(array: T[], predicate: (value: T) => boolean) => {
28
for (let l = array.length - 1; l >= 0; l -= 1) {
39
if (!predicate(array[l])) array.splice(l, 1)

0 commit comments

Comments
 (0)