@qwik.dev/core 2.0.0-beta.30 → 2.0.0-beta.32

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.
@@ -1,9 +1,20 @@
1
- import { isBrowser } from "@qwik.dev/core/build";
2
- const doc = isBrowser ? document : void 0;
3
- const config = { t: 0, o: 25, l: 0.65 };
4
- const rel = isBrowser && doc.createElement("link").relList.supports("modulepreload") ? "modulePreload" : "preload";
5
- const loadStart = Date.now();
1
+ import { isServer, isBrowser as isBrowser$2 } from "@qwik.dev/core/build";
2
+ const createMacroTask = (e) => {
3
+ let t;
4
+ if (typeof MessageChannel !== "undefined") {
5
+ const n = new MessageChannel();
6
+ n.port1.onmessage = () => e();
7
+ t = () => n.port2.postMessage(null);
8
+ } else t = () => setTimeout(e);
9
+ return t;
10
+ };
11
+ const isBrowser$1 = !isServer;
12
+ const doc = isBrowser$1 ? document : void 0;
13
+ const config = { t: 0, o: 25, u: 0.65 };
14
+ const rel = isBrowser$1 && doc.createElement("link").relList?.supports?.("modulepreload") ? "modulePreload" : "preload";
15
+ const loadStart = performance.now();
6
16
  const isJSRegex = /\.[mc]?js$/;
17
+ const yieldInterval = 1e3 / 60;
7
18
  const BundleImportState_None = 0;
8
19
  const BundleImportState_Queued = 1;
9
20
  const BundleImportState_Preload = 2;
@@ -13,178 +24,265 @@ const bundles = /* @__PURE__ */ new Map();
13
24
  let queueDirty;
14
25
  let preloadCount = 0;
15
26
  const queue = [];
27
+ const nextTriggerMacroTask = createMacroTask(trigger);
28
+ const nextAdjustmentMacroTask = createMacroTask(processPendingAdjustments);
29
+ let isTriggerScheduled = 0;
30
+ let isAdjustmentScheduled = 0;
31
+ let isProcessingAdjustments = 0;
32
+ const shouldYieldInBrowser = isBrowser$2;
33
+ const adjustmentStack = [];
16
34
  const log = (...e) => {
17
- console.log(`Preloader ${Date.now() - loadStart}ms ${preloadCount}/${queue.length} queued>`, ...e);
35
+ console.log(`Preloader ${performance.now() - loadStart}ms ${preloadCount}/${queue.length} queued>`, ...e);
18
36
  };
19
37
  const sortQueue = () => {
20
38
  if (queueDirty) {
21
- queue.sort((e, t) => e.u - t.u);
39
+ queue.sort((e, t) => e.l - t.l);
22
40
  queueDirty = 0;
23
41
  }
24
42
  };
