Skip to content
Merged
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
8 changes: 8 additions & 0 deletions packages/svelte-query/src/containers.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ type Subscriber = (update: VoidFn) => void | VoidFn
*/
export type Box<T> = { current: T }

/**
* A {@link Box} whose `current` value is computed on each read, and that notifies the reactive
* contexts reading it through the given subscriber.
*/
export class ReactiveValue<T> implements Box<T> {
#fn
#subscribe
Expand All @@ -17,6 +21,10 @@ export class ReactiveValue<T> implements Box<T> {
this.#subscribe = createSubscriber((update) => onSubscribe(update))
}

/**
* Subscribes the reactive context reading it, then computes the value.
* @returns The value returned by the compute function.
*/
get current() {
this.#subscribe()
return this.#fn()
Expand Down
8 changes: 8 additions & 0 deletions packages/svelte-query/src/utils.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ function runEffect(
}
}
type Getter<T> = () => T
/**
* Runs `effect` whenever the reactive state read by `sources` changes, even if they return the
* same values, skipping the first run. The effect receives the new and previous values, and is
* called untracked, so only `sources` are tracked.
* @param sources - The getter, or array of getters, to watch.
* @param flush - Whether to run after (`'post'`) or before (`'pre'`) the DOM updates.
* @param effect - Called with the new and previous values. It may return a cleanup function.
*/
export const watchChanges = <T>(
sources: Getter<T> | Array<Getter<T>>,
flush: 'post' | 'pre',
Expand Down
Loading