aoye 0.0.43 → 0.0.45

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
@@ -18,12 +18,120 @@ function runWithPulling(fn, scope) {
18
18
  return ret;
19
19
  }
20
20
 
21
+ let ScheduleType = function (ScheduleType) {
22
+ ScheduleType[ScheduleType["Sync"] = 1] = "Sync";
23
+ ScheduleType[ScheduleType["Pre"] = 2] = "Pre";
24
+ ScheduleType[ScheduleType["Render"] = 4] = "Render";
25
+ ScheduleType[ScheduleType["Post"] = 8] = "Post";
26
+ return ScheduleType;
27
+ }({});
28
+
29
+ class MultiScheduler {
30
+ hasTask = 0;
31
+ taskMap = {};
32
+ constructor(queueCount) {
33
+ for (let i = 0; i < queueCount; i++) {
34
+ this.taskMap[1 << i] = {
35
+ head: null,
36
+ tail: null
37
+ };
38
+ }
39
+ }
40
+ addTask(queueId, task) {
41
+ const queue = this.taskMap[queueId];
42
+ const tail = queue.tail;
43
+ const item = {
44
+ value: task,
45
+ next: null
46
+ };
47
+ if (tail) {
48
+ tail.next = item;
49
+ } else {
50
+ queue.head = item;
51
+ }
52
+ queue.tail = item;
53
+ this.hasTask |= queueId;
54
+ }
55
+ flushAllTask() {
56
+ while (this.hasTask) {
57
+ const hasTask = this.hasTask;
58
+ const highest = hasTask & ~hasTask + 1;
59
+ const task = this.consumeTask(highest).value;
60
+ task.get();
61
+ }
62
+ }
63
+ consumeTask(queueId) {
64
+ const queue = this.taskMap[queueId];
65
+ const head = queue.head,
66
+ tail = queue.tail;
67
+ const next = head.next;
68
+ head.next = null;
69
+ if (head === tail) {
70
+ queue.head = null;
71
+ queue.tail = null;
72
+ this.hasTask &= ~queueId;
73
+ } else {
74
+ head.next = null;
75
+ queue.head = next;
76
+ }
77
+ return head;
78
+ }
79
+ }
80
+ const multiScheduler = new MultiScheduler(4);
81
+
82
+ let Keys = function (Keys) {
83
+ Keys["Iterator"] = "__AOYE_ITERATOR";
84
+ Keys["Raw"] = "__AOYE_RAW";
85
+ Keys["Meta"] = "__AOYE_META";
86
+ Keys["ProxyFreeObject"] = "__AOYE_PROXY_FREE_OBJECT";
87
+ return Keys;
88
+ }({});
89
+ const IsStore = Symbol('__AOYE_IS_STORE'),
90
+ StoreIgnoreKeys = Symbol('__AOYE_IGNORE_KEYS');
91
+
92
+ const ide = globalThis.requestIdleCallback || (globalThis.requestAnimationFrame ? fn => globalThis.requestAnimationFrame(() => {
93
+ setTimeout(() => {
94
+ fn();
95
+ });
96
+ }) : globalThis.setTimeout);
97
+ const now = () => {
98
+ const timer = globalThis.performance || globalThis.Date;
99
+ return timer.now();
100
+ };
101
+ let channel = globalThis.MessageChannel ? new MessageChannel() : null;
102
+ if (globalThis.MessageChannel) {
103
+ channel = new MessageChannel();
104
+ }
105
+ let msgId = 0;
106
+ const macro = fn => {
107
+ if (!channel) {
108
+ setTimeout(fn);
109
+ }
110
+ const memoId = msgId;
111
+ function onMessage(e) {
112
+ if (memoId === e.data) {
113
+ fn();
114
+ channel.port2.removeEventListener('message', onMessage);
115
+ }
116
+ }
117
+ channel.port2.addEventListener('message', onMessage);
118
+ channel.port1.postMessage(msgId++);
119
+ };
120
+ const p = Promise.resolve();
121
+ const micro = cb => {
122
+ p.then(cb);
123
+ };
124
+ const toRaw = a => {
125
+ if (typeof a === 'object' && a !== null && a[Keys.Raw]) {
126
+ return toRaw(a[Keys.Raw]);
127
+ }
128
+ return a;
129
+ };
130
+
21
131
  function mark(signal) {
22
132
  let line = signal.emitHead;
23
133
  while (line) {
24
- const _line = line,
25
- down = _line.down;
26
- _line.up;
134
+ const down = line.down;
27
135
  const scope = down.scope,
28
136
  emitHead = down.emitHead,
29
137
  state = down.state;
@@ -32,7 +140,11 @@ function mark(signal) {
32
140
  down.state |= notLocked ? 2 : 16;
33
141
  if (state & 32) {
34
142
  if (notLocked && state & 512) {
35
- addEffect(down);
143
+ if (down.type === ScheduleType.Sync) {
144
+ addEffect(down);
145
+ } else {
146
+ multiScheduler.addTask(down.type, down);
147
+ }
36
148
  }
37
149
  } else if (emitHead) {
38
150
  markUnknownDeep(emitHead);
@@ -49,9 +161,7 @@ function markUnknownDeep(initialLine) {
49
161
  let line = stack[--len];
50
162
  stack[len] = null;
51
163
  while (line) {
52
- const _line2 = line,
53
- down = _line2.down;
54
- _line2.up;
164
+ const down = line.down;
55
165
  const state = down.state,
56
166
  scope = down.scope;
57
167
  if (scope && scope.state & 128 || noPulling && state & 6) ; else {
@@ -59,7 +169,11 @@ function markUnknownDeep(initialLine) {
59
169
  down.state |= notLocked ? 4 : 8;
60
170
  if (state & 32) {
61
171
  if (notLocked && state & 512) {
62
- addEffect(down);
172
+ if (down.type === ScheduleType.Sync) {
173
+ addEffect(down);
174
+ } else {
175
+ multiScheduler.addTask(down.type, down);
176
+ }
63
177
  }
64
178
  } else if (down.emitHead) {
65
179
  stack[len++] = down.emitHead;
@@ -103,8 +217,8 @@ function pullDeep(root) {
103
217
  if (noGoSibling = value !== prevValue) {
104
218
  let line = node.emitHead;
105
219
  while (line) {
106
- const _line3 = line,
107
- down = _line3.down;
220
+ const _line = line,
221
+ down = _line.down;
108
222
  down.state &= -5;
109
223
  down.state |= 2;
110
224
  line = line.nextEmitLine;
@@ -148,7 +262,7 @@ let consumeI = -1,
148
262
  function addEffect(effect) {
149
263
  effectQueue[++produceI] = effect;
150
264
  }
151
- function flushEffect() {
265
+ function flushSyncEffect() {
152
266
  if (consumeI !== -1) {
153
267
  return;
154
268
  }
@@ -162,12 +276,20 @@ function flushEffect() {
162
276
  consumeI = -1;
163
277
  produceI = -1;
164
278
  }
279
+ function flushMicroEffect() {
280
+ if (multiScheduler.hasTask) {
281
+ micro(() => {
282
+ multiScheduler.flushAllTask();
283
+ });
284
+ }
285
+ }
165
286
  let _batchDeep = 0;
166
287
  const batchStart = () => _batchDeep++;
167
288
  const batchEnd = () => {
168
289
  _batchDeep--;
169
290
  if (_batchDeep === 0) {
170
- flushEffect();
291
+ flushSyncEffect();
292
+ flushMicroEffect();
171
293
  }
172
294
  };
173
295
  const batchDeep = () => _batchDeep;
@@ -495,7 +617,8 @@ class Signal {
495
617
  if (this.emitHead) {
496
618
  mark(this);
497
619
  if (batchDeep() === 0) {
498
- flushEffect();
620
+ flushSyncEffect();
621
+ flushMicroEffect();
499
622
  }
500
623
  }
501
624
  }
@@ -511,8 +634,9 @@ class Effect {
511
634
  scope = getPulling();
512
635
  outLink = null;
513
636
  clean = null;
514
- constructor(callback) {
637
+ constructor(callback, type = ScheduleType.Sync) {
515
638
  this.callback = callback;
639
+ this.type = type;
516
640
  this.get();
517
641
  }
518
642
  get(shouldLink = true, notForceUpdate = true) {
@@ -579,16 +703,6 @@ class Scope {
579
703
  }
580
704
  Scope.prototype.dispose = dispose;
581
705
 
582
- let Keys = function (Keys) {
583
- Keys["Iterator"] = "__AOYE_ITERATOR";
584
- Keys["Raw"] = "__AOYE_RAW";
585
- Keys["Meta"] = "__AOYE_META";
586
- Keys["ProxyFreeObject"] = "__AOYE_PROXY_FREE_OBJECT";
587
- return Keys;
588
- }({});
589
- const IsStore = Symbol('__AOYE_IS_STORE'),
590
- StoreIgnoreKeys = Symbol('__AOYE_IGNORE_KEYS');
591
-
592
706
  const rawToProxy = new WeakMap();
593
707
  let State = function (State) {
594
708
  State[State["Clean"] = 0] = "Clean";
@@ -607,45 +721,6 @@ State.Unknown | State.Dirty;
607
721
  State.ScopeReady | State.ScopeAbort;
608
722
  State.ScopeAbort;
609
723
 
610
- const ide = globalThis.requestIdleCallback || (globalThis.requestAnimationFrame ? fn => globalThis.requestAnimationFrame(() => {
611
- setTimeout(() => {
612
- fn();
613
- });
614
- }) : globalThis.setTimeout);
615
- const now = () => {
616
- const timer = globalThis.performance || globalThis.Date;
617
- return timer.now();
618
- };
619
- let channel = globalThis.MessageChannel ? new MessageChannel() : null;
620
- if (globalThis.MessageChannel) {
621
- channel = new MessageChannel();
622
- }
623
- let msgId = 0;
624
- const macro = fn => {
625
- if (!channel) {
626
- setTimeout(fn);
627
- }
628
- const memoId = msgId;
629
- function onMessage(e) {
630
- if (memoId === e.data) {
631
- fn();
632
- channel.port2.removeEventListener('message', onMessage);
633
- }
634
- }
635
- channel.port2.addEventListener('message', onMessage);
636
- channel.port1.postMessage(msgId++);
637
- };
638
- const p = Promise.resolve();
639
- const micro = cb => {
640
- p.then(cb);
641
- };
642
- const toRaw = a => {
643
- if (typeof a === 'object' && a !== null && a[Keys.Raw]) {
644
- return toRaw(a[Keys.Raw]);
645
- }
646
- return a;
647
- };
648
-
649
724
  const deepSignal = (target, scope, deep = true) => {
650
725
  const isObj = typeof target === 'object' && target !== null;
651
726
  if (!isObj || target[Keys.Raw] || target[Keys.ProxyFreeObject]) return target;
@@ -1171,11 +1246,14 @@ function $(data) {
1171
1246
  };
1172
1247
  }
1173
1248
  }
1249
+ ({
1250
+ type: ScheduleType.Sync
1251
+ });
1174
1252
  function effectUt(callback, depOrOpt, opt) {
1175
1253
  const hasDep = Array.isArray(depOrOpt);
1176
1254
  opt = hasDep ? opt || {} : depOrOpt || {};
1177
1255
  if (!hasDep) {
1178
- const ef = new Effect(callback);
1256
+ const ef = new Effect(callback, opt.type);
1179
1257
  const run = ef.dispose.bind(ef);
1180
1258
  run.ins = ef;
1181
1259
  return run;
@@ -1189,19 +1267,19 @@ function effectUt(callback, depOrOpt, opt) {
1189
1267
  old: null,
1190
1268
  val: null
1191
1269
  }));
1192
- const ef = new Effect(() => {
1270
+ const ef = new Effect(eff => {
1193
1271
  for (let i = 0; i < deps.length; i++) {
1194
1272
  const value = deps[i].v;
1195
1273
  vs[i].old = vs[i].val;
1196
1274
  vs[i].val = value;
1197
1275
  }
1198
1276
  if (mounted || immediate) {
1199
- ef.state |= 256;
1277
+ eff.state |= 256;
1200
1278
  callback(...vs);
1201
- ef.state &= -257;
1279
+ eff.state &= -257;
1202
1280
  }
1203
1281
  mounted = true;
1204
- });
1282
+ }, opt.type);
1205
1283
  const run = ef.dispose.bind(ef);
1206
1284
  run.ins = ef;
1207
1285
  return run;
@@ -1210,7 +1288,7 @@ function effect(callback, depOrOpt, opt) {
1210
1288
  const hasDep = Array.isArray(depOrOpt);
1211
1289
  opt = hasDep ? opt || {} : depOrOpt || {};
1212
1290
  if (!hasDep) {
1213
- const ef = new Effect(callback);
1291
+ const ef = new Effect(callback, opt.type);
1214
1292
  return ef;
1215
1293
  }
1216
1294
  let mounted = false;
@@ -1234,7 +1312,7 @@ function effect(callback, depOrOpt, opt) {
1234
1312
  eff.state &= -257;
1235
1313
  }
1236
1314
  mounted = true;
1237
- });
1315
+ }, opt.type);
1238
1316
  return ef;
1239
1317
  }
1240
1318
  function scope(...args) {
@@ -1253,6 +1331,7 @@ exports.Computed = Computed;
1253
1331
  exports.Effect = Effect;
1254
1332
  exports.IsStore = IsStore;
1255
1333
  exports.Keys = Keys;
1334
+ exports.ScheduleType = ScheduleType;
1256
1335
  exports.Scope = Scope;
1257
1336
  exports.Signal = Signal;
1258
1337
  exports.Store = Store;