25
- const trigger = () => {
43
+ function trigger() {
44
+ isTriggerScheduled = 0;
26
45
  if (!queue.length) return;
27
46
  sortQueue();
47
+ const e = performance.now() + yieldInterval;
48
+ let t = 0;
28
49
  while (queue.length) {
29
- const e = queue[0];
30
- const t = e.u;
31
- const o = 1 - t;
32
- const n = graph ? config.o : (
50
+ const n = queue[0];
51
+ const s = n.l;
52
+ const o = 1 - s;
53
+ const r = graph ? config.o : (
33
54
  // While the graph is not available, we limit to 5 preloads
34
55
  5
35
56
  );
36
- if (o >= 0.99 || preloadCount < n) {
57
+ if (o >= 0.99 || preloadCount < r) {
37
58
  queue.shift();
38
- preloadOne(e);
59
+ preloadOne(n);
60
+ if (performance.now() >= e) {
61
+ t = 1;
62
+ break;
63
+ }
39
64
  } else break;
40
65
  }
66
+ if (t && queue.length && !isTriggerScheduled) {
67
+ isTriggerScheduled = 1;
68
+ nextTriggerMacroTask();
69
+ }
41
70
  if (config.t && !queue.length) {
42
- const e = [...bundles.values()].filter((e2) => e2.i > BundleImportState_None);
43
- const t = e.reduce((e2, t2) => e2 + t2.p, 0);
44
- const o = e.reduce((e2, t2) => e2 + t2.$, 0);
45
- log(`>>>> done ${e.length}/${bundles.size} total: ${t}ms waited, ${o}ms loaded`);
71
+ const e2 = [...bundles.values()].filter((e3) => e3.i > BundleImportState_None);
72
+ const t2 = e2.reduce((e3, t3) => e3 + t3.m, 0);
73
+ const n = e2.reduce((e3, t3) => e3 + t3.p, 0);
74
+ log(`>>>> done ${e2.length}/${bundles.size} total: ${t2}ms waited, ${n}ms loaded`);
46
75
  }
76
+ }
77
+ const enqueueAdjustment = (e, t, n, s) => {
78
+ adjustmentStack.unshift({ $: e, l: t, B: s, h: n });
47
79
  };
48
- const preloadOne = (e) => {
49
- if (e.i >= BundleImportState_Preload) return;
50
- preloadCount++;
51
- const t = Date.now();
52
- e.p = t - e.B;
53
- e.i = BundleImportState_Preload;
54
- config.t && log(`<< load ${Math.round((1 - e.u) * 100)}% after ${`${e.p}ms`}`, e.m);
55
- const o = doc.createElement("link");
56
- o.href = new URL(`${base}${e.m}`, doc.baseURI).toString();
57
- o.rel = rel;
58
- o.as = "script";
59
- o.onload = o.onerror = () => {
60
- preloadCount--;
61
- const n = Date.now();
62
- e.$ = n - t;
63
- e.i = BundleImportState_Loaded;
64
- config.t && log(`>> done after ${e.$}ms`, e.m);
65
- o.remove();
66
- trigger();
67
- };
68
- doc.head.appendChild(o);
69
- };
70
- const adjustProbabilities = (e, t, o) => {
71
- if (o?.has(e)) return;
72
- const n = e.u;
73
- e.u = t;
74
- if (n - e.u < 0.01) return;
80
+ const processAdjustmentFrame = () => {
81
+ const e = adjustmentStack[adjustmentStack.length - 1];
82
+ const t = e.$;
83
+ if (e.S) {
84
+ const n2 = e.j;
85
+ if (n2 >= e.S.length) {
86
+ adjustmentStack.pop();
87
+ return 0;
88
+ }
89
+ const s = e.S[n2];
90
+ e.j = n2 + 1;
91
+ const o = getBundle(s.k);
92
+ if (o.l === 0) return 1;
93
+ const r = 1 - t.l;
94
+ let a;
95
+ if (r === 1 || r >= 0.99 && e.h.T < 100) {
96
+ e.h.T++;
97
+ a = Math.min(0.01, 1 - s.I);
98
+ } else {
99
+ const e2 = 1 - s.I * r;
100
+ const t2 = s.A;
101
+ const n3 = e2 / t2;
102
+ a = Math.max(0.02, o.l * n3);
103
+ s.A = n3;
104
+ }
105
+ adjustmentStack.push({ $: o, l: a, B: e.B, h: e.h });
106
+ return 1;
107
+ }
108
+ if (e.B?.has(t)) {
109
+ adjustmentStack.pop();
110
+ return 0;
111
+ }
112
+ const n = t.l;
113
+ t.l = e.l;
114
+ if (n - t.l < 0.01) {
115
+ adjustmentStack.pop();
116
+ return 0;
117
+ }
75
118
  if (
76
119
  // don't queue until we have initialized the preloader
77
- base != null && e.i < BundleImportState_Preload
120
+ base != null && t.i < BundleImportState_Preload
78
121
  ) {
79
- if (e.i === BundleImportState_None) {
80
- e.i = BundleImportState_Queued;
81
- queue.push(e);
82
- config.t && log(`queued ${Math.round((1 - e.u) * 100)}%`, e.m);
122
+ if (t.i === BundleImportState_None) {
123
+ t.i = BundleImportState_Queued;
124
+ queue.push(t);
125
+ config.t && log(`queued ${Math.round((1 - t.l) * 100)}%`, t.k);
83
126
  }
84
127
  queueDirty = 1;
85
128
  }
86
- if (e.h) {
87
- o ||= /* @__PURE__ */ new Set();
88
- o.add(e);
89
- const t2 = 1 - e.u;
90
- for (const n2 of e.h) {
91
- const e2 = getBundle(n2.m);
92
- if (e2.u === 0) continue;
93
- let r;
94
- if (t2 === 1 || t2 >= 0.99 && depsCount < 100) {
95
- depsCount++;
96
- r = Math.min(0.01, 1 - n2.S);
97
- } else {
98
- const o2 = 1 - n2.S * t2;
99
- const s = n2.q;
100
- const l = o2 / s;
101
- r = Math.max(0.02, e2.u * l);
102
- n2.q = l;
129
+ if (t.S?.length) {
130
+ const n2 = e.B || /* @__PURE__ */ new Set();
131
+ n2.add(t);
132
+ e.B = n2;
133
+ e.S = t.S;
134
+ e.j = 0;
135
+ return 0;
136
+ }
137
+ adjustmentStack.pop();
138
+ return 0;
139
+ };
140
+ function processPendingAdjustments() {
141
+ if (isProcessingAdjustments || !adjustmentStack.length) return;
142
+ isAdjustmentScheduled = 0;
143
+ isProcessingAdjustments = 1;
144
+ const e = shouldYieldInBrowser ? performance.now() + yieldInterval : 0;
145
+ let t = 0;
146
+ while (adjustmentStack.length) {
147
+ t = 1;
148
+ const n = processAdjustmentFrame();
149
+ if (shouldYieldInBrowser && n && performance.now() >= e) {
150
+ if (!isAdjustmentScheduled) {
151
+ isAdjustmentScheduled = 1;
152
+ nextAdjustmentMacroTask();
103
153
  }
104
- adjustProbabilities(e2, r, o);
154
+ break;
105
155
  }
106
156
  }
157
+ isProcessingAdjustments = 0;
158
+ if (t && shouldYieldInBrowser) nextTriggerMacroTask();
159
+ }
160
+ const preloadOne = (e) => {
161
+ if (e.i >= BundleImportState_Preload) return;
162
+ preloadCount++;
163
+ const t = performance.now();
164
+ e.m = t - e.M;
165
+ e.i = BundleImportState_Preload;
166
+ config.t && log(`<< load ${Math.round((1 - e.l) * 100)}% after ${`${e.m}ms`}`, e.k);
167
+ const n = doc.createElement("link");
168
+ n.href = new URL(`${base}${e.k}`, doc.baseURI).toString();
169
+ n.rel = rel;
170
+ n.as = "script";
171
+ n.onload = n.onerror = () => {
172
+ preloadCount--;
173
+ const s = performance.now();
174
+ e.p = s - t;
175
+ e.i = BundleImportState_Loaded;
176
+ config.t && log(`>> done after ${e.p}ms`, e.k);
177
+ n.remove();
178
+ nextTriggerMacroTask();
179
+ };
180
+ doc.head.appendChild(n);
107
181
  };
108
- const handleBundle = (e, t) => {
109
- const o = getBundle(e);
110
- if (o && o.u > t) adjustProbabilities(o, t);
182
+ const adjustProbabilities = (e, t, n) => {
183
+ enqueueAdjustment(e, t, { T: 0 }, n);
184
+ if (shouldYieldInBrowser) nextAdjustmentMacroTask();
185
+ else processPendingAdjustments();
186
+ };
187
+ const handleBundle = (e, t, n) => {
188
+ const s = getBundle(e);
189
+ if (s && s.l > t) if (n) enqueueAdjustment(s, t, n);
190
+ else adjustProbabilities(s, t);
111
191
  };
112
- let depsCount;
113
192
  const preload = (e, t) => {
114
193
  if (!e?.length) return;
115
- depsCount = 0;
116
- let o = t ? 1 - t : 0.4;
194
+ let n = t ? 1 - t : 0.4;
195
+ const s = { T: 0 };
117
196
  if (Array.isArray(e)) for (let t2 = e.length - 1; t2 >= 0; t2--) {
118
- const n = e[t2];
119
- if (typeof n === "number") o = 1 - n / 10;
120
- else handleBundle(n, o);
197
+ const o = e[t2];
198
+ if (typeof o === "number") n = 1 - o / 10;
199
+ else handleBundle(o, n, s);
121
200
  }
122
- else handleBundle(e, o);
123
- if (isBrowser) trigger();
201
+ else handleBundle(e, n, s);
202
+ if (shouldYieldInBrowser) nextAdjustmentMacroTask();
203
+ else processPendingAdjustments();
124
204
  };
125
- if (isBrowser) document.addEventListener("qsymbol", (e) => {
126
- const { symbol: t, href: o } = e.detail;
127
- if (o) {
205
+ if (isBrowser$2) document.addEventListener("qsymbol", (e) => {
206
+ const { symbol: t, href: n } = e.detail;
207
+ if (n) {
128
208
  const e2 = t.slice(t.lastIndexOf("_") + 1);
129
209
  preload(e2, 1);
130
210
  }
131
211
  });
132
212
  let base;
133
213
  let graph;
134
- const makeBundle = (e, t) => ({ m: e, i: isJSRegex.test(e) ? BundleImportState_None : BundleImportState_Alias, h: t, u: 1, B: Date.now(), p: 0, $: 0 });
214
+ const isBrowser = !isServer;
215
+ const makeBundle = (e, t) => ({ k: e, i: isJSRegex.test(e) ? BundleImportState_None : BundleImportState_Alias, S: t, l: 1, M: performance.now(), m: 0, p: 0 });
135
216
  const parseBundleGraph = (e) => {
136
217
  const t = /* @__PURE__ */ new Map();
137
- let o = 0;
138
- while (o < e.length) {
139
- const n = e[o++];
140
- const r = [];
141
- let s;
142
- let l = 1;
143
- while (s = e[o], typeof s === "number") {
144
- if (s < 0) l = -s / 10;
145
- else r.push({ m: e[s], S: l, q: 1 });
146
- o++;
218
+ let n = 0;
219
+ while (n < e.length) {
220
+ const s = e[n++];
221
+ const o = [];
222
+ let r;
223
+ let a = 1;
224
+ while (r = e[n], typeof r === "number") {
225
+ if (r < 0) a = -r / 10;
226
+ else o.push({ k: e[r], I: a, A: 1 });
227
+ n++;
147
228
  }
148
- t.set(n, r);
229
+ t.set(s, o);
149
230
  }
150
231
  return t;
151
232
  };
152
233
  const getBundle = (e) => {
153
234
  let t = bundles.get(e);
154
235
  if (!t) {
155
- let o;
236
+ let n;
156
237
  if (graph) {
157
- o = graph.get(e);
158
- if (!o) return;
159
- if (!o.length) o = void 0;
238
+ n = graph.get(e);
239
+ if (!n) return;
240
+ if (!n.length) n = void 0;
160
241
  }
161
- t = makeBundle(e, o);
242
+ t = makeBundle(e, n);
162
243
  bundles.set(e, t);
163
244
  }
164
245
  return t;
165
246
  };
166
- const loadBundleGraph = (e, t, o) => {
167
- if (o) {
168
- if ("d" in o) config.t = !!o.d;
169
- if ("P" in o) config.o = o["P"];
170
- if ("Q" in o) config.l = 1 - o["Q"];
247
+ const loadBundleGraph = (e, t, n) => {
248
+ if (n) {
249
+ if ("d" in n) config.t = !!n.d;
250
+ if ("P" in n) config.o = n["P"];
251
+ if ("Q" in n) config.u = 1 - n["Q"];
171
252
  }
172
253
  if (!isBrowser || e == null) return;
173
254
  base = e;
174
255
  if (t) t.then((e2) => e2.text()).then((e2) => {
175
256
  graph = parseBundleGraph(JSON.parse(e2));
176
257
  const t2 = [];
177
- for (const [e3, o2] of graph.entries()) {
178
- const n = getBundle(e3);
179
- n.h = o2;
180
- if (n.u < 1) {
181
- t2.push([n, n.u]);
182
- n.u = 1;
258
+ for (const [e3, n3] of graph.entries()) {
259
+ const s2 = getBundle(e3);
260
+ s2.S = n3;
261
+ if (s2.l < 1) {
262
+ t2.push([s2, s2.l]);
263
+ s2.l = 1;
183
264
  }
184
265
  }
185
266
  config.t && log(`parseBundleGraph got ${graph.size} bundles, adjusting ${t2.length}`);
186
- for (const [e3, o2] of t2) adjustProbabilities(e3, o2);
187
- trigger();
267
+ if (!t2.length) {
268
+ nextTriggerMacroTask();
269
+ return;
270
+ }
271
+ let n2 = 0;
272
+ const s = createMacroTask(() => {
273
+ const e3 = performance.now() + yieldInterval;
274
+ while (n2 < t2.length) {
275
+ const [o, r] = t2[n2];
276
+ n2++;
277
+ adjustProbabilities(o, r);
278
+ if (n2 < t2.length && performance.now() >= e3) {
279
+ s();
280
+ return;
281
+ }
282
+ }
283
+ nextTriggerMacroTask();
284
+ });
285
+ s();
188
286
  }).catch(console.warn);
189
287
  };
190
288
  export {