aoye 0.0.2 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,860 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('bobe-shared')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'bobe-shared'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Aoye = {}, global.BobeShared));
5
+ })(this, (function (exports, bobeShared) { 'use strict';
6
+
7
+ const evt = new bobeShared.BaseEvent();
8
+ const G = {
9
+ /** 原子 signal 更新次数 */
10
+ version: 0,
11
+ id: 0,
12
+ /** scope 销毁任务序号 */
13
+ scopeDisposeI: 0,
14
+ PullingSignal: null
15
+ };
16
+ const dirtyLeafs = new bobeShared.SortMap();
17
+ var State = /* @__PURE__ */ ((State2) => {
18
+ State2[State2["Clean"] = 0] = "Clean";
19
+ State2[State2["LinkScopeOnly"] = 64] = "LinkScopeOnly";
20
+ State2[State2["ScopeAbort"] = 32] = "ScopeAbort";
21
+ State2[State2["ScopeReady"] = 16] = "ScopeReady";
22
+ State2[State2["IsScope"] = 8] = "IsScope";
23
+ State2[State2["Unknown"] = 4] = "Unknown";
24
+ State2[State2["Dirty"] = 2] = "Dirty";
25
+ State2[State2["Check"] = 1] = "Check";
26
+ return State2;
27
+ })(State || {});
28
+ const DirtyState = 4 /* Unknown */ | 2 /* Dirty */;
29
+ const ScopeExecuted = 16 /* ScopeReady */ | 32 /* ScopeAbort */;
30
+ const ScopeAbort = 32 /* ScopeAbort */;
31
+
32
+ const leakI = (y, max) => y < 0 || y >= max ? null : y;
33
+ const getLeft = (x, max) => leakI(x * 2 + 1, max);
34
+ const getRight = (x, max) => leakI(x * 2 + 2, max);
35
+ const getParent = (x, max) => leakI(x - 1 >>> 1, max);
36
+ const exchange = (arr, i, j) => [arr[i], arr[j]] = [arr[j], arr[i]];
37
+ class PriorityQueue {
38
+ // 构造函数接受一个compare函数
39
+ // compare返回的-1, 0, 1决定元素是否优先被去除
40
+ constructor(aIsUrgent) {
41
+ this.aIsUrgent = aIsUrgent;
42
+ this.arr = [];
43
+ this.goUp = (arr, current, len) => {
44
+ let i = len - 1;
45
+ while (i > 0) {
46
+ const item = arr[i];
47
+ const pI = getParent(i, len);
48
+ const parent = arr[pI];
49
+ if (this.aIsUrgent(item, parent)) {
50
+ exchange(arr, i, pI);
51
+ i = pI;
52
+ } else {
53
+ break;
54
+ }
55
+ }
56
+ };
57
+ this.goDown = (arr, i) => {
58
+ const len = this.size();
59
+ const half = len >>> 1;
60
+ while (i < half) {
61
+ const lI = getLeft(i, len);
62
+ const rI = getRight(i, len);
63
+ let point = i;
64
+ if (lI != null && this.aIsUrgent(arr[lI], arr[point])) {
65
+ point = lI;
66
+ }
67
+ if (rI != null && this.aIsUrgent(arr[rI], arr[point])) {
68
+ point = rI;
69
+ }
70
+ if (point === i) {
71
+ break;
72
+ }
73
+ exchange(arr, i, point);
74
+ i = point;
75
+ }
76
+ };
77
+ }
78
+ // 添加一个元素
79
+ _add(current) {
80
+ this.arr.push(current);
81
+ const len = this.size();
82
+ if (len === 1) {
83
+ return;
84
+ }
85
+ this.goUp(this.arr, current, len);
86
+ }
87
+ add(...items) {
88
+ items.forEach((it) => this._add(it));
89
+ }
90
+ // 去除头元素并返回
91
+ poll() {
92
+ const { arr } = this;
93
+ const len = this.size();
94
+ if (len <= 2) {
95
+ return arr.shift();
96
+ }
97
+ const last = arr.pop();
98
+ const first = arr[0];
99
+ arr[0] = last;
100
+ this.goDown(this.arr, 0);
101
+ return first;
102
+ }
103
+ // 取得头元素
104
+ peek() {
105
+ return this.arr[0];
106
+ }
107
+ // 取得元素数量
108
+ size() {
109
+ return this.arr.length;
110
+ }
111
+ logTree() {
112
+ const { arr } = this;
113
+ let i = 0;
114
+ let j = 1;
115
+ let level = 0;
116
+ const matrix = [];
117
+ do {
118
+ matrix.push(arr.slice(i, j));
119
+ i = i * 2 + 1;
120
+ j = i + Math.pow(2, level) + 1;
121
+ level++;
122
+ } while (i < arr.length);
123
+ const last = Math.pow(2, matrix.length - 1);
124
+ const arrStr = JSON.stringify(last);
125
+ const halfLen = arrStr.length >>> 1;
126
+ matrix.forEach((it) => {
127
+ const str = JSON.stringify(it);
128
+ const halfIt = str.length >>> 1;
129
+ console.log(str.padStart(halfLen + halfIt, " "));
130
+ });
131
+ console.log("\n");
132
+ }
133
+ }
134
+
135
+ class TaskQueue {
136
+ constructor(callbackAble, aIsUrgent) {
137
+ this.callbackAble = callbackAble;
138
+ this.aIsUrgent = aIsUrgent;
139
+ this.isScheduling = false;
140
+ }
141
+ static create({ callbackAble, aIsUrgent }) {
142
+ const queue = new TaskQueue(callbackAble, aIsUrgent);
143
+ queue.taskQueue = new PriorityQueue(aIsUrgent);
144
+ return queue;
145
+ }
146
+ pushTask(task) {
147
+ const { taskQueue, isScheduling } = this;
148
+ taskQueue._add(task);
149
+ if (!isScheduling) {
150
+ this.callbackAble(this.scheduleTask.bind(this));
151
+ this.isScheduling = true;
152
+ }
153
+ }
154
+ scheduleTask() {
155
+ const { taskQueue } = this;
156
+ const fn = taskQueue.peek();
157
+ if (!fn) return this.isScheduling = false;
158
+ const hasRemain = fn();
159
+ if (hasRemain) {
160
+ this.callbackAble(this.scheduleTask.bind(this));
161
+ return;
162
+ }
163
+ taskQueue.poll();
164
+ evt.emit("one", fn);
165
+ if (taskQueue.size() === 0) {
166
+ evt.emit("done", fn);
167
+ return this.isScheduling = false;
168
+ }
169
+ this.callbackAble(this.scheduleTask.bind(this));
170
+ }
171
+ }
172
+
173
+ let channel = globalThis.MessageChannel ? new MessageChannel() : null;
174
+ if (globalThis.MessageChannel) {
175
+ channel = new MessageChannel();
176
+ }
177
+ let msgId = 0;
178
+ const macro = (fn) => {
179
+ if (!channel) {
180
+ setTimeout(fn);
181
+ }
182
+ const memoId = msgId;
183
+ function onMessage(e) {
184
+ if (memoId === e.data) {
185
+ fn();
186
+ channel.port2.removeEventListener("message", onMessage);
187
+ }
188
+ }
189
+ channel.port2.addEventListener("message", onMessage);
190
+ channel.port1.postMessage(msgId++);
191
+ };
192
+ const p = Promise.resolve();
193
+ const micro = (cb) => {
194
+ p.then(cb);
195
+ };
196
+
197
+ var Scheduler = /* @__PURE__ */ ((Scheduler2) => {
198
+ Scheduler2["Sync"] = "__Sync_";
199
+ Scheduler2["Layout"] = "__Layout_";
200
+ Scheduler2["Micro"] = "__Micro_";
201
+ Scheduler2["Macro"] = "__Macro_";
202
+ return Scheduler2;
203
+ })(Scheduler || {});
204
+ const _scheduler = {
205
+ ["__Sync_" /* Sync */]: defaultScheduler,
206
+ ["__Micro_" /* Micro */]: microScheduler,
207
+ ["__Macro_" /* Macro */]: macroScheduler,
208
+ ["__Layout_" /* Layout */]: layoutScheduler
209
+ };
210
+ const scheduler = (key, value) => _scheduler[key] = value;
211
+ function defaultScheduler(effects) {
212
+ for (const effect of effects) {
213
+ effect.runIfDirty();
214
+ }
215
+ }
216
+ let microSTaskQueue;
217
+ let macroSTaskQueue;
218
+ let layoutSTaskQueue;
219
+ function microScheduler(effects) {
220
+ microSTaskQueue = microSTaskQueue || TaskQueue.create({ callbackAble: micro, aIsUrgent: (a, b) => a.time < b.time });
221
+ microSTaskQueue.pushTask(defaultScheduler.bind(null, effects));
222
+ }
223
+ function macroScheduler(effects) {
224
+ macroSTaskQueue = macroSTaskQueue || TaskQueue.create({ callbackAble: macro, aIsUrgent: (a, b) => a.time < b.time });
225
+ macroSTaskQueue.pushTask(defaultScheduler.bind(null, effects));
226
+ }
227
+ function layoutScheduler(effects) {
228
+ layoutSTaskQueue = layoutSTaskQueue || TaskQueue.create({ callbackAble: macro, aIsUrgent: (a, b) => a.time < b.time });
229
+ layoutSTaskQueue.pushTask(defaultScheduler.bind(null, effects));
230
+ }
231
+
232
+ var __defProp$1 = Object.defineProperty;
233
+ var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
234
+ var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
235
+ var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
236
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
237
+ var __spreadValues$1 = (a, b) => {
238
+ for (var prop in b || (b = {}))
239
+ if (__hasOwnProp$2.call(b, prop))
240
+ __defNormalProp$1(a, prop, b[prop]);
241
+ if (__getOwnPropSymbols$2)
242
+ for (var prop of __getOwnPropSymbols$2(b)) {
243
+ if (__propIsEnum$2.call(b, prop))
244
+ __defNormalProp$1(a, prop, b[prop]);
245
+ }
246
+ return a;
247
+ };
248
+ const DefaultDFSOpt = {
249
+ isUp: false,
250
+ begin: null,
251
+ complete: null,
252
+ breakStack: [],
253
+ breakLine: null,
254
+ breakNode: null
255
+ };
256
+ function dfs(root, opt = {}) {
257
+ const { isUp, begin, complete, breakStack: lineStack, breakLine } = __spreadValues$1(__spreadValues$1({}, DefaultDFSOpt), opt);
258
+ let node = opt.breakNode || root;
259
+ let line = breakLine;
260
+ const listKey = isUp ? "recStart" : "emitStart";
261
+ const nodeKey = isUp ? "upstream" : "downstream";
262
+ const nextLineKey = isUp ? "nextRecLine" : "nextEmitLine";
263
+ const reverseNodeKey = isUp ? "downstream" : "upstream";
264
+ while (1) {
265
+ let notGoDeep = begin == null ? void 0 : begin({
266
+ node,
267
+ lineFromUp: line,
268
+ walkedLine: lineStack
269
+ });
270
+ lineStack.push(line);
271
+ line = node[listKey];
272
+ if (line && !notGoDeep) {
273
+ const firstChild = line[nodeKey];
274
+ node = firstChild;
275
+ continue;
276
+ }
277
+ while (1) {
278
+ const noGoSibling = complete == null ? void 0 : complete({
279
+ node,
280
+ lineToDeep: line,
281
+ walkedLine: lineStack,
282
+ notGoDeep
283
+ });
284
+ line = lineStack.pop();
285
+ if (node === root) {
286
+ return;
287
+ }
288
+ notGoDeep = false;
289
+ const nextLine = line[nextLineKey];
290
+ if (!noGoSibling && nextLine) {
291
+ line = nextLine;
292
+ node = nextLine[nodeKey];
293
+ break;
294
+ }
295
+ node = line[reverseNodeKey];
296
+ }
297
+ }
298
+ }
299
+
300
+ class Line {
301
+ constructor() {
302
+ /** 上游顶点 */
303
+ this.upstream = null;
304
+ /** 上游节点 发出的上一条线 */
305
+ this.prevEmitLine = null;
306
+ /** 上游节点 发出的下一条线 */
307
+ this.nextEmitLine = null;
308
+ /** 下游顶点 */
309
+ this.downstream = null;
310
+ /** 下游节点 接收的上一条线 */
311
+ this.prevRecLine = null;
312
+ /** 下游节点 接收的下一条线 */
313
+ this.nextRecLine = null;
314
+ /** 表示 scope 当前存在的 外部 link */
315
+ this.prevOutLink = null;
316
+ this.nextOutLink = null;
317
+ }
318
+ static link(v1, v2) {
319
+ let { emitEnd } = v1, { recEnd, recStart } = v2, noRecEnd = !recEnd, head = { nextRecLine: recStart }, line;
320
+ recEnd = recEnd || head;
321
+ const { nextRecLine } = recEnd || {};
322
+ if (!nextRecLine) {
323
+ line = new Line();
324
+ Line.emit_line(v1, line);
325
+ Line.rec_line(v2, line);
326
+ emitEnd && Line.line_line_emit(emitEnd, line);
327
+ !noRecEnd && Line.line_line_rec(recEnd, line);
328
+ } else if (nextRecLine.upstream === v1) {
329
+ v2.recEnd = nextRecLine;
330
+ } else {
331
+ line = new Line();
332
+ Line.emit_line(v1, line);
333
+ Line.rec_line(v2, line);
334
+ emitEnd && Line.line_line_emit(emitEnd, line);
335
+ Line.insert_line_rec(recEnd, nextRecLine, line);
336
+ }
337
+ for (const key in head) {
338
+ head[key] = null;
339
+ }
340
+ if (line && v2.scope && v1.scope !== v2.scope && !(v1.state & State.IsScope)) {
341
+ const first = v2.scope.outLink;
342
+ if (!first) {
343
+ v2.scope.outLink = line;
344
+ } else {
345
+ first.prevOutLink = line;
346
+ line.nextOutLink = first;
347
+ v2.scope.outLink = line;
348
+ }
349
+ }
350
+ }
351
+ static unlink(line) {
352
+ let { prevEmitLine, nextEmitLine, prevRecLine, nextRecLine, upstream, downstream, nextOutLink, prevOutLink } = line;
353
+ line.prevEmitLine = null;
354
+ line.nextEmitLine = null;
355
+ line.prevRecLine = null;
356
+ line.nextRecLine = null;
357
+ line.upstream = null;
358
+ line.downstream = null;
359
+ line.prevOutLink = null;
360
+ line.nextOutLink = null;
361
+ const downNode = downstream;
362
+ if (prevOutLink) {
363
+ prevOutLink.nextOutLink = nextOutLink;
364
+ }
365
+ if (nextOutLink) {
366
+ nextOutLink.prevOutLink = prevOutLink;
367
+ }
368
+ if (downNode.scope && downNode.scope.outLink === line) {
369
+ downNode.scope.outLink = nextOutLink;
370
+ }
371
+ if (prevEmitLine) {
372
+ prevEmitLine.nextEmitLine = nextEmitLine;
373
+ } else {
374
+ upstream.emitStart = nextEmitLine;
375
+ }
376
+ if (nextEmitLine) {
377
+ nextEmitLine.prevEmitLine = prevEmitLine;
378
+ } else {
379
+ upstream.emitEnd = prevEmitLine;
380
+ }
381
+ if (prevRecLine) {
382
+ prevRecLine.nextRecLine = nextRecLine;
383
+ } else {
384
+ downstream.recStart = nextRecLine;
385
+ }
386
+ if (nextRecLine) {
387
+ nextRecLine.prevRecLine = prevRecLine;
388
+ } else {
389
+ downstream.recEnd = prevRecLine;
390
+ }
391
+ }
392
+ static unlinkRec(line) {
393
+ let toDel = line;
394
+ while (toDel) {
395
+ const memoNext = toDel.nextRecLine;
396
+ Line.unlink(toDel);
397
+ toDel = memoNext;
398
+ }
399
+ }
400
+ static unlinkEmit(line) {
401
+ let toDel = line;
402
+ while (toDel) {
403
+ const memoNext = toDel.nextEmitLine;
404
+ Line.unlink(toDel);
405
+ toDel = memoNext;
406
+ }
407
+ }
408
+ /** 上游节点 连 link */
409
+ static emit_line(upstream, line) {
410
+ if (!upstream.emitStart) {
411
+ upstream.emitStart = line;
412
+ }
413
+ upstream.emitEnd = line;
414
+ line.upstream = upstream;
415
+ }
416
+ /** 下游节点 连 link */
417
+ static rec_line(downstream, line) {
418
+ if (!downstream.recStart) {
419
+ downstream.recStart = line;
420
+ }
421
+ downstream.recEnd = line;
422
+ line.downstream = downstream;
423
+ }
424
+ /** 同一节点发出的 两个条线 相连 */
425
+ static line_line_emit(l1, l2) {
426
+ if (!l1 || !l2) return;
427
+ l1.nextEmitLine = l2;
428
+ l2.prevEmitLine = l1;
429
+ }
430
+ /** 同一节点接收的 两个条线 相连 */
431
+ static line_line_rec(l1, l2) {
432
+ if (!l1 || !l2) return;
433
+ l1.nextRecLine = l2;
434
+ l2.prevRecLine = l1;
435
+ }
436
+ static insert_line_emit(l1, l2, ins) {
437
+ l1.nextEmitLine = ins;
438
+ ins.prevEmitLine = l1;
439
+ l2.prevEmitLine = ins;
440
+ ins.nextEmitLine = l2;
441
+ }
442
+ static insert_line_rec(l1, l2, ins) {
443
+ l1.nextRecLine = ins;
444
+ ins.prevRecLine = l1;
445
+ l2.prevRecLine = ins;
446
+ ins.nextRecLine = l2;
447
+ }
448
+ }
449
+
450
+ function unlinkRecWithScope(line) {
451
+ let toDel = line;
452
+ while (toDel) {
453
+ const memoNext = toDel.nextRecLine;
454
+ const upstream = toDel.upstream;
455
+ if (upstream.state & State.IsScope) {
456
+ dispose.call(upstream);
457
+ } else {
458
+ unlinkSingleLine(toDel);
459
+ }
460
+ toDel = memoNext;
461
+ }
462
+ }
463
+ function unlinkSingleLine(line) {
464
+ const upstream = line.upstream;
465
+ if (upstream.emitStart === upstream.emitEnd) {
466
+ unlinkSingleRefedNode(upstream);
467
+ } else {
468
+ Line.unlink(line);
469
+ }
470
+ }
471
+ function unlinkSingleRefedNode(delRoot) {
472
+ let toUnlink;
473
+ dfs(delRoot, {
474
+ isUp: true,
475
+ begin: ({ node }) => {
476
+ doUnlink(toUnlink);
477
+ toUnlink = null;
478
+ if (node.emitStart !== node.emitEnd) {
479
+ return true;
480
+ }
481
+ },
482
+ complete: ({ node, notGoDeep }) => {
483
+ doUnlink(toUnlink);
484
+ toUnlink = null;
485
+ const isSingleRefed = !notGoDeep;
486
+ if (isSingleRefed) {
487
+ toUnlink = node.emitStart;
488
+ }
489
+ }
490
+ });
491
+ doUnlink(toUnlink);
492
+ }
493
+ function doUnlink(line) {
494
+ if (!line) {
495
+ return;
496
+ }
497
+ Line.unlink(line);
498
+ }
499
+ function dispose() {
500
+ let toDel = this.recStart;
501
+ while (toDel) {
502
+ const memoNext = toDel.nextRecLine;
503
+ const upstream = toDel.upstream;
504
+ if (upstream.state & State.IsScope) {
505
+ dfs(upstream, {
506
+ isUp: true,
507
+ begin: ({ node }) => {
508
+ if (!(node.state & State.IsScope) || node.state & ScopeAbort) return true;
509
+ },
510
+ complete: ({ node: scope, notGoDeep }) => {
511
+ const shouldAbort = !notGoDeep;
512
+ if (shouldAbort) {
513
+ releaseScope(scope);
514
+ }
515
+ }
516
+ });
517
+ } else {
518
+ unlinkSingleLine(toDel);
519
+ }
520
+ toDel = memoNext;
521
+ }
522
+ releaseScope(this);
523
+ doUnlink(this.emitStart);
524
+ }
525
+ function releaseScope(scope) {
526
+ var _a;
527
+ let outLink = scope.outLink;
528
+ while (outLink) {
529
+ const memoNext = outLink.nextOutLink;
530
+ unlinkSingleLine(outLink);
531
+ outLink = memoNext;
532
+ }
533
+ scope.state |= State.ScopeAbort;
534
+ (_a = scope.clean) == null ? void 0 : _a.call(scope);
535
+ scope.clean = null;
536
+ }
537
+ function clean(cb) {
538
+ G.PullingSignal.clean = () => runWithPulling(cb, null);
539
+ }
540
+ function runWithPulling(fn, signal) {
541
+ const prevPulling = G.PullingSignal;
542
+ G.PullingSignal = signal;
543
+ fn();
544
+ G.PullingSignal = prevPulling;
545
+ }
546
+
547
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
548
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
549
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
550
+ var __objRest = (source, exclude) => {
551
+ var target = {};
552
+ for (var prop in source)
553
+ if (__hasOwnProp$1.call(source, prop) && exclude.indexOf(prop) < 0)
554
+ target[prop] = source[prop];
555
+ if (source != null && __getOwnPropSymbols$1)
556
+ for (var prop of __getOwnPropSymbols$1(source)) {
557
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$1.call(source, prop))
558
+ target[prop] = source[prop];
559
+ }
560
+ return target;
561
+ };
562
+ const markDeep = (signal) => {
563
+ let level = 0;
564
+ dfs(signal, {
565
+ isUp: false,
566
+ begin: ({ node }) => {
567
+ if (node.state & (State.Check | State.Unknown | State.Dirty) || node.isDisabled()) {
568
+ return true;
569
+ }
570
+ const isEffect = level > 0;
571
+ const isLeaf = !node.emitStart || node.emitStart.downstream === node.scope;
572
+ if (isEffect) {
573
+ node.state |= State.Unknown;
574
+ } else if (!isLeaf) {
575
+ node.state |= State.Dirty;
576
+ }
577
+ if (isLeaf && isEffect) {
578
+ dirtyLeafs.add(node.scheduler, node);
579
+ }
580
+ level++;
581
+ }
582
+ });
583
+ for (const key in dirtyLeafs.data) {
584
+ const effects = dirtyLeafs.data[key];
585
+ const scheduler = _scheduler[key];
586
+ scheduler(effects);
587
+ }
588
+ dirtyLeafs.clear();
589
+ };
590
+ const _Signal = class _Signal {
591
+ constructor(nextValue, customPull) {
592
+ this.nextValue = nextValue;
593
+ this.customPull = customPull;
594
+ this.version = -1;
595
+ this.id = G.id++;
596
+ this.state = State.Clean;
597
+ /** 当前节点创建时处于的 effect 就是 scope */
598
+ this.scope = G.PullingSignal;
599
+ this.recEnd = null;
600
+ this.recStart = null;
601
+ this.emitStart = null;
602
+ this.emitEnd = null;
603
+ this.scheduler = null;
604
+ this.value = null;
605
+ this.outLink = null;
606
+ this.pull = null;
607
+ /** 记录当前 effect 中 clean */
608
+ this.clean = null;
609
+ }
610
+ static create(nextValue, _a) {
611
+ var _b = _a, { customPull, isScope, immediate } = _b, rest = __objRest(_b, ["customPull", "isScope", "immediate"]);
612
+ const s = new _Signal(nextValue, customPull);
613
+ s.pull = s.customPull || s.DEFAULT_PULL;
614
+ Object.assign(s, rest);
615
+ if (isScope) {
616
+ s.state |= State.IsScope;
617
+ }
618
+ return s;
619
+ }
620
+ DEFAULT_PULL() {
621
+ return this.nextValue;
622
+ }
623
+ /**
624
+ * 递归拉取负责建立以来链
625
+ */
626
+ pullRecurse(shouldLink = true) {
627
+ var _a, _b;
628
+ let downstream = G.PullingSignal;
629
+ const isScope = this.state & State.IsScope;
630
+ if (
631
+ // 1. 外部支持 link
632
+ shouldLink && // 2. 有下游
633
+ downstream && // 3. 下游是 watcher,不链接非 scope
634
+ !(downstream.state & State.LinkScopeOnly && !isScope)
635
+ ) {
636
+ Line.link(this, downstream);
637
+ }
638
+ try {
639
+ if (this.version === G.version) {
640
+ return this.value;
641
+ }
642
+ this.recEnd = null;
643
+ G.PullingSignal = this;
644
+ (_a = this.clean) == null ? void 0 : _a.call(this);
645
+ this.clean = null;
646
+ let v = this.pull();
647
+ if (isScope && typeof v === "function") {
648
+ const fn = v;
649
+ this.clean = () => runWithPulling(fn, null);
650
+ v = this.value;
651
+ }
652
+ this.pull = this.customPull || this.DEFAULT_PULL;
653
+ this.value = v;
654
+ this.version = G.version;
655
+ return this.value;
656
+ } catch (error) {
657
+ console.error("\u8BA1\u7B97\u5C5E\u6027\u62A5\u9519\u8FD9\u6B21\u4E0D\u89E6\u53D1\uFF0C\u540E\u7EED\u72B6\u6001\u53EF\u80FD\u51FA\u9519", error);
658
+ return this.value;
659
+ } finally {
660
+ const toDel = (_b = this.recEnd) == null ? void 0 : _b.nextRecLine;
661
+ unlinkRecWithScope(toDel);
662
+ G.PullingSignal = downstream;
663
+ }
664
+ }
665
+ pullDeep() {
666
+ const signal = this;
667
+ if (!(signal.state & DirtyState)) {
668
+ return this.value;
669
+ }
670
+ dfs(signal, {
671
+ isUp: true,
672
+ begin: ({ node }) => {
673
+ if (node.state & State.Check || !(node.state & DirtyState) || node.isDisabled()) {
674
+ return true;
675
+ }
676
+ node.state |= State.Check;
677
+ },
678
+ complete: ({ node, notGoDeep: currentClean, walkedLine }) => {
679
+ let noGoSibling = false;
680
+ const last = walkedLine[walkedLine.length - 1];
681
+ const downstream = last == null ? void 0 : last.downstream;
682
+ if (currentClean) ; else if (node.state & State.Dirty) {
683
+ if (!node.recStart && node.value !== node.nextValue) {
684
+ node.markDownStreamsDirty();
685
+ node.state &= ~State.Dirty;
686
+ node.state &= ~State.Check;
687
+ return;
688
+ } else {
689
+ const prevPulling = G.PullingSignal;
690
+ G.PullingSignal = downstream;
691
+ const prevValue = node.value;
692
+ node.pullRecurse(false);
693
+ if (prevValue !== node.value) {
694
+ node.markDownStreamsDirty();
695
+ }
696
+ node.state &= ~State.Dirty;
697
+ G.PullingSignal = prevPulling;
698
+ noGoSibling = true;
699
+ }
700
+ } else if (node.state & State.Unknown) {
701
+ node.state &= ~State.Unknown;
702
+ }
703
+ node.version = G.version;
704
+ node.state &= ~State.Check;
705
+ return noGoSibling;
706
+ }
707
+ });
708
+ return this.value;
709
+ }
710
+ get v() {
711
+ if (this.isDisabled()) {
712
+ return this.value;
713
+ }
714
+ if (!this.recStart) {
715
+ return this.pullRecurse(true);
716
+ }
717
+ return this.pullDeep();
718
+ }
719
+ // pause() {
720
+ // this.state |= State.SelfPaused;
721
+ // }
722
+ // resume() {
723
+ // this.state &= ~State.SelfPaused;
724
+ // }
725
+ markDownStreamsDirty() {
726
+ let point = this.emitStart;
727
+ while (point != null) {
728
+ const downstream = point.downstream;
729
+ downstream.state |= State.Dirty;
730
+ downstream.state &= ~State.Unknown;
731
+ point = point.nextEmitLine;
732
+ }
733
+ }
734
+ set v(v) {
735
+ if (this.isDisabled() || this.nextValue === v) {
736
+ return;
737
+ }
738
+ this.nextValue = v;
739
+ this.pull = this.DEFAULT_PULL;
740
+ G.version++;
741
+ markDeep(this);
742
+ }
743
+ runIfDirty() {
744
+ this.state & (State.Unknown | State.Dirty) && this.v;
745
+ }
746
+ isDisabled() {
747
+ return (
748
+ // scope 被取消
749
+ this.scope && this.scope.state & State.ScopeAbort || // 是 scope 节点,且处于 ready 状态,不需要重复执行
750
+ this.state & State.IsScope && this.state & ScopeExecuted
751
+ );
752
+ }
753
+ };
754
+ _Signal.Pulling = null;
755
+ let Signal = _Signal;
756
+
757
+ var __defProp = Object.defineProperty;
758
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
759
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
760
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
761
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
762
+ var __spreadValues = (a, b) => {
763
+ for (var prop in b || (b = {}))
764
+ if (__hasOwnProp.call(b, prop))
765
+ __defNormalProp(a, prop, b[prop]);
766
+ if (__getOwnPropSymbols)
767
+ for (var prop of __getOwnPropSymbols(b)) {
768
+ if (__propIsEnum.call(b, prop))
769
+ __defNormalProp(a, prop, b[prop]);
770
+ }
771
+ return a;
772
+ };
773
+ ({
774
+ scheduler: Scheduler.Sync});
775
+ const $ = (init) => {
776
+ let intiValue, customPull;
777
+ if (init instanceof Function) {
778
+ intiValue = null;
779
+ customPull = init;
780
+ } else {
781
+ intiValue = init;
782
+ }
783
+ const s = Signal.create(intiValue, {
784
+ scheduler: Scheduler.Sync,
785
+ isScope: false,
786
+ customPull
787
+ });
788
+ return s;
789
+ };
790
+ const effect = (customPull, depOrOpt, opt) => {
791
+ var _a;
792
+ const hasDep = Array.isArray(depOrOpt);
793
+ opt = hasDep ? opt || {} : depOrOpt || {};
794
+ if (!hasDep) {
795
+ const s2 = Signal.create(null, __spreadValues({
796
+ customPull,
797
+ scheduler: Scheduler.Sync,
798
+ isScope: true
799
+ }, opt));
800
+ s2.v;
801
+ const bound2 = dispose.bind(s2);
802
+ bound2.ins = s2;
803
+ return bound2;
804
+ }
805
+ let mounted = false;
806
+ const deps = depOrOpt;
807
+ const immediate = deps.length === 0 ? true : (_a = opt.immediate) != null ? _a : true;
808
+ const vs = Array.from({ length: deps.length }, () => ({ old: null, val: null }));
809
+ const s = Signal.create(null, __spreadValues({
810
+ customPull() {
811
+ for (let i = 0; i < deps.length; i++) {
812
+ const value = deps[i].v;
813
+ vs[i].old = vs[i].val;
814
+ vs[i].val = value;
815
+ }
816
+ if (mounted || immediate) {
817
+ s.state |= State.LinkScopeOnly;
818
+ customPull(...vs);
819
+ s.state &= ~State.LinkScopeOnly;
820
+ }
821
+ mounted = true;
822
+ },
823
+ scheduler: Scheduler.Sync,
824
+ isScope: true
825
+ }, opt));
826
+ s.v;
827
+ const bound = dispose.bind(s);
828
+ bound.ins = s;
829
+ return bound;
830
+ };
831
+ const scope = (customPull) => {
832
+ const s = Signal.create(null, {
833
+ customPull,
834
+ scheduler: Scheduler.Sync,
835
+ isScope: true
836
+ });
837
+ s.v;
838
+ s.state |= State.ScopeReady;
839
+ const bound = dispose.bind(s);
840
+ bound.ins = s;
841
+ return bound;
842
+ };
843
+ const customEffect = (opt) => {
844
+ return ((init, innerOpt = {}) => {
845
+ return effect(init, __spreadValues(__spreadValues({}, opt), innerOpt));
846
+ });
847
+ };
848
+
849
+ exports.$ = $;
850
+ exports.Scheduler = Scheduler;
851
+ exports.TaskQueue = TaskQueue;
852
+ exports.clean = clean;
853
+ exports.customEffect = customEffect;
854
+ exports.effect = effect;
855
+ exports.runWithPulling = runWithPulling;
856
+ exports.scheduler = scheduler;
857
+ exports.scope = scope;
858
+
859
+ }));
860
+ //# sourceMappingURL=index.umd.js.map