deno.land / x / solid@v1.5.6 / bench / s-mod.cjs

نووسراو ببینە
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// Modified version of S.js[https://github.com/adamhaile/S] by Adam Haile// Comparator memos from VSJolund fork https://github.com/VSjolund/vs-bindconst equalFn = (a, b) => a === b;const ERROR = Symbol("error");// Public interfacefunction createRoot(fn, detachedOwner) { detachedOwner && (Owner = detachedOwner); let owner = Owner, listener = Listener, root = fn.length === 0 ? UNOWNED : createComputationNode(null, null), result = undefined, disposer = function _dispose() { if (RunningClock !== null) { RootClock.disposes.add(root); } else { dispose(root); } }; Owner = root; Listener = null; try { result = fn(disposer); } catch (err) { const fns = lookup(Owner, ERROR); if (!fns) throw err; fns.forEach(f => f(err)); } finally { RootClock.afters.run(f => f()); Listener = listener; Owner = owner; } return result;}function createSignal(value, areEqual) { const d = new DataNode(value); let setter; if (areEqual) { let age = -1; setter = v => { if (!areEqual(v, value)) { const time = RootClock.time; if (time === age) { throw new Error(`Conflicting value update: ${v} is not the same as ${value}`); } age = time; value = v; d.next(v); } }; } else setter = d.next.bind(d); return [d.current.bind(d), setter];}function createEffect(fn, value) { createComputationNode(fn, value);}function createDependentEffect(fn, deps, defer) { const resolved = Array.isArray(deps) ? callAll(deps) : deps; defer = !!defer; createComputationNode(value => { const listener = Listener; resolved(); if (defer) defer = false; else { Listener = null; value = fn(value); Listener = listener; } return value; });}function createMemo(fn, value, areEqual) { var node = createComputationNode(fn, value); node.comparator = areEqual || null; return () => { if (Listener !== null) { const state = node.state; if ((state & 7) !== 0) { liftComputation(node); } if (node.age === RootClock.time && state === 8) { throw new Error("Circular dependency."); } if ((state & 16) === 0) { if (node.log === null) node.log = createLog(); logRead(node.log); } } return node.value; };}function batch(fn) { let result = undefined; if (RunningClock !== null) result = fn(); else { RunningClock = RootClock; RunningClock.changes.reset(); try { result = fn(); event(); } finally { RunningClock = null; } } return result;}function sample(fn) { let result, listener = Listener; Listener = null; result = fn(); Listener = listener; return result;}function afterEffects(fn) { if (RunningClock !== null) RunningClock.afters.add(fn); else RootClock.afters.add(fn);}function onCleanup(fn) { if (Owner === null) console.warn("cleanups created outside a `createRoot` or `render` will never be run"); else if (Owner.cleanups === null) Owner.cleanups = [fn]; else Owner.cleanups.push(fn);}function onError(fn) { if (Owner === null) console.warn("error handlers created outside a `createRoot` or `render` will never be run"); else if (Owner.context === null) Owner.context = { [ERROR]: [fn] }; else if (!Owner.context[ERROR]) Owner.context[ERROR] = [fn]; else Owner.context[ERROR].push(fn);}function isListening() { return Listener !== null;}function createContext(defaultValue) { const id = Symbol("context"); return { id, Provider: createProvider(id), defaultValue };}function useContext(context) { return lookup(Owner, context.id) || context.defaultValue;}function getOwner() { return Owner;}// Internal implementation/// Graph classes and operationsclass DataNode { constructor(value) { this.value = value; this.pending = NOTPENDING; this.log = null; } current() { if (Listener !== null) { if (this.log === null) this.log = createLog(); logRead(this.log); } return this.value; } next(value) { if (RunningClock !== null) { if (this.pending !== NOTPENDING) { // value has already been set once, check for conflicts if (value !== this.pending) { throw new Error("conflicting changes: " + value + " !== " + this.pending); } } else { // add to list of changes this.pending = value; RootClock.changes.add(this); } } else { // not batching, respond to change now if (this.log !== null) { this.pending = value; RootClock.changes.add(this); event(); } else { this.value = value; } } return value; }}function createComputationNode(fn, value) { const node = { fn, value, age: RootClock.time, state: 0, comparator: null, source1: null, source1slot: 0, sources: null, sourceslots: null, dependents: null, dependentslot: 0, dependentcount: 0, owner: Owner, owned: null, log: null, context: null, cleanups: null }; if (fn === null) return node; let owner = Owner, listener = Listener; if (owner === null) console.warn("computations created outside a `createRoot` or `render` will never be disposed"); Owner = Listener = node; if (RunningClock === null) { toplevelComputation(node); } else node.value = node.fn(node.value); if (owner && owner !== UNOWNED) { if (owner.owned === null) owner.owned = [node]; else owner.owned.push(node); } Owner = owner; Listener = listener; return node;}function createClock() { return { time: 0, changes: new Queue(), updates: new Queue(), disposes: new Queue(), afters: new Queue() };}function createLog() { return { node1: null, node1slot: 0, nodes: null, nodeslots: null };}class Queue { constructor() { this.items = []; this.count = 0; } reset() { this.count = 0; } add(item) { this.items[this.count++] = item; } run(fn) { let items = this.items; for (let i = 0; i < this.count; i++) { try { const item = items[i]; items[i] = null; fn(item); } catch (err) { const fns = lookup(Owner, ERROR); if (!fns) throw err; fns.forEach(f => f(err)); } } this.count = 0; }}// "Globals" used to keep track of current system statelet RootClock = createClock(), RunningClock = null, // currently running clock Listener = null, // currently listening computation Owner = null, // owner for new computations Pending = null; // pending node// Constantslet NOTPENDING = {}, UNOWNED = createComputationNode(null, null);// State// 1 - Stale, 2 - Pending, 4 - Pending Disposal, 8 - Running, 16 - Disposed// Functionsfunction callAll(ss) { return function all() { for (let i = 0; i < ss.length; i++) ss[i](); };}function lookup(owner, key) { return ( owner && ((owner.context && owner.context[key]) || (owner.owner && lookup(owner.owner, key))) );}function resolveChildren(children) { if (typeof children === "function") return createMemo(() => resolveChildren(children())); if (Array.isArray(children)) { const results = []; for (let i = 0; i < children.length; i++) { let result = resolveChildren(children[i]); Array.isArray(result) ? results.push.apply(results, result) : results.push(result); } return results; } return children;}function createProvider(id) { return function provider(props) { let rendered; createComputationNode(() => { Owner.context = { [id]: props.value }; rendered = sample(() => resolveChildren(props.children)); }); return rendered; };}function logRead(from) { let to = Listener, fromslot, toslot = to.source1 === null ? -1 : to.sources === null ? 0 : to.sources.length; if (from.node1 === null) { from.node1 = to; from.node1slot = toslot; fromslot = -1; } else if (from.nodes === null) { if (from.node1 === to) return; from.nodes = [to]; from.nodeslots = [toslot]; fromslot = 0; } else { fromslot = from.nodes.length; if (from.nodes[fromslot - 1] === to) return; from.nodes.push(to); from.nodeslots.push(toslot); } if (to.source1 === null) { to.source1 = from; to.source1slot = fromslot; } else if (to.sources === null) { to.sources = [from]; to.sourceslots = [fromslot]; } else { to.sources.push(from); to.sourceslots.push(fromslot); }}function liftComputation(node) { if ((node.state & 6) !== 0) { applyUpstreamUpdates(node); } if ((node.state & 1) !== 0) { updateNode(node); } resetComputation(node, 31);}function event() { // b/c we might be under a top level S.root(), have to preserve current root let owner = Owner; RootClock.updates.reset(); RootClock.time++; try { run(RootClock); } finally { RunningClock = Listener = null; Owner = owner; }}function toplevelComputation(node) { RunningClock = RootClock; RootClock.changes.reset(); RootClock.updates.reset(); try { node.value = node.fn(node.value); if (RootClock.changes.count > 0 || RootClock.updates.count > 0) { RootClock.time++; run(RootClock); } } catch (err) { const fns = lookup(Owner, ERROR); if (!fns) throw err; fns.forEach(f => f(err)); } finally { RunningClock = Owner = Listener = null; }}function run(clock) { let running = RunningClock, count = 0; RunningClock = clock; clock.disposes.reset(); // for each batch ... while (clock.changes.count !== 0 || clock.updates.count !== 0 || clock.disposes.count !== 0) { if (count > 0) // don't tick on first run, or else we expire already scheduled updates clock.time++; clock.changes.run(applyDataChange); clock.updates.run(updateNode); clock.disposes.run(dispose); // if there are still changes after excessive batches, assume runaway if (count++ > 1e5) { throw new Error("Runaway clock detected"); } } clock.afters.run(f => f()); RunningClock = running;}function applyDataChange(data) { data.value = data.pending; data.pending = NOTPENDING; if (data.log) setComputationState(data.log, stateStale);}function updateNode(node) { const state = node.state; if ((state & 16) === 0) { if ((state & 2) !== 0) { node.dependents[node.dependentslot++] = null; if (node.dependentslot === node.dependentcount) { resetComputation(node, 14); } } else if ((state & 1) !== 0) { if ((state & 4) !== 0) { liftComputation(node); } else if (node.comparator) { const current = updateComputation(node); const comparator = node.comparator; if (!comparator(current, node.value)) { markDownstreamComputations(node, false, true); } } else { updateComputation(node); } } }}function updateComputation(node) { const value = node.value, owner = Owner, listener = Listener; Owner = Listener = node; node.state = 8; cleanupNode(node, false); node.value = node.fn(node.value); resetComputation(node, 31); Owner = owner; Listener = listener; return value;}function stateStale(node) { const time = RootClock.time; if (node.age < time) { node.state |= 1; node.age = time; setDownstreamState(node, !!node.comparator); }}function statePending(node) { const time = RootClock.time; if (node.age < time) { node.state |= 2; let dependents = node.dependents || (node.dependents = []); dependents[node.dependentcount++] = Pending; setDownstreamState(node, true); }}function pendingStateStale(node) { if ((node.state & 2) !== 0) { node.state = 1; const time = RootClock.time; if (node.age < time) { node.age = time; if (!node.comparator) { markDownstreamComputations(node, false, true); } } }}function setDownstreamState(node, pending) { RootClock.updates.add(node); if (node.comparator) { const pending = Pending; Pending = node; markDownstreamComputations(node, true, false); Pending = pending; } else { markDownstreamComputations(node, pending, false); }}function markDownstreamComputations(node, onchange, dirty) { const owned = node.owned; if (owned !== null) { const pending = onchange && !dirty; markForDisposal(owned, pending, RootClock.time); } const log = node.log; if (log !== null) { setComputationState(log, dirty ? pendingStateStale : onchange ? statePending : stateStale); }}function setComputationState(log, stateFn) { const node1 = log.node1, nodes = log.nodes; if (node1 !== null) stateFn(node1); if (nodes !== null) { for (let i = 0, ln = nodes.length; i < ln; i++) { stateFn(nodes[i]); } }}function markForDisposal(children, pending, time) { for (let i = 0, ln = children.length; i < ln; i++) { const child = children[i]; if (child !== null) { if (pending) { if ((child.state & 16) === 0) { child.state |= 4; } } else { child.age = time; child.state = 16; } const owned = child.owned; if (owned !== null) markForDisposal(owned, pending, time); } }}function applyUpstreamUpdates(node) { if ((node.state & 4) !== 0) { const owner = node.owner; if ((owner.state & 7) !== 0) liftComputation(owner); node.state &= ~4; } if ((node.state & 2) !== 0) { const slots = node.dependents; for (let i = node.dependentslot, ln = node.dependentcount; i < ln; i++) { const slot = slots[i]; if (slot != null) liftComputation(slot); slots[i] = null; } node.state &= ~2; }}function cleanupNode(node, final) { let source1 = node.source1, sources = node.sources, sourceslots = node.sourceslots, cleanups = node.cleanups, owned = node.owned, i, len; if (cleanups !== null) { for (i = 0; i < cleanups.length; i++) { cleanups[i](final); } node.cleanups = null; } node.context = null; if (owned !== null) { for (i = 0; i < owned.length; i++) { dispose(owned[i]); } node.owned = null; } if (source1 !== null) { cleanupSource(source1, node.source1slot); node.source1 = null; } if (sources !== null) { for (i = 0, len = sources.length; i < len; i++) { cleanupSource(sources.pop(), sourceslots.pop()); } }}function cleanupSource(source, slot) { let nodes = source.nodes, nodeslots = source.nodeslots, last, lastslot; if (slot === -1) { source.node1 = null; } else { last = nodes.pop(); lastslot = nodeslots.pop(); if (slot !== nodes.length) { nodes[slot] = last; nodeslots[slot] = lastslot; if (lastslot === -1) { last.source1slot = slot; } else { last.sourceslots[lastslot] = slot; } } }}function resetComputation(node, flags) { node.state &= ~flags; node.dependentslot = 0; node.dependentcount = 0;}function dispose(node) { node.fn = null; node.log = null; node.dependents = null; cleanupNode(node, true); resetComputation(node, 31);}
module.exports = { createRoot, createComputed: createEffect, createSignal}
solid

Version Info

Tagged at
a year ago