-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemoized-computeds.ts
320 lines (258 loc) · 8.39 KB
/
memoized-computeds.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
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
import { computed, type ReadonlySignal, Signal } from '@preact/signals-core'
import { MemoizedArrayOfSignals } from './memoized-array-of-signals.ts'
// NOTE: intentionally not implementing methods which produce a copy, that
// defeats the purpose of having a stable memoized array of computeds
export class MemoizedComputeds<T, I, R> {
static get [Symbol.species]() {
return MemoizedComputeds
}
[Symbol.toStringTag] = 'MemoizedComputeds'
#cache: Map<I, ReadonlySignal<R>>
#idFn: (elem: R) => I
// deno-lint-ignore no-explicit-any
#signal: MemoizedArrayOfSignals<T, I> | MemoizedComputeds<any, I, T>
#storage: ReadonlySignal<ReadonlySignal<R>[]>
#transform: (elem: Signal<T>) => R
get idFn() {
return this.#idFn
}
constructor(
// deno-lint-ignore no-explicit-any
signal: MemoizedArrayOfSignals<T, I> | MemoizedComputeds<any, I, T>,
transform: (elem: Signal<T>) => R,
idFn: (elem: R) => I
) {
this.#cache = new Map()
this.#signal = signal
this.#transform = transform
this.#idFn = idFn
this.#storage = computed(() => {
const newStorage = this.#signal.map(sig => {
const id = this.#signal.getID(sig)
const possible = this.#cache.get(id)
if (possible) { return possible }
const comp = computed(() => this.#transform(sig))
this.#cache.set(id, comp)
return comp
})
for (const [id, _comp] of this.#cache) {
if (!this.#signal.getByID(id)) {
this.#cache.delete(id)
}
}
return newStorage
})
}
getID(comp: ReadonlySignal<R> | R): I {
if (comp instanceof Signal) {
return this.#idFn(comp.value)
} else {
return this.#idFn(comp as R)
}
}
getByID(id: I): ReadonlySignal<R> | undefined {
// NOTE: make sure the computed is not too lazy and has never been computed
this.#storage.peek()
return this.#cache.get(id)
}
computedById(id: I): ReadonlySignal<R | undefined> {
return computed(() => {
// NOTE: subscribe for updates from the storage array
this.#storage.value
return this.#cache.get(id)?.value
})
}
get value(): ReadonlySignal<R>[] {
return this.#storage.value
}
peek(): ReadonlySignal<R>[] {
return this.#storage.peek()
}
subscribe(cb: (newValue: ReadonlySignal<R>[]) => void): () => void {
return this.#storage.subscribe(cb)
}
*[Symbol.iterator](): IterableIterator<ReadonlySignal<R>> {
for (const elem of this.#storage.value) {
yield elem
}
}
at(index: number): ReadonlySignal<R> | undefined {
return this.#storage.value.at(index)
}
// SKIP: clear() - mutating method
// SKIP: concat() - copying method
// SKIP: copyWithin() - mutating method
entries(): IterableIterator<ReadonlySignal<R>> {
return this[Symbol.iterator]()
}
every(
cb: (element: ReadonlySignal<R>, index: number, list: MemoizedComputeds<T, I, R>) => boolean,
thisArg?: unknown
): boolean {
return this.#storage.value.every((element, index) => {
return cb(element, index, this)
}, thisArg)
}
// SKIP: fill() - mutating method
filter(
cb: (elem: ReadonlySignal<R>, index: number, list: MemoizedComputeds<T, I, R>) => boolean,
thisArg?: unknown
): ReadonlySignal<R>[] {
return this.#storage.value.filter((element, index) => {
return cb(element, index, this)
}, thisArg)
}
find(
cb: (elem: ReadonlySignal<R>, index: number, list: MemoizedComputeds<T, I, R>) => unknown,
thisArg?: unknown
): ReadonlySignal<R> | undefined {
return this.#storage.value.find((element, index) => {
return cb(element, index, this)
}, thisArg)
}
findIndex(
cb: (elem: ReadonlySignal<R>, index: number, list: MemoizedComputeds<T, I, R>) => unknown,
thisArg?: unknown
): number {
return this.#storage.value.findIndex((element, index) => {
return cb(element, index, this)
}, thisArg)
}
findLast(
cb: (elem: ReadonlySignal<R>, index: number, list: MemoizedComputeds<T, I, R>) => unknown,
thisArg?: unknown
): ReadonlySignal<R> | undefined {
return this.#storage.value.findLast((element, index) => {
return cb(element, index, this)
}, thisArg)
}
findLastIndex(
cb: (elem: ReadonlySignal<R>, index: number, list: MemoizedComputeds<T, I, R>) => unknown,
thisArg?: unknown
): number {
return this.#storage.value.findLastIndex((element, index) => {
return cb(element, index, this)
}, thisArg)
}
// SKIP: flat() - copying method
flatMap<U>(
cb: (
value: ReadonlySignal<R>,
index: number,
array: MemoizedComputeds<T, I, R>
) => U | ReadonlyArray<U>
): U[] {
return this.#storage.value.flatMap((item, index) => {
return cb(item, index, this)
})
}
forEach(
cb: (elem: ReadonlySignal<R>, index: number, list: MemoizedComputeds<T, I, R>) => unknown,
thisArg?: unknown
): void {
return this.#storage.value.forEach((element, index) => {
return cb(element, index, this)
}, thisArg)
}
// SKIP: static from() - use the constructor
// SKIP: static fromAsync()
includes(item: ReadonlySignal<R> | R, fromIndex?: number): boolean {
// NOTE: make sure the computed is not too lazy and has never been computed
this.#storage.peek()
const id = this.getID(item)
const foundItem = this.#cache.get(id)
if (foundItem === undefined) {
return false
}
return this.#storage.value.includes(foundItem, fromIndex)
}
indexOf(item: ReadonlySignal<R> | R): number {
// NOTE: make sure the computed is not too lazy and has never been computed
this.#storage.peek()
const id = this.getID(item)
const foundItem = this.#cache.get(id)
if (foundItem === undefined) {
return -1
}
return this.#storage.value.indexOf(foundItem)
}
join(seperator?: string): string {
return this.#storage.value.join(seperator)
}
keys(): IterableIterator<number> {
return this.#storage.value.keys()
}
lastIndexOf(item: ReadonlySignal<R> | R, fromIndex?: number): number {
// NOTE: make sure the computed is not too lazy and has never been computed
this.#storage.peek()
const id = this.getID(item)
const foundItem = this.#cache.get(id)
if (foundItem === undefined) {
return -1
}
if (fromIndex) {
return this.#storage.value.lastIndexOf(foundItem, fromIndex)
} else {
return this.#storage.value.lastIndexOf(foundItem)
}
}
get length(): number {
return this.#storage.value.length
}
map<M>(
cb: (value: ReadonlySignal<R>, index: number, list: MemoizedComputeds<T, I, R>) => M,
thisArg?: unknown
): M[] {
return this.#storage.value.map((element, index) => {
return cb(element, index, this)
}, thisArg)
}
// SKIP: static of() – use the constructor
// SKIP: pop() – mutating
// SKIP: push() – mutating
// NOTE: we intentionally only support this typing for reduce() and
// reduceRight() which has an initial value. It doesn't make sense to
// reduce into a ReadonlySignal<R> as the accumulator type.
reduce<U>(
cb: (acc: U, current: ReadonlySignal<R>, index: number, array: MemoizedComputeds<T, I, R>) => U,
initialValue: U
): U {
return this.#storage.value.reduce((acc: U, item: ReadonlySignal<R>, index): U => {
return cb(acc, item, index, this)
}, initialValue)
}
reduceRight<U>(
cb: (acc: U, current: ReadonlySignal<R>, index: number, array: MemoizedComputeds<T, I, R>) => U,
initialValue: U
): U {
return this.#storage.value.reduceRight((acc: U, item: ReadonlySignal<R>, index): U => {
return cb(acc, item, index, this)
}, initialValue)
}
// SKIP: reverse() – mutating
// SKIP: shift() – mutating
// SKIP: slice() – copying method
some(
cb: (element: ReadonlySignal<R>, index: number, list: MemoizedComputeds<T, I, R>) => boolean,
thisArg?: unknown
): boolean {
return this.#storage.value.some((element, index) => {
return cb(element, index, this)
}, thisArg)
}
// SKIP: sort() – mutating
// SKIP: splice() – mutating
toLocalString(): string {
return this.#storage.toLocaleString()
}
// SKIP: toReversed() - copying method
// SKIP: toSorted() - copying method
toString(): string {
return this.#storage.toString()
}
// SKIP: unshift() – mutating
values(): IterableIterator<ReadonlySignal<R>> {
return this.#storage.value.values()
}
// SKIP: with() - copying method
}