Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

containers.svelte.ts

123 lines3.9 KB

1import { SvelteSet, createSubscriber } from 'svelte/reactivity'
2​
3type VoidFn = () => void
4type Subscriber = (update: VoidFn) => void | VoidFn
5​
6export type Box<T> = { current: T }
7​
8export class ReactiveValue<T> implements Box<T> {
9 #fn
10 #subscribe
11​
12 constructor(fn: () => T, onSubscribe: Subscriber) {
13 this.#fn = fn
14 this.#subscribe = createSubscriber((update) => onSubscribe(update))
15 }
16​
17 get current() {
18 this.#subscribe()
19 return this.#fn()
20 }
22​
23/**
24 * Makes all of the top-level keys of an object into $state.raw fields whose initial values
25 * are the same as in the original object. Does not mutate the original object. Provides an `update`
26 * function that _can_ (but does not have to be) be used to replace all of the object's top-level keys
27 * with the values of the new object, while maintaining the original root object's reference.
28 */
29export function createRawRef<T extends {} | Array<unknown>>(
30 init: T,
31): [T, (newValue: T) => void] {
32 const refObj = (Array.isArray(init) ? [] : {}) as T
33 const hiddenKeys = new SvelteSet<PropertyKey>()
34 const out = new Proxy(refObj, {
35 set(target, prop, value, receiver) {
36 hiddenKeys.delete(prop)
37 if (prop in target) {
38 return Reflect.set(target, prop, value, receiver)
39 }
40 let state = $state.raw(value)
41 Object.defineProperty(target, prop, {
42 configurable: true,
43 enumerable: true,
44 get: () => {
45 // If this is a lazy value, we need to call it.
46 // We can't do something like typeof state === 'function'
47 // because the value could actually be a function that we don't want to call.
48 return state && isBranded(state) ? state() : state
49 },
50 set: (v) => {
51 state = v
52 },
53 })
54 return true
55 },
56 has: (target, prop) => {
57 if (hiddenKeys.has(prop)) {
58 return false
59 }
60 return prop in target
61 },
62 ownKeys(target) {
63 return Reflect.ownKeys(target).filter((key) => !hiddenKeys.has(key))
64 },
65 getOwnPropertyDescriptor(target, prop) {
66 if (hiddenKeys.has(prop)) {
67 return undefined
68 }
69 return Reflect.getOwnPropertyDescriptor(target, prop)
70 },
71 deleteProperty(target, prop) {
72 if (prop in target) {
73 // @ts-expect-error
74 // We need to set the value to undefined to signal to the listeners that the value has changed.
75 // If we just deleted it, the reactivity system wouldn't have any idea that the value was gone.
76 target[prop] = undefined
77 hiddenKeys.add(prop)
78 if (Array.isArray(target)) {
79 target.length--
80 }
81 return true
82 }
83 return false
84 },
85 })
86​
87 function update(newValue: T) {
88 const existingKeys = Object.keys(out)
89 const newKeys = Object.keys(newValue)
90 const keysToRemove = existingKeys.filter((key) => !newKeys.includes(key))
91 for (const key of keysToRemove) {
92 // @ts-expect-error
93 delete out[key]
94 }
95 for (const key of newKeys) {
96 // @ts-expect-error
97 // This craziness is required because Tanstack Query defines getters for all of the keys on the object.
98 // These getters track property access, so if we access all of them here, we'll end up tracking everything.
99 // So we wrap the property access in a special function that we can identify later to lazily access the value.
100 // (See above)
101 out[key] = brand(() => newValue[key])
102 }
103 }
104​
105 // we can't pass `init` directly into the proxy because it'll never set the state fields
106 // (because (prop in target) will always be true)
107 update(init)
108​
109 return [out, update]
111​
112const lazyBrand = Symbol('LazyValue')
113type Branded<T extends () => unknown> = T & { [lazyBrand]: true }
114​
115function brand<T extends () => unknown>(fn: T): Branded<T> {
116 // @ts-expect-error
117 fn[lazyBrand] = true
118 return fn as Branded<T>
120​
121function isBranded<T extends () => unknown>(fn: T): fn is Branded<T> {
122 return Boolean((fn as Branded<T>)[lazyBrand])