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