Skip to content

Commit

Permalink
drop optional priority
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Dec 23, 2024
1 parent a5a0772 commit 0d2e258
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export type AtomState<Value = AnyValue> = {
*/
readonly d: Map<AnyAtom, number>
/** Set of priority listeners to run when the atom value changes. */
readonly l: Set<readonly [listener: BatchListener, priority?: BatchPriority]>
readonly l: Set<readonly [listener: BatchListener, priority: BatchPriority]>
/**
* Set of atoms with pending promise that depend on the atom.
*
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
10 changes: 6 additions & 4 deletions tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import '@testing-library/jest-dom/vitest'
import { expect } from 'vitest'
import { expect, vi } from 'vitest'

type MockFunction = ReturnType<typeof vi.fn>

expect.extend({
toHaveBeenCalledBefore(received, expected) {
toHaveBeenCalledBefore(received: MockFunction, expected: MockFunction) {
const pass =
received.mock.invocationCallOrder[0] <
expected.mock.invocationCallOrder[0]
received.mock.invocationCallOrder[0]! <
expected.mock.invocationCallOrder[0]!
return {
pass,
message: () =>
Expand Down
6 changes: 3 additions & 3 deletions tests/vanilla/effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 4 additions & 10 deletions tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
}
Expand All @@ -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,
)
})

0 comments on commit 0d2e258

Please sign in to comment.