aoye 0.0.6 → 0.0.8

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.
package/dist/aoye.cjs.js CHANGED
@@ -2,30 +2,34 @@
2
2
 
3
3
  var bobeShared = require('bobe-shared');
4
4
 
5
- const evt = new bobeShared.BaseEvent();
5
+ const rawToProxy = /* @__PURE__ */ new WeakMap();
6
+ new bobeShared.BaseEvent();
6
7
  const G = {
7
8
  /** 原子 signal 更新次数 */
8
9
  version: 0,
9
10
  id: 0,
10
11
  /** scope 销毁任务序号 */
11
12
  scopeDisposeI: 0,
12
- PullingSignal: null
13
+ PullingSignal: null,
14
+ /** 表示当前处于 pull 递归中 */
15
+ PullingRecurseDeep: 0
13
16
  };
14
- const dirtyLeafs = new bobeShared.SortMap();
15
17
  var State = /* @__PURE__ */ ((State2) => {
16
18
  State2[State2["Clean"] = 0] = "Clean";
17
- State2[State2["LinkScopeOnly"] = 64] = "LinkScopeOnly";
18
- State2[State2["ScopeAbort"] = 32] = "ScopeAbort";
19
- State2[State2["ScopeReady"] = 16] = "ScopeReady";
20
- State2[State2["IsScope"] = 8] = "IsScope";
19
+ State2[State2["LinkScopeOnly"] = 256] = "LinkScopeOnly";
20
+ State2[State2["ScopeAbort"] = 128] = "ScopeAbort";
21
+ State2[State2["ScopeReady"] = 64] = "ScopeReady";
22
+ State2[State2["IsScope"] = 32] = "IsScope";
23
+ State2[State2["PullingDirty"] = 16] = "PullingDirty";
24
+ State2[State2["PullingUnknown"] = 8] = "PullingUnknown";
21
25
  State2[State2["Unknown"] = 4] = "Unknown";
22
26
  State2[State2["Dirty"] = 2] = "Dirty";
23
- State2[State2["Check"] = 1] = "Check";
27
+ State2[State2["Pulling"] = 1] = "Pulling";
24
28
  return State2;
25
29
  })(State || {});
26
30
  const DirtyState = 4 /* Unknown */ | 2 /* Dirty */;
27
- const ScopeExecuted = 16 /* ScopeReady */ | 32 /* ScopeAbort */;
28
- const ScopeAbort = 32 /* ScopeAbort */;
31
+ const ScopeExecuted = 64 /* ScopeReady */ | 128 /* ScopeAbort */;
32
+ const ScopeAbort = 128 /* ScopeAbort */;
29
33
 
30
34
  const leakI = (y, max) => y < 0 || y >= max ? null : y;
31
35
  const getLeft = (x, max) => leakI(x * 2 + 1, max);
@@ -130,6 +134,26 @@ class PriorityQueue {
130
134
  }
131
135
  }
132
136
 
