diff --git a/src/vanilla/store.ts b/src/vanilla/store.ts index 4c0f92cb337..3d36a75d423 100644 --- a/src/vanilla/store.ts +++ b/src/vanilla/store.ts @@ -93,7 +93,7 @@ export type AtomState = { */ readonly d: Map /** Set of priority listeners to run when the atom value changes. */ - readonly l: Set + readonly l: Set /** * Set of atoms with pending promise that depend on the atom. * @@ -203,7 +203,7 @@ const registerBatchAtom = ( if (!batch.D.has(atom)) { batch.D.set(atom, new Set()) const scheduleListeners = () => { - for (const [listener, priority = 'M'] of atomState.l || []) { + for (const [listener, priority] of atomState.l) { addBatchFunc(batch, listener, priority) } } @@ -692,7 +692,7 @@ const buildStore = (...storeArgs: StoreArgs): Store => { // unmount self const onUnmount = atomState.m.u if (onUnmount) { - addBatchFunc(batch, () => onUnmount(batch), 'L') + addBatchFunc(batch, onUnmount, 'L') } delete atomState.m // unmount dependencies @@ -709,14 +709,14 @@ const buildStore = (...storeArgs: StoreArgs): Store => { const batch = createBatch() const atomState = getAtomState(atom) const mounted = mountAtom(batch, atom, atomState) - const priorityListener = [() => listener()] as const - atomState.l.add(priorityListener) + const priorityListener = [() => listener(), 'M'] as const ++mounted.l + atomState.l.add(priorityListener) flushBatch(batch) return () => { const batch = createBatch() - atomState.l.delete(priorityListener) --mounted.l + atomState.l.delete(priorityListener) unmountAtom(batch, atom, atomState) flushBatch(batch) } diff --git a/tests/vanilla/effect.test.ts b/tests/vanilla/effect.test.ts index a132a77d208..8acfc8e8a29 100644 --- a/tests/vanilla/effect.test.ts +++ b/tests/vanilla/effect.test.ts @@ -108,9 +108,9 @@ function atomSyncEffect(effect: Effect) { : null } const priority: BatchPriority = 'H' - const entry = [batchListener, priority] as const - atomState.l.add(entry) - return () => atomState.l.delete(entry) + const priorityListener = [batchListener, priority] as const + atomState.l.add(priorityListener) + return () => atomState.l.delete(priorityListener) } } const effectAtom = Object.assign( diff --git a/tests/vanilla/store.test.tsx b/tests/vanilla/store.test.tsx index e8ddb6e1f08..428bd2549cf 100644 --- a/tests/vanilla/store.test.tsx +++ b/tests/vanilla/store.test.tsx @@ -1108,11 +1108,9 @@ it('should call the batch listener with batch and respect the priority', () => { const a = atom(0) const highPriorityBatchListener = vi.fn() as BatchListener const mediumPriorityBatchListener = vi.fn() as BatchListener - const defaultPriorityBatchListener = vi.fn() as BatchListener // medium const lowPriorityBatchListener = vi.fn() as BatchListener a.INTERNAL_onInit = (_store, atomState) => { atomState.l.add([lowPriorityBatchListener, 'L']) - atomState.l.add([defaultPriorityBatchListener]) atomState.l.add([highPriorityBatchListener, 'H']) atomState.l.add([mediumPriorityBatchListener, 'M']) } @@ -1122,16 +1120,12 @@ it('should call the batch listener with batch and respect the priority', () => { expect(highPriorityBatchListener).toHaveBeenCalledWith(mockBatch) expect(mediumPriorityBatchListener).toHaveBeenCalledWith(mockBatch) expect(lowPriorityBatchListener).toHaveBeenCalledWith(mockBatch) - // @ts-expect-error toHaveBeenCalledBefore is a custom matcher - expect(highPriorityBatchListener).toHaveBeenCalledBefore( + // eslint-disable-next-line @vitest/valid-expect + ;(expect(highPriorityBatchListener) as any).toHaveBeenCalledBefore( mediumPriorityBatchListener, ) - // @ts-expect-error toHaveBeenCalledBefore is a custom matcher - expect(mediumPriorityBatchListener).toHaveBeenCalledBefore( - lowPriorityBatchListener, - ) - // @ts-expect-error toHaveBeenCalledBefore is a custom matcher - expect(defaultPriorityBatchListener).toHaveBeenCalledBefore( + // eslint-disable-next-line @vitest/valid-expect + ;(expect(mediumPriorityBatchListener) as any).toHaveBeenCalledBefore( lowPriorityBatchListener, ) })