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/index.umd.js CHANGED
@@ -20,12 +20,126 @@
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
+ let ScheduleStatus = function (ScheduleStatus) {
31
+ ScheduleStatus[ScheduleStatus["Idle"] = 0] = "Idle";
32
+ ScheduleStatus[ScheduleStatus["Ready"] = 1] = "Ready";
33
+ ScheduleStatus[ScheduleStatus["Running"] = 2] = "Running";
34
+ return ScheduleStatus;
35
+ }({});
36
+
37
+ class MultiScheduler {
38
+ hasTask = 0;
39
+ taskMap = {};
40
+ constructor(queueCount) {
41
+ for (let i = 0; i < queueCount; i++) {
42
+ this.taskMap[1 << i] = {
43
+ head: null,
44
+ tail: null
45
+ };
46
+ }
47
+ }
48
+ addTask(queueId, task) {
49
+ const queue = this.taskMap[queueId];
50
+ const tail = queue.tail;
51
+ const item = {
52
+ value: task,
53
+ next: null
54
+ };
55
+ if (tail) {
56
+ tail.next = item;
57
+ } else {
58
+ queue.head = item;
59
+ }
60
+ queue.tail = item;
61
+ this.hasTask |= queueId;
62
+ }
63
+ flushAllTask() {
64
+ while (this.hasTask) {
65
+ const hasTask = this.hasTask;
66
+ const highest = hasTask & ~hasTask + 1;
67
+ const task = this.consumeTask(highest).value;
68
+ task.get();
69
+ }
70
+ }
71
+ consumeTask(queueId) {
72
+ const queue = this.taskMap[queueId];
73
+ const head = queue.head,
74
+ tail = queue.tail;
75
+ const next = head.next;
76
+ head.next = null;
77
+ if (head === tail) {
78
+ queue.head = null;
79
+ queue.tail = null;
80
+ this.hasTask &= ~queueId;
81
+ } else {
82
+ head.next = null;
83
+ queue.head = next;
84
+ }
85
+ return head;
86
+ }
87
+ }
88
+ const multiScheduler = new MultiScheduler(4);
89
+
90
+ let Keys = function (Keys) {
91
+ Keys["Iterator"] = "__AOYE_ITERATOR";
92
+ Keys["Raw"] = "__AOYE_RAW";
93
+ Keys["Meta"] = "__AOYE_META";
94
+ Keys["ProxyFreeObject"] = "__AOYE_PROXY_FREE_OBJECT";
95
+ return Keys;
96
+ }({});
97
+ const IsStore = Symbol('__AOYE_IS_STORE'),
98
+ StoreIgnoreKeys = Symbol('__AOYE_IGNORE_KEYS');
99
+
100
+ const ide = globalThis.requestIdleCallback || (globalThis.requestAnimationFrame ? fn => globalThis.requestAnimationFrame(() => {
101
+ setTimeout(() => {
102
+ fn();
103
+ });
104
+ }) : globalThis.setTimeout);
105
+ const now = () => {
106
+ const timer = globalThis.performance || globalThis.Date;
107
+ return timer.now();
108
+ };
109
+ let channel = globalThis.MessageChannel ? new MessageChannel() : null;
110
+ if (globalThis.MessageChannel) {
111
+ channel = new MessageChannel();
112
+ }
113
+ let msgId = 0;
114
+ const macro = fn => {
115
+ if (!channel) {
116
+ setTimeout(fn);
117
+ }
118
+ const memoId = msgId;
119
+ function onMessage(e) {
120
+ if (memoId === e.data) {
121
+ fn();
122
+ channel.port2.removeEventListener('message', onMessage);
123
+ }
124
+ }
125
+ channel.port2.addEventListener('message', onMessage);
126
+ channel.port1.postMessage(msgId++);
127
+ };
128
+ const p = Promise.resolve();
129
+ const micro = cb => {
130
+ p.then(cb);
131
+ };
132
+ const toRaw = a => {
133
+ if (typeof a === 'object' && a !== null && a[Keys.Raw]) {
134
+ return toRaw(a[Keys.Raw]);
135
+ }
136
+ return a;
137
+ };
138
+
23
139
  function mark(signal) {
24
140
  let line = signal.emitHead;
25
141
  while (line) {
26
- const _line = line,
27
- down = _line.down;
28
- _line.up;
142
+ const down = line.down;
29
143
  const scope = down.scope,
30
144
  emitHead = down.emitHead,
31
145
  state = down.state;
@@ -34,7 +148,11 @@
34
148
  down.state |= notLocked ? 2 : 16;
35
149
  if (state & 32) {
36
150
  if (notLocked && state & 512) {
37
- addEffect(down);
151
+ if (down.type === ScheduleType.Sync) {
152
+ addEffect(down);
153
+ } else {
154
+ multiScheduler.addTask(down.type, down);
155
+ }
38
156
  }
39
157
  } else if (emitHead) {
40
158
  markUnknownDeep(emitHead);
@@ -51,9 +169,7 @@
51
169
  let line = stack[--len];
52
170
  stack[len] = null;
53
171
  while (line) {
54
- const _line2 = line,
55
- down = _line2.down;
56
- _line2.up;
172
+ const down = line.down;
57
173
  const state = down.state,
58
174
  scope = down.scope;
59
175
  if (scope && scope.state & 128 || noPulling && state & 6) ; else {
@@ -61,7 +177,11 @@
61
177
  down.state |= notLocked ? 4 : 8;
62
178
  if (state & 32) {
63
179
  if (notLocked && state & 512) {
64
- addEffect(down);
180
+ if (down.type === ScheduleType.Sync) {
181
+ addEffect(down);
182
+ } else {
183
+ multiScheduler.addTask(down.type, down);
184
+ }
65
185
  }
66
186
  } else if (down.emitHead) {
67
187
  stack[len++] = down.emitHead;
@@ -105,8 +225,8 @@
105
225
  if (noGoSibling = value !== prevValue) {
106
226
  let line = node.emitHead;
107
227
  while (line) {
108
- const _line3 = line,
109
- down = _line3.down;
228
+ const _line = line,
229
+ down = _line.down;
110
230
  down.state &= -5;
111
231
  down.state |= 2;
112
232
  line = line.nextEmitLine;
@@ -150,7 +270,7 @@
150
270
  function addEffect(effect) {
151
271
  effectQueue[++produceI] = effect;
152
272
  }
153
- function flushEffect() {
273
+ function flushSyncEffect() {
154
274
  if (consumeI !== -1) {
155
275
  return;
156
276
  }
@@ -164,12 +284,24 @@
164
284
  consumeI = -1;
165
285
  produceI = -1;
166
286
  }
287
+ let schedulerStatus = ScheduleStatus.Idle;
288
+ function flushMicroEffect() {
289
+ if (schedulerStatus === ScheduleStatus.Idle && multiScheduler.hasTask) {
290
+ schedulerStatus = ScheduleStatus.Ready;
291
+ micro(() => {
292
+ schedulerStatus = ScheduleStatus.Running;
293
+ multiScheduler.flushAllTask();
294
+ schedulerStatus = ScheduleStatus.Idle;
295
+ });
296
+ }
297
+ }
167
298
  let _batchDeep = 0;
168
299
  const batchStart = () => _batchDeep++;
169
300
  const batchEnd = () => {
170
301
  _batchDeep--;
171
302
  if (_batchDeep === 0) {
172
- flushEffect();
303
+ flushSyncEffect();
304
+ flushMicroEffect();
173
305
  }
174
306
  };
175
307
  const batchDeep = () => _batchDeep;
@@ -497,7 +629,8 @@
497
629
  if (this.emitHead) {
498
630
  mark(this);
499
631
  if (batchDeep() === 0) {
500
- flushEffect();
632
+ flushSyncEffect();
633
+ flushMicroEffect();
501
634
  }
502
635
  }
503
636
  }
@@ -513,8 +646,9 @@
513
646
  scope = getPulling();
514
647
  outLink = null;
515
648
  clean = null;
516
- constructor(callback) {
649
+ constructor(callback, type = ScheduleType.Sync) {
517
650
  this.callback = callback;
651
+ this.type = type;
518
652
  this.get();
519
653
  }
520
654
  get(shouldLink = true, notForceUpdate = true) {
@@ -581,16 +715,6 @@
581
715
  }
582
716
  Scope.prototype.dispose = dispose;
583
717
 
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
718
  const rawToProxy = new WeakMap();
595
719
  let State = function (State) {
596
720
  State[State["Clean"] = 0] = "Clean";
@@ -609,45 +733,6 @@
609
733
  State.ScopeReady | State.ScopeAbort;
610
734
  State.ScopeAbort;
611
735
 
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
736
  const deepSignal = (target, scope, deep = true) => {
652
737
  const isObj = typeof target === 'object' && target !== null;
653
738
  if (!isObj || target[Keys.Raw] || target[Keys.ProxyFreeObject]) return target;
@@ -1173,11 +1258,14 @@
1173
1258
  };
1174
1259
  }
1175
1260
  }
1261
+ ({
1262
+ type: ScheduleType.Sync
1263
+ });
1176
1264
  function effectUt(callback, depOrOpt, opt) {
1177
1265
  const hasDep = Array.isArray(depOrOpt);
1178
1266
  opt = hasDep ? opt || {} : depOrOpt || {};
1179
1267
  if (!hasDep) {
1180
- const ef = new Effect(callback);
1268
+ const ef = new Effect(callback, opt.type);
1181
1269
  const run = ef.dispose.bind(ef);
1182
1270
  run.ins = ef;
1183
1271
  return run;
@@ -1191,19 +1279,19 @@
1191
1279
  old: null,
1192
1280
  val: null
1193
1281
  }));
1194
- const ef = new Effect(() => {
1282
+ const ef = new Effect(eff => {
1195
1283
  for (let i = 0; i < deps.length; i++) {
1196
1284
  const value = deps[i].v;
1197
1285
  vs[i].old = vs[i].val;
1198
1286
  vs[i].val = value;
1199
1287
  }
1200
1288
  if (mounted || immediate) {
1201
- ef.state |= 256;
1289
+ eff.state |= 256;
1202
1290
  callback(...vs);
1203
- ef.state &= -257;
1291
+ eff.state &= -257;
1204
1292
  }
1205
1293
  mounted = true;
1206
- });
1294
+ }, opt.type);
1207
1295
  const run = ef.dispose.bind(ef);
1208
1296
  run.ins = ef;
1209
1297
  return run;
@@ -1212,7 +1300,7 @@
1212
1300
  const hasDep = Array.isArray(depOrOpt);
1213
1301
  opt = hasDep ? opt || {} : depOrOpt || {};
1214
1302
  if (!hasDep) {
1215
- const ef = new Effect(callback);
1303
+ const ef = new Effect(callback, opt.type);
1216
1304
  return ef;
1217
1305
  }
1218
1306
  let mounted = false;
@@ -1236,7 +1324,7 @@
1236
1324
  eff.state &= -257;
1237
1325
  }
1238
1326
  mounted = true;
1239
- });
1327
+ }, opt.type);
1240
1328
  return ef;
1241
1329
  }
1242
1330
  function scope(...args) {
@@ -1255,6 +1343,8 @@
1255
1343
  exports.Effect = Effect;
1256
1344
  exports.IsStore = IsStore;
1257
1345
  exports.Keys = Keys;
1346
+ exports.ScheduleStatus = ScheduleStatus;
1347
+ exports.ScheduleType = ScheduleType;
1258
1348
  exports.Scope = Scope;
1259
1349
  exports.Signal = Signal;
1260
1350
  exports.Store = Store;