Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub priority #2894

Closed
Closed
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
21 changes: 15 additions & 6 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ const isPromiseLike = (
*/
type Mounted = {
/** Set of listeners to notify when the atom value changes. */
readonly l: Set<() => void>
readonly l: Set<
readonly [listener: () => void, priority?: BatchPriority | undefined]
>
/** Set of mounted atoms that the atom depends on. */
readonly d: Set<AnyAtom>
/** Set of mounted atoms that depends on the atom. */
Expand Down Expand Up @@ -198,8 +200,10 @@ const registerBatchAtom = (
) => {
if (!batch.D.has(atom)) {
batch.D.set(atom, new Set())
addBatchFunc(batch, 'M', () => {
atomState.m?.l.forEach((listener) => addBatchFunc(batch, 'M', listener))
addBatchFunc(batch, 'H', () => {
for (const [listener, priority = 'M'] of atomState.m?.l || []) {
addBatchFunc(batch, priority, listener)
}
})
}
}
Expand Down Expand Up @@ -710,15 +714,20 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
return atomState.m
}

const subscribeAtom = (atom: AnyAtom, listener: () => void) => {
const subscribeAtom = (
atom: AnyAtom,
listener: () => void,
priority?: BatchPriority,
) => {
const batch = createBatch()
const atomState = ensureAtomState(atom)
const mounted = mountAtom(batch, atom, atomState)
const listeners = mounted.l
listeners.add(listener)
const priorityListener = [listener, priority] as const
listeners.add(priorityListener)
flushBatch(batch)
return () => {
listeners.delete(listener)
listeners.delete(priorityListener)
const batch = createBatch()
unmountAtom(batch, atom, atomState)
flushBatch(batch)
Expand Down
Loading