-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
storeUtils.ts
164 lines (150 loc) · 3.9 KB
/
storeUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { onDestroy } from 'svelte'
import { derived, writable, type Readable, type Writable } from 'svelte/store'
type Stores = Readable<any> | [Readable<any>, ...Array<Readable<any>>] | Array<Readable<any>>
type StoresValues<T> =
T extends Readable<infer U>
? U
: {
[K in keyof T]: T[K] extends Readable<infer U> ? U : never
}
type MaybePromise<T> = T | Promise<T>
type CleanupFn = () => MaybePromise<void>
type CallbackFn<T> = (values: T) => MaybePromise<void | CleanupFn>
/**
* ### `watch`
*
* Watch a single store or multiple stores and call a callback when they change to trigger side effects.
* The callback can return a cleanup function that will be called when the stores change again or when the component is destroyed.
*
* ```ts
* const store = writable(0)
*
* watch(store, (value) => {
* console.log(value) // 0
* })
* ```
*
* You can also watch multiple stores:
*
* ```ts
* const store1 = writable(0)
* const store2 = writable(1)
*
* watch([store1, store2], ([value1, value2]) => {
* console.log(value1, value2) // 0 1
* })
* ```
*
* The callback can return a cleanup function that will be called when the stores change again or when the component is destroyed.
*
* ```ts
* const store = writable(0)
*
* watch(store, (value) => {
* console.log(value) // 0
* return () => {
* console.log('cleanup')
* }
* })
* ```
*
* @param stores
* @param callback
*/
export const watch = <S extends Stores>(stores: S, callback: CallbackFn<StoresValues<S>>): void => {
const d = derived(stores, (values) => {
return values
})
let cleanupFn: CleanupFn
const unsubscribe = d.subscribe(async (values) => {
if (cleanupFn) cleanupFn()
const fn = await callback(values)
if (fn) cleanupFn = fn
})
onDestroy(() => {
unsubscribe()
if (cleanupFn) cleanupFn()
})
}
/**
* ### `memoize`
*
* Use a single store or multiple stores and return the value(s) as an object.
* This is useful for using stores in a non-reactive way e.g. in loops.
*
* ```ts
* const store = writable(0)
* const memoized = memoize(store) // { current: 0 }
*
* useTask(() => {
* store.update(n => n + 1)
* console.log(memoized.current) // 1, 2, 3, ...
* })
* ```
*
* You can also pass a transform function to transform the values:
*
* ```ts
* const store = writable(0)
* const doubled = memoize(store, n => n * 2) // { current: 0 }
*
* useTask(() => {
* store.update(n => n + 1)
* console.log(doubled.current) // 2, 4, 6, ...
* })
* ```
*
* @param stores
* @param transform
*/
export function memoize<U, S extends Stores>(stores: S): { current: StoresValues<S> }
export function memoize<U, S extends Stores>(
stores: S,
transform: (values: StoresValues<S>) => U
): { current: U }
export function memoize<U, S extends Stores>(
stores: S,
transform?: (values: StoresValues<S>) => U
): { current: U | StoresValues<S> } {
const obj = {
current: undefined
} as any
watch(stores, (v) => {
obj.current = transform ? transform(v) : v
})
return obj
}
export type CurrentWritable<T = unknown> = Writable<T> & { current: T }
/**
* ### `currentWritable`
*
* A writable store that also has a `current` property that is updated synchronously.
* For use in non-reactive contexts e.g. loops where unwrapping a store every frame is expensive.
*
* ```ts
* const store = currentWritable(0)
*
* useTask(() => {
* console.log(store.current) // 0
* })
*
* @param value
* @returns
*/
export const currentWritable = <T>(value: T): CurrentWritable<T> => {
const store = writable<T>(value)
const extendedWritable = {
set: (value: T) => {
extendedWritable.current = value
store.set(value)
},
subscribe: store.subscribe,
update: (fn: (value: T) => T) => {
const newValue = fn(extendedWritable.current)
extendedWritable.current = newValue
store.set(newValue)
},
current: value
}
return extendedWritable
}