137
+ var __defProp$3 = Object.defineProperty;
138
+ var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
139
+ var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
140
+ var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
141
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
142
+ var __spreadValues$3 = (a, b) => {
143
+ for (var prop in b || (b = {}))
144
+ if (__hasOwnProp$4.call(b, prop))
145
+ __defNormalProp$3(a, prop, b[prop]);
146
+ if (__getOwnPropSymbols$4)
147
+ for (var prop of __getOwnPropSymbols$4(b)) {
148
+ if (__propIsEnum$4.call(b, prop))
149
+ __defNormalProp$3(a, prop, b[prop]);
150
+ }
151
+ return a;
152
+ };
153
+ const DefaultTaskControlReturn = {
154
+ finished: true,
155
+ startNewCallbackAble: true
156
+ };
133
157
  class TaskQueue {
134
158
  constructor(callbackAble, aIsUrgent) {
135
159
  this.callbackAble = callbackAble;
@@ -153,21 +177,30 @@ class TaskQueue {
153
177
  const { taskQueue } = this;
154
178
  const fn = taskQueue.peek();
155
179
  if (!fn) return this.isScheduling = false;
156
- const hasRemain = fn();
157
- if (hasRemain) {
158
- this.callbackAble(this.scheduleTask.bind(this));
159
- return;
180
+ let info = fn() || {};
181
+ info = __spreadValues$3(__spreadValues$3({}, DefaultTaskControlReturn), info);
182
+ if (info.finished) {
183
+ taskQueue.poll();
184
+ if (taskQueue.size() === 0) {
185
+ return this.isScheduling = false;
186
+ }
160
187
  }
161
- taskQueue.poll();
162
- evt.emit("one", fn);
163
- if (taskQueue.size() === 0) {
164
- evt.emit("done", fn);
165
- return this.isScheduling = false;
188
+ if (info.startNewCallbackAble) {
189
+ this.callbackAble(this.scheduleTask.bind(this));
190
+ } else {
191
+ this.scheduleTask();
166
192
  }
167
- this.callbackAble(this.scheduleTask.bind(this));
168
193
  }
169
194
  }
170
195
 
196
+ var Keys = /* @__PURE__ */ ((Keys2) => {
197
+ Keys2["Iterator"] = "__AOYE_ITERATOR";
198
+ Keys2["Raw"] = "__AOYE_RAW";
199
+ Keys2["Deep"] = "__AOYE_DEEP";
200
+ Keys2["Scope"] = "__AOYE_SCOPE";
201
+ return Keys2;
202
+ })(Keys || {});
203
+
171
204
  let channel = globalThis.MessageChannel ? new MessageChannel() : null;
172
205
  if (globalThis.MessageChannel) {
173
206
  channel = new MessageChannel();
@@ -191,55 +224,121 @@ const p = Promise.resolve();
191
224
  const micro = (cb) => {
192
225
  p.then(cb);
193
226
  };
194
-
195
- var Scheduler = /* @__PURE__ */ ((Scheduler2) => {
196
- Scheduler2["Sync"] = "__Sync_";
197
- Scheduler2["Layout"] = "__Layout_";
198
- Scheduler2["Micro"] = "__Micro_";
199
- Scheduler2["Macro"] = "__Macro_";
200
- return Scheduler2;
201
- })(Scheduler || {});
202
- const _scheduler = {
203
- ["__Sync_" /* Sync */]: defaultScheduler,
204
- ["__Micro_" /* Micro */]: microScheduler,
205
- ["__Macro_" /* Macro */]: macroScheduler,
206
- ["__Layout_" /* Layout */]: layoutScheduler
227
+ const toRaw = (a) => {
228
+ if (typeof a === "object" && a !== null && a[Keys.Raw]) {
229
+ return toRaw(a[Keys.Raw]);
230
+ }
231
+ return a;
207
232
  };
208
- const scheduler = (key, value) => _scheduler[key] = value;
209
- function defaultScheduler(effects) {
210
- for (const effect of effects) {
211
- effect.runIfDirty();
233
+
234
+ class Scheduler {
235
+ constructor() {
236
+ this.effectQueue = new bobeShared.Queue();
237
+ /** 每当 Set 或 BatchSet 开始时标记 */
238
+ this.firstEffectItem = null;
239
+ /** 记录 Set 或 BatchSet 产生的最后一个 Effect */
240
+ this.lastEffectItem = null;
241
+ }
242
+ endSet() {
243
+ var _a;
244
+ if (!this.firstEffectItem) return;
245
+ const subQueue = this.effectQueue.subRef(this.firstEffectItem, this.lastEffectItem);
246
+ (_a = this.onOneSetEffectsAdded) == null ? void 0 : _a.call(this, subQueue, this.effectQueue);
247
+ this.firstEffectItem = null;
248
+ this.lastEffectItem = null;
249
+ }
250
+ addEffect(effect) {
251
+ var _a;
252
+ const item = this.effectQueue.push(effect);
253
+ (_a = this.onEffectAdded) == null ? void 0 : _a.call(this, effect, item, this.effectQueue);
254
+ return item;
212
255
  }
213
256
  }
214
- let microSTaskQueue;
215
- let macroSTaskQueue;
216
- let layoutSTaskQueue;
217
- function microScheduler(effects) {
218
- microSTaskQueue = microSTaskQueue || TaskQueue.create({ callbackAble: micro, aIsUrgent: (a, b) => a.time < b.time });
219
- microSTaskQueue.pushTask(defaultScheduler.bind(null, effects));
257
+ Scheduler.Sync = "__Sync_";
258
+ Scheduler.Layout = "__Layout_";
259
+ Scheduler.Micro = "__Micro_";
260
+ Scheduler.Macro = "__Macro_";
261
+ class SyncScheduler extends Scheduler {
262
+ onOneSetEffectsAdded(subQueue, queue) {
263
+ subQueue.forEach((effect, item) => {
264
+ effect.runIfDirty();
265
+ subQueue.delete(item);
266
+ });
267
+ }
220
268
  }
221
- function macroScheduler(effects) {
222
- macroSTaskQueue = macroSTaskQueue || TaskQueue.create({ callbackAble: macro, aIsUrgent: (a, b) => a.time < b.time });
223
- macroSTaskQueue.pushTask(defaultScheduler.bind(null, effects));
269
+ class MicroScheduler extends Scheduler {
270
+ constructor() {
271
+ super(...arguments);
272
+ this.taskQueue = TaskQueue.create({ callbackAble: micro, aIsUrgent: (a, b) => a.time < b.time });
273
+ }
274
+ onOneSetEffectsAdded(subQueue, queue) {
275
+ const task = () => {
276
+ subQueue.forEach((effect, item) => {
277
+ effect.runIfDirty();
278
+ subQueue.delete(item);
279
+ });
280
+ return {
281
+ finished: true,
282
+ startNewCallbackAble: false
283
+ };
284
+ };
285
+ task.time = Date.now();
286
+ this.taskQueue.pushTask(task);
287
+ }
288
+ }
289
+ class MacroScheduler extends Scheduler {
290
+ constructor() {
291
+ super(...arguments);
292
+ this.taskQueue = TaskQueue.create({ callbackAble: macro, aIsUrgent: (a, b) => a.time < b.time });
293
+ }
294
+ onOneSetEffectsAdded(subQueue, queue) {
295
+ const task = () => {
296
+ subQueue.forEach((effect, item) => {
297
+ effect.runIfDirty();
298
+ subQueue.delete(item);
299
+ });
300
+ };
301
+ task.time = Date.now();
302
+ this.taskQueue.pushTask(task);
303
+ }
224
304
  }
225
- function layoutScheduler(effects) {
226
- layoutSTaskQueue = layoutSTaskQueue || TaskQueue.create({ callbackAble: macro, aIsUrgent: (a, b) => a.time < b.time });
227
- layoutSTaskQueue.pushTask(defaultScheduler.bind(null, effects));
305
+ class LayoutScheduler extends Scheduler {
306
+ constructor() {
307
+ super(...arguments);
308
+ this.taskQueue = TaskQueue.create({ callbackAble: macro, aIsUrgent: (a, b) => a.time < b.time });
309
+ }
310
+ onOneSetEffectsAdded(subQueue, queue) {
311
+ const task = () => {
312
+ subQueue.forEach((effect, item) => {
313
+ effect.runIfDirty();
314
+ subQueue.delete(item);
315
+ });
316
+ };
317
+ task.time = Date.now();
318
+ this.taskQueue.pushTask(task);
319
+ }
228
320
  }
321
+ const _scheduler = {
322
+ [Scheduler.Sync]: new SyncScheduler(),
323
+ [Scheduler.Micro]: new MicroScheduler(),
324
+ [Scheduler.Macro]: new MacroScheduler(),
325
+ [Scheduler.Layout]: new LayoutScheduler()
326
+ };
327
+ const registerScheduler = (key, Ctor) => _scheduler[key] = new Ctor();
229
328
 
230
- var __defProp$1 = Object.defineProperty;
231
- var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
232
- var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
233
- var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
234
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
235
- var __spreadValues$1 = (a, b) => {
329
+ var __defProp$2 = Object.defineProperty;
330
+ var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
331
+ var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
332
+ var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
333
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
334
+ var __spreadValues$2 = (a, b) => {
236
335
  for (var prop in b || (b = {}))
237
- if (__hasOwnProp$2.call(b, prop))
238
- __defNormalProp$1(a, prop, b[prop]);
239
- if (__getOwnPropSymbols$2)
240
- for (var prop of __getOwnPropSymbols$2(b)) {
241
- if (__propIsEnum$2.call(b, prop))
242
- __defNormalProp$1(a, prop, b[prop]);
336
+ if (__hasOwnProp$3.call(b, prop))
337
+ __defNormalProp$2(a, prop, b[prop]);
338
+ if (__getOwnPropSymbols$3)
339
+ for (var prop of __getOwnPropSymbols$3(b)) {
340
+ if (__propIsEnum$3.call(b, prop))
341
+ __defNormalProp$2(a, prop, b[prop]);
243
342
  }
244
343
  return a;
245
344
  };
@@ -252,7 +351,7 @@ const DefaultDFSOpt = {
252
351
  breakNode: null
253
352
  };
254
353
  function dfs(root, opt = {}) {
255
- const { isUp, begin, complete, breakStack: lineStack, breakLine } = __spreadValues$1(__spreadValues$1({}, DefaultDFSOpt), opt);
354
+ const { isUp, begin, complete, breakStack: lineStack, breakLine } = __spreadValues$2(__spreadValues$2({}, DefaultDFSOpt), opt);
256
355
  let node = opt.breakNode || root;
257
356
  let line = breakLine;
258
357
  const listKey = isUp ? "recStart" : "emitStart";
@@ -538,52 +637,89 @@ function clean(cb) {
538
637
  function runWithPulling(fn, signal) {
539
638
  const prevPulling = G.PullingSignal;
540
639
  G.PullingSignal = signal;
541
- fn();
640
+ const res = fn();
542
641
  G.PullingSignal = prevPulling;
642
+ return res;
543
643
  }
544
644
 
545
- var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
546
- var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
547
- var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
645
+ class Batch {
646
+ constructor() {
647
+ this.deep = 0;
648
+ }
649
+ start() {
650
+ this.deep++;
651
+ }
652
+ end() {
653
+ this.deep--;
654
+ if (this.deep !== 0) return;
655
+ for (const key in _scheduler) {
656
+ const instance = _scheduler[key];
657
+ instance.endSet();
658
+ }
659
+ }
660
+ inBatch() {
661
+ return this.deep > 0;
662
+ }
663
+ }
664
+ const batch = new Batch();
665
+
666
+ var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
667
+ var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
668
+ var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
548
669
  var __objRest = (source, exclude) => {
549
670
  var target = {};
550
671
  for (var prop in source)
551
- if (__hasOwnProp$1.call(source, prop) && exclude.indexOf(prop) < 0)
672
+ if (__hasOwnProp$2.call(source, prop) && exclude.indexOf(prop) < 0)
552
673
  target[prop] = source[prop];
553
- if (source != null && __getOwnPropSymbols$1)
554
- for (var prop of __getOwnPropSymbols$1(source)) {
555
- if (exclude.indexOf(prop) < 0 && __propIsEnum$1.call(source, prop))
674
+ if (source != null && __getOwnPropSymbols$2)
675
+ for (var prop of __getOwnPropSymbols$2(source)) {
676
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$2.call(source, prop))
556
677
  target[prop] = source[prop];
557
678
  }
558
679
  return target;
559
680
  };
560
681
  const markDeep = (signal) => {
561
- let level = 0;
682
+ let level = 0, updatedSchedulers = /* @__PURE__ */ new Set();
562
683
  dfs(signal, {
563
684
  isUp: false,
564
685
  begin: ({ node }) => {
565
- if (node.state & (State.Check | State.Unknown | State.Dirty) || node.isDisabled()) {
686
+ if (node.isDisabled()) {
566
687
  return true;
567
688
  }
689
+ const inPullingArea = node.state & State.Pulling;
690
+ const Unknown = inPullingArea ? State.PullingUnknown : State.Unknown;
568
691
  const isEffect = level > 0;
569
692
  const isLeaf = !node.emitStart || node.emitStart.downstream === node.scope;
570
693
  if (isEffect) {
571
- node.state |= State.Unknown;
694
+ node.state |= Unknown;
572
695
  } else if (!isLeaf) {
573
696
  node.state |= State.Dirty;
574
697
  }
575
- if (isLeaf && isEffect) {
576
- dirtyLeafs.add(node.scheduler, node);
698
+ if (isLeaf && isEffect && !inPullingArea) {
699
+ const key = node.scheduler;
700
+ const instance = _scheduler[key];
701
+ const item = instance.addEffect(node);
702
+ if (!instance.firstEffectItem) {
703
+ instance.firstEffectItem = item;
704
+ }
705
+ instance.lastEffectItem = item;
706
+ updatedSchedulers.add(key);
577
707
  }
578
708
  level++;
579
709
  }
580
710
  });
581
- for (const key in dirtyLeafs.data) {
582
- const effects = dirtyLeafs.data[key];
583
- const scheduler = _scheduler[key];
584
- scheduler(effects);
711
+ if (batch.inBatch()) return;
712
+ for (const key in _scheduler) {
713
+ const instance = _scheduler[key];
714
+ instance.endSet();
715
+ }
716
+ };
717
+ const pullingPostprocess = (node) => {
718
+ node.state &= ~State.Pulling;
719
+ if (node.state & State.PullingUnknown) {
720
+ node.state &= ~State.PullingUnknown;
721
+ node.state |= State.Unknown;
585
722
  }
586
- dirtyLeafs.clear();
587
723
  };
588
724
  const _Signal = class _Signal {
589
725
  constructor(nextValue, customPull) {
@@ -606,13 +742,16 @@ const _Signal = class _Signal {
606
742
  this.clean = null;
607
743
  }
608
744
  static create(nextValue, _a) {
609
- var _b = _a, { customPull, isScope, immediate } = _b, rest = __objRest(_b, ["customPull", "isScope", "immediate"]);
745
+ var _b = _a, { customPull, isScope, scope, immediate } = _b, rest = __objRest(_b, ["customPull", "isScope", "scope", "immediate"]);
610
746
  const s = new _Signal(nextValue, customPull);
611
747
  s.pull = s.customPull || s.DEFAULT_PULL;
612
748
  Object.assign(s, rest);
613
749
  if (isScope) {
614
750
  s.state |= State.IsScope;
615
751
  }
752
+ if (scope !== void 0) {
753
+ s.scope = scope;
754
+ }
616
755
  return s;
617
756
  }
618
757
  DEFAULT_PULL() {
@@ -623,6 +762,7 @@ const _Signal = class _Signal {
623
762
  */
624
763
  pullRecurse(shouldLink = true) {
625
764
  var _a, _b;
765
+ G.PullingRecurseDeep++;
626
766
  let downstream = G.PullingSignal;
627
767
  const isScope = this.state & State.IsScope;
628
768
  if (
@@ -638,6 +778,7 @@ const _Signal = class _Signal {
638
778
  return this.value;
639
779
  }
640
780
  this.recEnd = null;
781
+ this.state |= State.Pulling;
641
782
  G.PullingSignal = this;
642
783
  (_a = this.clean) == null ? void 0 : _a.call(this);
643
784
  this.clean = null;
@@ -655,9 +796,11 @@ const _Signal = class _Signal {
655
796
  console.error("\u8BA1\u7B97\u5C5E\u6027\u62A5\u9519\u8FD9\u6B21\u4E0D\u89E6\u53D1\uFF0C\u540E\u7EED\u72B6\u6001\u53EF\u80FD\u51FA\u9519", error);
656
797
  return this.value;
657
798
  } finally {
799
+ pullingPostprocess(this);
658
800
  const toDel = (_b = this.recEnd) == null ? void 0 : _b.nextRecLine;
659
801
  unlinkRecWithScope(toDel);
660
802
  G.PullingSignal = downstream;
803
+ G.PullingRecurseDeep--;
661
804
  }
662
805
  }
663
806
  pullDeep() {
@@ -668,10 +811,10 @@ const _Signal = class _Signal {
668
811
  dfs(signal, {
669
812
  isUp: true,
670
813
  begin: ({ node }) => {
671
- if (node.state & State.Check || !(node.state & DirtyState) || node.isDisabled()) {
814
+ if (node.state & State.Pulling || !(node.state & DirtyState) || node.isDisabled()) {
672
815
  return true;
673
816
  }
674
- node.state |= State.Check;
817
+ node.state |= State.Pulling;
675
818
  },
676
819
  complete: ({ node, notGoDeep: currentClean, walkedLine }) => {
677
820
  let noGoSibling = false;
@@ -681,7 +824,7 @@ const _Signal = class _Signal {
681
824
  if (!node.recStart && node.value !== node.nextValue) {
682
825
  node.markDownStreamsDirty();
683
826
  node.state &= ~State.Dirty;
684
- node.state &= ~State.Check;
827
+ node.state &= ~State.Pulling;
685
828
  return;
686
829
  } else {
687
830
  const prevPulling = G.PullingSignal;
@@ -699,7 +842,7 @@ const _Signal = class _Signal {
699
842
  node.state &= ~State.Unknown;
700
843
  }
701
844
  node.version = G.version;
702
- node.state &= ~State.Check;
845
+ pullingPostprocess(node);
703
846
  return noGoSibling;
704
847
  }
705
848
  });
@@ -738,6 +881,7 @@ const _Signal = class _Signal {
738
881
  G.version++;
739
882
  markDeep(this);
740
883
  }
884
+ /** 返回值为 true 表示已处理 */
741
885
  runIfDirty() {
742
886
  this.state & (State.Unknown | State.Dirty) && this.v;
743
887
  }
@@ -752,6 +896,396 @@ const _Signal = class _Signal {
752
896
  _Signal.Pulling = null;
753
897
  let Signal = _Signal;
754
898
 
899
+ var __defProp$1 = Object.defineProperty;
900
+ var __defProps = Object.defineProperties;
901
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
902
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
903
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
904
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
905
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
906
+ var __spreadValues$1 = (a, b) => {
907
+ for (var prop in b || (b = {}))
908
+ if (__hasOwnProp$1.call(b, prop))
909
+ __defNormalProp$1(a, prop, b[prop]);
910
+ if (__getOwnPropSymbols$1)
911
+ for (var prop of __getOwnPropSymbols$1(b)) {
912
+ if (__propIsEnum$1.call(b, prop))
913
+ __defNormalProp$1(a, prop, b[prop]);
914
+ }
915
+ return a;
916
+ };
917
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
918
+ const deepSignal = (target, scope, deep = true) => {
919
+ const isObj = typeof target === "object" && target !== null;
920
+ if (!isObj || target[Keys.Raw]) return target;
921
+ if (rawToProxy.has(target)) return rawToProxy.get(target);
922
+ const cells = /* @__PURE__ */ new Map();
923
+ const targetIsArray = Array.isArray(target);
924
+ const proxy = new Proxy(target, {
925
+ get(obj, prop, receiver) {
926
+ switch (prop) {
927
+ case Keys.Raw:
928
+ return target;
929
+ case Keys.Deep:
930
+ return deep;
931
+ case Keys.Scope:
932
+ return scope;
933
+ }
934
+ const value = Reflect.get(obj, prop, receiver);
935
+ const valueIsFn = typeof value === "function";
936
+ if (valueIsFn) {
937
+ if (targetIsArray) {
938
+ return arrayMethodReWrites[prop] || value;
939
+ } else {
940
+ return value;
941
+ }
942
+ }
943
+ if (cells.has(prop)) {
944
+ return cells.get(prop).v;
945
+ }
946
+ const wrappedValue = deep ? deepSignal(value, scope) : value;
947
+ const s = Signal.create(wrappedValue, {
948
+ scheduler: Scheduler.Sync,
949
+ isScope: false,
950
+ scope
951
+ });
952
+ cells.set(prop, s);
953
+ return s.v;
954
+ },
955
+ set(obj, prop, value, receiver) {
956
+ batch.start();
957
+ const success = Reflect.set(obj, prop, value, receiver);
958
+ if (cells.has(prop)) {
959
+ const cell = cells.get(prop);
960
+ cell.v = deep ? deepSignal(value, scope) : value;
961
+ }
962
+ if (targetIsArray) {
963
+ handleArraySet(obj, prop, value, receiver);
964
+ }
965
+ batch.end();
966
+ return success;
967
+ },
968
+ // 【核心修改】拦截 delete 操作
969
+ deleteProperty(obj, prop) {
970
+ if (cells.has(prop)) {
971
+ cells.delete(prop);
972
+ }
973
+ return Reflect.deleteProperty(obj, prop);
974
+ },
975
+ ownKeys(obj) {
976
+ if (targetIsArray) {
977
+ proxy.length;
978
+ } else {
979
+ proxy[Keys.Iterator];
980
+ }
981
+ return Reflect.ownKeys(obj);
982
+ }
983
+ });
984
+ rawToProxy.set(target, proxy);
985
+ return proxy;
986
+ };
987
+ function handleArraySet(arr, prop, value, receiver) {
988
+ if (prop === "length") ; else if (bobeShared.isNatureNumStr(prop)) {
989
+ receiver[Keys.Iterator] = (arr[Keys.Iterator] || 0) + 1;
990
+ } else ;
991
+ }
992
+ const arrayMethodReWrites = {};
993
+ ["pop", "push", "shift", "splice", "unshift", "copyWithin", "reverse", "fill"].forEach((key) => {
994
+ arrayMethodReWrites[key] = function(...args) {
995
+ batch.start();
996
+ const fn = Array.prototype[key];
997
+ const res = runWithPulling(() => fn.call(this, ...args), null);
998
+ this[Keys.Iterator] = (this[Keys.Raw][Keys.Iterator] || 0) + 1;
999
+ batch.end();
1000
+ return res;
1001
+ };
1002
+ });
1003
+ ["includes", "indexOf", "lastIndexOf"].forEach((key) => {
1004
+ arrayMethodReWrites[key] = function(...args) {
1005
+ const fn = Array.prototype[key];
1006
+ const that = toRaw(this);
1007
+ let result = fn.call(that, ...args);
1008
+ const value = args[0];
1009
+ if ((result === false || result === -1) && typeof value === "object" && value !== null) {
1010
+ if (value[Keys.Raw]) {
1011
+ args[0] = value[Keys.Raw];
1012
+ result = fn.call(that, ...args);
1013
+ }
1014
+ if (rawToProxy.has(value)) {
1015
+ args[0] = rawToProxy.get(value);
1016
+ result = fn.call(that, ...args);
1017
+ }
1018
+ }
1019
+ this[Keys.Iterator];
1020
+ return result;
1021
+ };
1022
+ });
1023
+ [Symbol.iterator, "values", "entries"].forEach((key) => {
1024
+ const isEntries = key === "entries";
1025
+ arrayMethodReWrites[key] = function(...args) {
1026
+ const fn = Array.prototype[key];
1027
+ const rawArray = toRaw(this);
1028
+ const iter = fn.call(rawArray, ...args);
1029
+ const scope = this[Keys.Scope];
1030
+ const isDeep = this[Keys.Deep];
1031
+ if (isDeep) {
1032
+ const rawNext = iter.next.bind(iter);
1033
+ iter.next = () => {
1034
+ const result = rawNext();
1035
+ if (!result.done) {
1036
+ if (isEntries) {
1037
+ result.value[1] = deepSignal(result.value[1], scope);
1038
+ } else {
1039
+ result.value = deepSignal(result.value, scope);
1040
+ }
1041
+ }
1042
+ return result;
1043
+ };
1044
+ }
1045
+ this[Keys.Iterator];
1046
+ return iter;
1047
+ };
1048
+ });
1049
+ arrayMethodReWrites.filter = function(callback, thisArg) {
1050
+ const scope = this[Keys.Scope];
1051
+ const isDeep = this[Keys.Deep];
1052
+ const that = toRaw(this);
1053
+ const result = [];
1054
+ let resultIndex = 0;
1055
+ const userThis = thisArg || that;
1056
+ const len = that.length;
1057
+ for (let i = 0; i < len; i++) {
1058
+ if (i in that) {
1059
+ const value = isDeep ? deepSignal(that[i], scope) : that[i];
1060
+ if (callback.call(userThis, value, i, userThis)) {
1061
+ result[resultIndex++] = value;
1062
+ }
1063
+ }
1064
+ }
1065
+ this[Keys.Iterator];
1066
+ return result;
1067
+ };
1068
+ arrayMethodReWrites.slice = function(start, end) {
1069
+ const scope = this[Keys.Scope];
1070
+ const isDeep = this[Keys.Deep];
1071
+ const that = toRaw(this);
1072
+ const len = that.length;
1073
+ let k = start || 0;
1074
+ if (k < 0) {
1075
+ k = Math.max(len + k, 0);
1076
+ } else {
1077
+ k = Math.min(k, len);
1078
+ }
1079
+ let final = end === void 0 ? len : end;
1080
+ if (final < 0) {
1081
+ final = Math.max(len + final, 0);
1082
+ } else {
1083
+ final = Math.min(final, len);
1084
+ }
1085
+ const count = Math.max(final - k, 0);
1086
+ const result = new Array(count);
1087
+ for (let i = 0; i < count; i++) {
1088
+ if (i + k in that) {
1089
+ result[i] = isDeep ? deepSignal(that[i + k], scope) : that[i + k];
1090
+ }
1091
+ }
1092
+ this[Keys.Iterator];
1093
+ return result;
1094
+ };
1095
+ arrayMethodReWrites.toReversed = function() {
1096
+ const scope = this[Keys.Scope];
1097
+ const isDeep = this[Keys.Deep];
1098
+ const that = toRaw(this);
1099
+ const len = that.length;
1100
+ const result = new Array(len);
1101
+ let k = 0;
1102
+ while (k < len) {
1103
+ result[k] = isDeep ? deepSignal(that[len - 1 - k], scope) : that[len - 1 - k];
1104
+ k++;
1105
+ }
1106
+ this[Keys.Iterator];
1107
+ return result;
1108
+ };
1109
+ arrayMethodReWrites.toSpliced = function(start, deleteCount, ...items) {
1110
+ const scope = this[Keys.Scope];
1111
+ const isDeep = this[Keys.Deep];
1112
+ const that = toRaw(this);
1113
+ const len = that.length;
1114
+ let relativeStart = start >> 0;
1115
+ let actualStart = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
1116
+ let actualDeleteCount;
1117
+ if (arguments.length === 0) {
1118
+ actualDeleteCount = 0;
1119
+ } else if (arguments.length === 1) {
1120
+ actualDeleteCount = len - actualStart;
1121
+ } else {
1122
+ let dc = deleteCount >> 0;
1123
+ actualDeleteCount = Math.min(Math.max(dc, 0), len - actualStart);
1124
+ }
1125
+ const insertCount = items.length;
1126
+ const newLen = len - actualDeleteCount + insertCount;
1127
+ const result = new Array(newLen);
1128
+ for (let i = 0; i < actualStart; i++) {
1129
+ result[i] = isDeep ? deepSignal(that[i], scope) : that[i];
1130
+ }
1131
+ for (let i = 0; i < insertCount; i++) {
1132
+ result[actualStart + i] = isDeep ? deepSignal(items[i], scope) : items[i];
1133
+ }
1134
+ const remainingStart = actualStart + actualDeleteCount;
1135
+ const resultOffset = actualStart + insertCount;
1136
+ for (let i = 0; i < len - remainingStart; i++) {
1137
+ result[resultOffset + i] = isDeep ? deepSignal(that[remainingStart + i], scope) : that[remainingStart + i];
1138
+ }
1139
+ this[Keys.Iterator];
1140
+ return result;
1141
+ };
1142
+ arrayMethodReWrites.with = function(index, value) {
1143
+ const scope = this[Keys.Scope];
1144
+ const isDeep = this[Keys.Deep];
1145
+ const that = toRaw(this);
1146
+ const len = that.length;
1147
+ let relativeIndex = Number(index) || 0;
1148
+ let actualIndex = relativeIndex >= 0 ? relativeIndex : len + relativeIndex;
1149
+ if (actualIndex >= len || actualIndex < 0) {
1150
+ throw new RangeError(`Invalid index: ${index}`);
1151
+ }
1152
+ const result = new Array(len);
1153
+ for (let i = 0; i < len; i++) {
1154
+ if (i === actualIndex) {
1155
+ result[i] = isDeep ? deepSignal(value, scope) : value;
1156
+ } else {
1157
+ result[i] = isDeep ? deepSignal(that[i], scope) : that[i];
1158
+ }
1159
+ }
1160
+ this[Keys.Iterator];
1161
+ return result;
1162
+ };
1163
+ arrayMethodReWrites.concat = function(...items) {
1164
+ const scope = this[Keys.Scope];
1165
+ const isDeep = this[Keys.Deep];
1166
+ const that = toRaw(this);
1167
+ const selfLen = that.length;
1168
+ let totalLength = selfLen;
1169
+ for (let i = 0; i < items.length; i++) {
1170
+ const item = items[i];
1171
+ if (Array.isArray(item)) {
1172
+ totalLength += item.length;
1173
+ } else {
1174
+ totalLength += 1;
1175
+ }
1176
+ }
1177
+ const result = new Array(totalLength);
1178
+ let k = 0;
1179
+ for (; k < selfLen; k++) {
1180
+ if (k in that) {
1181
+ result[k] = isDeep ? deepSignal(that[k], scope) : that[k];
1182
+ }
1183
+ }
1184
+ for (let i = 0; i < items.length; i++) {
1185
+ const item = items[i];
1186
+ if (Array.isArray(item)) {
1187
+ for (let j = 0; j < item.length; j++) {
1188
+ if (j in item) {
1189
+ result[k] = isDeep ? deepSignal(item[j], scope) : item[j];
1190
+ }
1191
+ k++;
1192
+ }
1193
+ } else {
1194
+ result[k] = isDeep ? deepSignal(item, scope) : item;
1195
+ k++;
1196
+ }
1197
+ }
1198
+ this[Keys.Iterator];
1199
+ return result;
1200
+ };
1201
+ const GetMethodConf = {
1202
+ wrapReturn: false,
1203
+ wrapArgs: 1
1204
+ };
1205
+ [
1206
+ __spreadValues$1({
1207
+ key: "every"
1208
+ }, GetMethodConf),
1209
+ __spreadValues$1({
1210
+ key: "find",
1211
+ wrapReturn: true
1212
+ }, GetMethodConf),
1213
+ __spreadProps(__spreadValues$1({
1214
+ key: "findLast"
1215
+ }, GetMethodConf), {
1216
+ wrapReturn: true
1217
+ }),
1218
+ __spreadValues$1({
1219
+ key: "findIndex"
1220
+ }, GetMethodConf),
1221
+ __spreadValues$1({
1222
+ key: "findLastIndex"
1223
+ }, GetMethodConf),
1224
+ __spreadValues$1({
1225
+ key: "forEach"
1226
+ }, GetMethodConf),
1227
+ __spreadValues$1({
1228
+ key: "map"
1229
+ }, GetMethodConf),
1230
+ __spreadValues$1({
1231
+ key: "some"
1232
+ }, GetMethodConf),
1233
+ __spreadProps(__spreadValues$1({
1234
+ key: "reduce"
1235
+ }, GetMethodConf), {
1236
+ wrapArgs: 2
1237
+ }),
1238
+ __spreadProps(__spreadValues$1({
1239
+ key: "reduceRight"
1240
+ }, GetMethodConf), {
1241
+ wrapArgs: 2
1242
+ })
1243
+ ].forEach(({ key, wrapReturn, wrapArgs }) => {
1244
+ arrayMethodReWrites[key] = function(...args) {
1245
+ const scope = this[Keys.Scope];
1246
+ const fn = Array.prototype[key];
1247
+ const isDeep = this[Keys.Deep];
1248
+ const that = toRaw(this);
1249
+ warpCallbackArgs(isDeep, args, scope, wrapArgs);
1250
+ let result = fn.call(that, ...args);
1251
+ if (wrapReturn && isDeep) {
1252
+ result = deepSignal(result, scope);
1253
+ }
1254
+ this[Keys.Iterator];
1255
+ return result;
1256
+ };
1257
+ });
1258
+ arrayMethodReWrites.toSorted = function(...args) {
1259
+ const fn = Array.prototype["toSorted"];
1260
+ const scope = this[Keys.Scope];
1261
+ const isDeep = this[Keys.Deep];
1262
+ const that = toRaw(this);
1263
+ warpCallbackArgs(isDeep, args, scope, 3);
1264
+ let result = fn.call(that, ...args);
1265
+ this[Keys.Iterator];
1266
+ return isDeep ? result.map((it) => deepSignal(it, scope)) : result;
1267
+ };
1268
+ ["join", "toString", "toLocaleString"].forEach((key) => {
1269
+ arrayMethodReWrites[key] = function(...args) {
1270
+ const fn = Array.prototype[key];
1271
+ const that = toRaw(this);
1272
+ const result = fn.call(that, ...args);
1273
+ this[Keys.Iterator];
1274
+ return result;
1275
+ };
1276
+ });
1277
+ function warpCallbackArgs(isDeep, args, scope, wrapArgs = 1) {
1278
+ const callback = args[0];
1279
+ const wrapCb = function(...cbArgs) {
1280
+ if (isDeep) {
1281
+ if (wrapArgs & 1) cbArgs[0] = deepSignal(cbArgs[0], scope);
1282
+ if (wrapArgs & 2) cbArgs[1] = deepSignal(cbArgs[1], scope);
1283
+ }
1284
+ return callback.call(this, ...cbArgs);
1285
+ };
1286
+ args[0] = wrapCb;
1287
+ }
1288
+
755
1289
  var __defProp = Object.defineProperty;
756
1290
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
757
1291
  var __hasOwnProp = Object.prototype.hasOwnProperty;
@@ -768,13 +1302,26 @@ var __spreadValues = (a, b) => {
768
1302
  }
769
1303
  return a;
770
1304
  };
1305
+ const DefaultCustomSignalOpt = {
1306
+ /** 三种模式
1307
+ * 1. auto: 根据值类型自动判断 默认
1308
+ * 2. ref: 对任何值使用 {v: xxx} 进行包装
1309
+ * 3. proxy: 使用 proxy 进行包装
1310
+ */
1311
+ mode: "auto",
1312
+ /** 是否深度响应式 */
1313
+ deep: true
1314
+ };
771
1315
  ({
772
1316
  scheduler: Scheduler.Sync});
773
- const $ = (init) => {
1317
+ const $ = (init, opt = {}) => {
1318
+ opt = __spreadValues(__spreadValues({}, DefaultCustomSignalOpt), opt);
774
1319
  let intiValue, customPull;
775
- if (init instanceof Function) {
1320
+ if (typeof init === "function") {
776
1321
  intiValue = null;
777
1322
  customPull = init;
1323
+ } else if (opt.mode !== "ref" && typeof init === "object" && init !== null) {
1324
+ return deepSignal(init, G.PullingSignal, opt.deep);
778
1325
  } else {
779
1326
  intiValue = init;
780
1327
  }
@@ -843,14 +1390,24 @@ const customEffect = (opt) => {
843
1390
  return effect(init, __spreadValues(__spreadValues({}, opt), innerOpt));
844
1391
  });
845
1392
  };
1393
+ const isSignal = (value) => {
1394
+ return value instanceof Signal;
1395
+ };
1396
+ const isScope = (value) => {
1397
+ return value instanceof Signal;
1398
+ };
846
1399
 
847
1400
  exports.$ = $;
1401
+ exports.Keys = Keys;
848
1402
  exports.Scheduler = Scheduler;
849
1403
  exports.TaskQueue = TaskQueue;
1404
+ exports.batch = batch;
850
1405
  exports.clean = clean;
851
1406
  exports.customEffect = customEffect;
852
1407
  exports.effect = effect;
1408
+ exports.isScope = isScope;
1409
+ exports.isSignal = isSignal;
1410
+ exports.registerScheduler = registerScheduler;
853
1411
  exports.runWithPulling = runWithPulling;
854
- exports.scheduler = scheduler;
855
1412
  exports.scope = scope;
856
1413
  //# sourceMappingURL=aoye.cjs.js.map