aoye 0.0.44 → 0.0.46

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,126 @@ 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
+ let ScheduleStatus = function (ScheduleStatus) {
27
+ ScheduleStatus[ScheduleStatus["Idle"] = 0] = "Idle";
28
+ ScheduleStatus[ScheduleStatus["Ready"] = 1] = "Ready";
29
+ ScheduleStatus[ScheduleStatus["Running"] = 2] = "Running";
30
+ return ScheduleStatus;
31
+ }({});
32
+
33
+ class MultiScheduler {
34
+ hasTask = 0;
35
+ taskMap = {};
36
+ constructor(queueCount) {
37
+ for (let i = 0; i < queueCount; i++) {
38
+ this.taskMap[1 << i] = {
39
+ head: null,
40
+ tail: null
41
+ };
42
+ }
43
+ }
44
+ addTask(queueId, task) {
45
+ const queue = this.taskMap[queueId];
46
+ const tail = queue.tail;
47
+ const item = {
48
+ value: task,
49
+ next: null
50
+ };
51
+ if (tail) {
52
+ tail.next = item;
53
+ } else {
54
+ queue.head = item;
55
+ }
56
+ queue.tail = item;
57
+ this.hasTask |= queueId;
58
+ }
59
+ flushAllTask() {
60
+ while (this.hasTask) {
61
+ const hasTask = this.hasTask;
62
+ const highest = hasTask & ~hasTask + 1;
63
+ const task = this.consumeTask(highest).value;
64
+ task.get();
65
+ }
66
+ }
67
+ consumeTask(queueId) {
68
+ const queue = this.taskMap[queueId];
69
+ const head = queue.head,
70
+ tail = queue.tail;
71
+ const next = head.next;
72
+ head.next = null;
73
+ if (head === tail) {
74
+ queue.head = null;
75
+ queue.tail = null;
76
+ this.hasTask &= ~queueId;
77
+ } else {
78
+ head.next = null;
79
+ queue.head = next;
80
+ }
81
+ return head;
82
+ }
83
+ }
84
+ const multiScheduler = new MultiScheduler(4);
85
+
86
+ let Keys = function (Keys) {
87
+ Keys["Iterator"] = "__AOYE_ITERATOR";
88
+ Keys["Raw"] = "__AOYE_RAW";
89
+ Keys["Meta"] = "__AOYE_META";
90
+ Keys["ProxyFreeObject"] = "__AOYE_PROXY_FREE_OBJECT";
91
+ return Keys;
92
+ }({});
93
+ const IsStore = Symbol('__AOYE_IS_STORE'),
94
+ StoreIgnoreKeys = Symbol('__AOYE_IGNORE_KEYS');
95
+
96
+ const ide = globalThis.requestIdleCallback || (globalThis.requestAnimationFrame ? fn => globalThis.requestAnimationFrame(() => {
97
+ setTimeout(() => {
98
+ fn();
99
+ });
100
+ }) : globalThis.setTimeout);
101
+ const now = () => {
102
+ const timer = globalThis.performance || globalThis.Date;
103
+ return timer.now();
104
+ };
105
+ let channel = globalThis.MessageChannel ? new MessageChannel() : null;
106
+ if (globalThis.MessageChannel) {
107
+ channel = new MessageChannel();
108
+ }
109
+ let msgId = 0;
110
+ const macro = fn => {
111
+ if (!channel) {
112
+ setTimeout(fn);
113
+ }
114
+ const memoId = msgId;
115
+ function onMessage(e) {
116
+ if (memoId === e.data) {
117
+ fn();
118
+ channel.port2.removeEventListener('message', onMessage);
119
+ }
120
+ }
121
+ channel.port2.addEventListener('message', onMessage);
122
+ channel.port1.postMessage(msgId++);
123
+ };
124
+ const p = Promise.resolve();
125
+ const micro = cb => {
126
+ p.then(cb);
127
+ };
128
+ const toRaw = a => {
129
+ if (typeof a === 'object' && a !== null && a[Keys.Raw]) {
130
+ return toRaw(a[Keys.Raw]);
131
+ }
132
+ return a;
133
+ };
134
+
19
135
  function mark(signal) {
20
136
  let line = signal.emitHead;
21
137
  while (line) {
22
- const _line = line,
23
- down = _line.down;
24
- _line.up;
138
+ const down = line.down;
25
139
  const scope = down.scope,
26
140
  emitHead = down.emitHead,
27
141
  state = down.state;
@@ -30,7 +144,11 @@ function mark(signal) {
30
144
  down.state |= notLocked ? 2 : 16;
31
145
  if (state & 32) {
32
146
  if (notLocked && state & 512) {
33
- addEffect(down);
147
+ if (down.type === ScheduleType.Sync) {
148
+ addEffect(down);
149
+ } else {
150
+ multiScheduler.addTask(down.type, down);
151
+ }
34
152
  }
35
153
  } else if (emitHead) {
36
154
  markUnknownDeep(emitHead);
@@ -47,9 +165,7 @@ function markUnknownDeep(initialLine) {
47
165
  let line = stack[--len];
48
166
  stack[len] = null;
49
167
  while (line) {
50
- const _line2 = line,
51
- down = _line2.down;
52
- _line2.up;
168
+ const down = line.down;
53
169
  const state = down.state,
54
170
  scope = down.scope;
55
171
  if (scope && scope.state & 128 || noPulling && state & 6) ; else {
@@ -57,7 +173,11 @@ function markUnknownDeep(initialLine) {
57
173
  down.state |= notLocked ? 4 : 8;
58
174
  if (state & 32) {
59
175
  if (notLocked && state & 512) {
60
- addEffect(down);
176
+ if (down.type === ScheduleType.Sync) {
177
+ addEffect(down);
178
+ } else {
179
+ multiScheduler.addTask(down.type, down);
180
+ }
61
181
  }
62
182
  } else if (down.emitHead) {
63
183
  stack[len++] = down.emitHead;
@@ -101,8 +221,8 @@ function pullDeep(root) {
101
221
  if (noGoSibling = value !== prevValue) {
102
222
  let line = node.emitHead;
103
223
  while (line) {
104
- const _line3 = line,
105
- down = _line3.down;
224
+ const _line = line,
225
+ down = _line.down;
106
226
  down.state &= -5;
107
227
  down.state |= 2;
108
228
  line = line.nextEmitLine;
@@ -146,7 +266,7 @@ let consumeI = -1,
146
266
  function addEffect(effect) {
147
267
  effectQueue[++produceI] = effect;
148
268
  }
149
- function flushEffect() {
269
+ function flushSyncEffect() {
150
270
  if (consumeI !== -1) {
151
271
  return;
152
272
  }
@@ -160,12 +280,24 @@ function flushEffect() {
160
280
  consumeI = -1;
161
281
  produceI = -1;
162
282
  }
283
+ let schedulerStatus = ScheduleStatus.Idle;
284
+ function flushMicroEffect() {
285
+ if (schedulerStatus === ScheduleStatus.Idle && multiScheduler.hasTask) {
286
+ schedulerStatus = ScheduleStatus.Ready;
287
+ micro(() => {
288
+ schedulerStatus = ScheduleStatus.Running;
289
+ multiScheduler.flushAllTask();
290
+ schedulerStatus = ScheduleStatus.Idle;
291
+ });
292
+ }
293
+ }
163
294
  let _batchDeep = 0;
164
295
  const batchStart = () => _batchDeep++;
165
296
  const batchEnd = () => {
166
297
  _batchDeep--;
167
298
  if (_batchDeep === 0) {
168
- flushEffect();
299
+ flushSyncEffect();
300
+ flushMicroEffect();
169
301
  }
170
302
  };
171
303
  const batchDeep = () => _batchDeep;
@@ -493,7 +625,8 @@ class Signal {
493
625
  if (this.emitHead) {
494
626
  mark(this);
495
627
  if (batchDeep() === 0) {
496
- flushEffect();
628
+ flushSyncEffect();
629
+ flushMicroEffect();
497
630
  }
498
631
  }
499
632
  }
@@ -509,8 +642,9 @@ class Effect {
509
642
  scope = getPulling();
510
643
  outLink = null;
511
644
  clean = null;
512
- constructor(callback) {
645
+ constructor(callback, type = ScheduleType.Sync) {
513
646
  this.callback = callback;
647
+ this.type = type;
514
648
  this.get();
515
649
  }
516
650
  get(shouldLink = true, notForceUpdate = true) {
@@ -577,16 +711,6 @@ class Scope {
577
711
  }
578
712
  Scope.prototype.dispose = dispose;
579
713
 
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
714
  const rawToProxy = new WeakMap();
591
715
  let State = function (State) {
592
716
  State[State["Clean"] = 0] = "Clean";
@@ -605,45 +729,6 @@ State.Unknown | State.Dirty;
605
729
  State.ScopeReady | State.ScopeAbort;
606
730
  State.ScopeAbort;
607
731
 
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
732
  const deepSignal = (target, scope, deep = true) => {
648
733
  const isObj = typeof target === 'object' && target !== null;
649
734
  if (!isObj || target[Keys.Raw] || target[Keys.ProxyFreeObject]) return target;
@@ -1169,11 +1254,14 @@ function $(data) {
1169
1254
  };
1170
1255
  }
1171
1256
  }
1257
+ ({
1258
+ type: ScheduleType.Sync
1259
+ });
1172
1260
  function effectUt(callback, depOrOpt, opt) {
1173
1261
  const hasDep = Array.isArray(depOrOpt);
1174
1262
  opt = hasDep ? opt || {} : depOrOpt || {};
1175
1263
  if (!hasDep) {
1176
- const ef = new Effect(callback);
1264
+ const ef = new Effect(callback, opt.type);
1177
1265
  const run = ef.dispose.bind(ef);
1178
1266
  run.ins = ef;
1179
1267
  return run;
@@ -1187,19 +1275,19 @@ function effectUt(callback, depOrOpt, opt) {
1187
1275
  old: null,
1188
1276
  val: null
1189
1277
  }));
1190
- const ef = new Effect(() => {
1278
+ const ef = new Effect(eff => {
1191
1279
  for (let i = 0; i < deps.length; i++) {
1192
1280
  const value = deps[i].v;
1193
1281
  vs[i].old = vs[i].val;
1194
1282
  vs[i].val = value;
1195
1283
  }
1196
1284
  if (mounted || immediate) {
1197
- ef.state |= 256;
1285
+ eff.state |= 256;
1198
1286
  callback(...vs);
1199
- ef.state &= -257;
1287
+ eff.state &= -257;
1200
1288
  }
1201
1289
  mounted = true;
1202
- });
1290
+ }, opt.type);
1203
1291
  const run = ef.dispose.bind(ef);
1204
1292
  run.ins = ef;
1205
1293
  return run;
@@ -1208,7 +1296,7 @@ function effect(callback, depOrOpt, opt) {
1208
1296
  const hasDep = Array.isArray(depOrOpt);
1209
1297
  opt = hasDep ? opt || {} : depOrOpt || {};
1210
1298
  if (!hasDep) {
1211
- const ef = new Effect(callback);
1299
+ const ef = new Effect(callback, opt.type);
1212
1300
  return ef;
1213
1301
  }
1214
1302
  let mounted = false;
@@ -1232,7 +1320,7 @@ function effect(callback, depOrOpt, opt) {
1232
1320
  eff.state &= -257;
1233
1321
  }
1234
1322
  mounted = true;
1235
- });
1323
+ }, opt.type);
1236
1324
  return ef;
1237
1325
  }
1238
1326
  function scope(...args) {
@@ -1246,5 +1334,5 @@ function scope(...args) {
1246
1334
  return run;
1247
1335
  }
1248
1336
 
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 };
1337
+ export { $, Computed, Effect, IsStore, Keys, ScheduleStatus, 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
1338
  //# sourceMappingURL=aoye.esm.js.map