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

fix(utils): add watch options for notify-in-sync #285

Merged
merged 1 commit into from
Dec 1, 2021
Merged
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
10 changes: 8 additions & 2 deletions src/utils/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { subscribe } from '../vanilla'

type WatchGet = <T extends object>(value: T) => T
type WatchCallback = (get: WatchGet) => (() => void) | void | undefined
type WatchOptions = {
sync?: boolean
}

let currentCleanups: Set<() => void> | undefined

Expand All @@ -28,7 +31,10 @@ let currentCleanups: Set<() => void> | undefined
* @returns A cleanup function that stops the callback from reevaluating and
* also performs cleanups registered into `watch`.
*/
export const watch = (callback: WatchCallback): (() => void) => {
export const watch = (
callback: WatchCallback,
options?: WatchOptions
): (() => void) => {
const cleanups = new Set<() => void>()
const subscriptions = new Set<object>()

Expand Down Expand Up @@ -72,7 +78,7 @@ export const watch = (callback: WatchCallback): (() => void) => {

// Subscribe to all collected proxies
subscriptions.forEach((proxy) => {
const clean = subscribe(proxy, revalidate)
const clean = subscribe(proxy, revalidate, options?.sync)
cleanups.add(clean)
})
}
Expand Down