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
14 changes: 8 additions & 6 deletions packages/use-store/src/experimental/Emitter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
export default class Emitter<T extends Array<unknown>> {
_listeners: Array<(...value: T) => void> = [];
_listeners: Set<(...value: T) => void> = new Set();

subscribe(cb: (...value: T) => void): () => void {
const wrapped = (...value: T) => cb(...value);
this._listeners.push(wrapped);
this._listeners.add(cb);

return () => {
this._listeners = this._listeners.filter((s) => s !== wrapped);
this._listeners.delete(cb);
};
}

notify(...value: T) {
this._listeners.forEach((cb) => {
for (const cb of this._listeners) {
cb(...value);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,6 @@ describe("createStore for Redux", () => {
</DocumentFragment>
`);
unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});
});
32 changes: 16 additions & 16 deletions packages/use-store/src/experimental/useStore.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe("Experimental Userland Store", () => {
</DocumentFragment>
`);
unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});

it("Does not tear when new component mounts in its own transition mid transition", async () => {
Expand Down Expand Up @@ -252,7 +252,7 @@ describe("Experimental Userland Store", () => {
</DocumentFragment>
`);
unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});

it("Does not miss updates triggered in useEffect or useLayoutEffect", async () => {
Expand Down Expand Up @@ -386,7 +386,7 @@ describe("Experimental Userland Store", () => {
</DocumentFragment>
`);
unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});

// This should catch the case where fixups accidentally could get entangled with a transition when they should flush sync.
Expand Down Expand Up @@ -552,7 +552,7 @@ describe("Experimental Userland Store", () => {
</DocumentFragment>
`);
unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});

it("Sync update interrupting transition correctly tracks committed state", async () => {
Expand Down Expand Up @@ -684,7 +684,7 @@ describe("Experimental Userland Store", () => {
</DocumentFragment>
`);
unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});

it("Multiple sync updates interrupting transition correctly tracks committed state", async () => {
Expand Down Expand Up @@ -818,7 +818,7 @@ describe("Experimental Userland Store", () => {
</DocumentFragment>
`);
unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});

it("flushSync update interrupting transition correctly tracks committed state", async () => {
Expand Down Expand Up @@ -946,7 +946,7 @@ describe("Experimental Userland Store", () => {
</DocumentFragment>
`);
unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});

it("correctly handles consecutive sync updates", async () => {
Expand Down Expand Up @@ -995,7 +995,7 @@ describe("Experimental Userland Store", () => {
</DocumentFragment>
`);
unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});

it("dynamic stores are not yet supported", async () => {
Expand Down Expand Up @@ -1048,8 +1048,8 @@ describe("Experimental Userland Store", () => {
"useStoreSelector does not currently support dynamic stores",
);
unmount();
expect(store1._listeners.length).toBe(0);
expect(store2._listeners.length).toBe(0);
expect(store1._listeners.size).toBe(0);
expect(store2._listeners.size).toBe(0);
});

it("transition store update causes new store reader to mount", async () => {
Expand Down Expand Up @@ -1104,7 +1104,7 @@ describe("Experimental Userland Store", () => {
</DocumentFragment>
`);
unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});

// Describes a limitation of our fixup logic: If a component mounts sync
Expand Down Expand Up @@ -1244,7 +1244,7 @@ describe("Experimental Userland Store", () => {
</DocumentFragment>
`);
unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});

it("can read from multiple different stores updating independently", async () => {
Expand Down Expand Up @@ -1311,8 +1311,8 @@ describe("Experimental Userland Store", () => {

unmount();

expect(storeA._listeners.length).toBe(0);
expect(storeB._listeners.length).toBe(0);
expect(storeA._listeners.size).toBe(0);
expect(storeB._listeners.size).toBe(0);
});
});

Expand Down Expand Up @@ -1366,7 +1366,7 @@ describe("Selectors can be dynamic", () => {
`);

unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});

it("selector changes sync during a transition update to the store", async () => {
Expand Down Expand Up @@ -1453,6 +1453,6 @@ describe("Selectors can be dynamic", () => {
`);

unmount();
expect(store._listeners.length).toBe(0);
expect(store._listeners.size).toBe(0);
});
});
Loading