-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path04-stores.ts
More file actions
320 lines (279 loc) · 9.26 KB
/
04-stores.ts
File metadata and controls
320 lines (279 loc) · 9.26 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// An implementation and comparison of stores from multiple frameworks, each with:
// `derived`, which creates a store whose value is computed based on other stores;
// `effect`, which runs an effect when the values of one or more stores change;
// `get`, which retrieves the value of a store;
// `readable`, which creates a store that is read-only;
// `untrack`, which performs a side effect on multiple stores without subscribing to them; and
// `writable`, which creates a store that is readable and writable.
/**
* If you look through this repository, it becomes pretty obvious which kind of
* store I like the most, and it's clearly Solid. Solid stores have very concise
* syntax, work without any compilation, and are still faster than Svelte
* stores, despite the fact that Svelte literally uses a compiler and Solid is
* plain JavaScript.
*
* The Solid namespace is missing certain methods, namely:
*
* - `get`, as Solid stores already expose a getter function.
*
* **Minification results:** 345 characters minified (271 without exports)
*
* The good parts:
*
* - Solid automatically tracks accessed stores in `derived` and `effect`.
* - Using arrays instead of objects allows for users of Solid style stores to
* choose any names they like, instead of being tied to methods predefined by
* their framework, such as Vue's `.value` or Svelte's `.set` and
* `.subscribe`.
*
* The bad parts:
*
* - None!
*
* **My opinion:**
*
* Solid has the best kind of stores. They're framework agnostic and minify
* really well. They have direct getter functions, automatically recognize
* called getters in `derived` and `effect`, and work with fine-grained
* reactivity.
*/
export namespace Solid {
let currentEffect: (() => void) | undefined
export function writable<T>(
value: T,
): [get: () => T, set: (value: T) => void] {
const tracking = new Set<() => void>()
return [
() => {
if (currentEffect) {
tracking.add(currentEffect)
}
return value
},
(newValue) => {
value = newValue
tracking.forEach((fn) => fn())
},
]
}
export function readable<T>(
value: T,
updater: (set: (value: T) => void) => void,
): () => T {
const [get, set] = writable(value)
updater(set)
return get
}
export function derived<T>(get: () => T): () => T {
const [_get, set] = writable<T>(null!)
effect(() => set(get()))
return _get
}
export function effect(effect: () => void) {
const parentEffect = currentEffect
currentEffect = effect
effect()
currentEffect = parentEffect
}
export function untrack<T>(get: () => T): T {
const parentEffect = currentEffect
currentEffect = undefined
const value = get()
currentEffect = parentEffect
return value
}
}
/**
* Svelte stores are OK, but they don't have a direct getter function, which I
* really dislike. Instead, one has to resort to a slightly hacky approach that
* might not even work with custom stores that call `onUpdate` asynchronously.
*
* The Svelte namespace is missing certain methods, namely:
*
* - `untrack`, as you can just call the return value of `.subscribe`
*
* **Minification results:** 570 characters minified (500 without exports)
*
* The good parts:
*
* - None!
*
* The bad parts:
*
* - Using object methods instead of arrays ties users to specific names. Not only
* does this reduce choice, but it prevents minification unless you're working
* with an entire codebase at once.
* - Svelte stores don't have a built in `get` function. Instead, `get` relies on
* weird semantics that most stores seem to have. However, it could fail for
* stores defined in user land; specifically, ones that call `onUpdate`
* asynchronously.
*
* **My opinion:**
*
* Svelte stores are bad. There's no reason to use objects over tuples, and a
* performance focused framework such as Svelte should know that. Honestly, this
* in unacceptable.
*/
export namespace Svelte {
export type Readable<T> = {
subscribe(onUpdate: (value: T) => void): () => void
}
export type Writable<T> = {
set(value: T): void
} & Readable<T>
export type Infer<T extends Readable<any>> =
T extends Readable<infer U> ? U : never
export function writable<T>(value: T): Writable<T> {
const subscribers = new Set<(value: T) => void>()
const set = (newValue: T) => {
value = newValue
subscribers.forEach((fn) => fn(value))
}
return {
set,
subscribe(onUpdate) {
subscribers.add(onUpdate)
onUpdate(value)
return () => subscribers.delete(onUpdate)
},
}
}
export function readable<T>(
value: T,
updater: (set: (value: T) => void) => void,
): Readable<T> {
const { set, subscribe } = writable(value)
updater(set)
return { subscribe }
}
export function get<T>(store: Readable<T>): T {
let value!: T
let didNotGetValue = true
store.subscribe((storeValue: T) => {
value = storeValue
didNotGetValue = false
})()
if (didNotGetValue) {
throw new Error(
"The passed store did not call 'onUpdate' synchronously.",
)
}
return value
}
export function effect<T extends readonly Readable<any>[]>(
stores: T,
effect: (...values: { [K in keyof T]: Infer<T[K]> }) => void,
) {
const values: { -readonly [K in keyof T]: Infer<T[K]> } = [] as any
let isInitialPass = true
stores.forEach((store, index) => {
let wasUpdated = false
store.subscribe((value) => {
wasUpdated = true
values[index] = value
if (!isInitialPass) {
effect(...values)
}
})
if (!wasUpdated) {
throw new Error(
"The passed store did not call 'onUpdate' synchronously.",
)
}
})
isInitialPass = false
effect(...values)
}
export function derived<T extends readonly Readable<any>[], U>(
stores: T,
getValue: (...values: { [K in keyof T]: Infer<T[K]> }) => U,
): Readable<U> {
const { set, subscribe } = writable<U>(null!)
effect(stores, (...values) => set(getValue(...values)))
return { subscribe }
}
}
/**
* I don't know how Vue ~~stores~~ refs work; I only know that they use getters
* and setters on the `.value` property. For Vue, I copied the Solid code and
* changed it to use `.value` instead of an accessor/updater pair.
*
* **Minification results:** 487 characters minified (404 without exports)
*
* The good parts:
*
* - Vue automatically tracks accessed stores in `derived` and `effect`.
*
* The bad parts:
*
* - It's like Solid, but it uses `.value`. This means that you need to create
* additional getters and setters to make a readonly store rather than just
* returning the getter function. Using a property also breaks minification.
*
* **My opinion:**
*
* Vue stores are mid. I don't understand why Vue chose a property over Solid's
* tuple syntax (which I absolutely love), as it causes a lot of problems with
* minification. But at least it's 100 characters smaller than Svelte.
*/
export namespace Vue {
export type Writable<T> = { value: T }
export type Readable<T> = { readonly value: T }
let currentEffect: (() => void) | undefined
export function writable<T>(value: T): Writable<T> {
const tracking = new Set<() => void>()
return {
get value() {
if (currentEffect) {
tracking.add(currentEffect)
}
return value
},
set value(newValue) {
value = newValue
tracking.forEach((fn) => fn())
},
}
}
export function readable<T>(
value: T,
updater: (set: (value: T) => void) => void,
): Readable<T> {
const store = writable(value)
updater((newValue) => {
store.value = newValue
})
return {
get value() {
return store.value
},
}
}
export function derived<T>(get: () => T): Readable<T> {
const store = writable<T>(null!)
effect(() => {
store.value = get()
})
return {
get value() {
return store.value
},
}
}
export function effect(effect: () => void) {
const parentEffect = currentEffect
currentEffect = effect
effect()
currentEffect = parentEffect
}
export function untrack<T>(get: () => T): T {
const parentEffect = currentEffect
currentEffect = undefined
const value = get()
currentEffect = parentEffect
return value
}
export function get<T>(store: Readable<T>): T {
return untrack(() => store.value)
}
}