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