@isoloop/runtime 0.0.0-next.d028bc1 → 0.0.0-next.g3322248
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/bootstrap.js +388 -43
- package/dist/worker.js +372 -46
- package/package.json +3 -3
package/dist/bootstrap.js
CHANGED
|
@@ -8,14 +8,14 @@ var __esmMin = (fn, res, err) => () => {
|
|
|
8
8
|
throw err = [e], e;
|
|
9
9
|
}
|
|
10
10
|
};
|
|
11
|
-
var __exportAll = (all,
|
|
12
|
-
let target = {};
|
|
11
|
+
var __exportAll = (all, flags) => {
|
|
12
|
+
let target = Object.setPrototypeOf({}, null);
|
|
13
13
|
for (var name in all) __defProp(target, name, {
|
|
14
14
|
get: all[name],
|
|
15
15
|
enumerable: true
|
|
16
16
|
});
|
|
17
|
-
if (!
|
|
18
|
-
return target;
|
|
17
|
+
if (!(flags & 1)) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
18
|
+
return flags & 2 ? target : Object.preventExtensions(target);
|
|
19
19
|
};
|
|
20
20
|
// ../protocol/dist/index.js
|
|
21
21
|
/**
|
|
@@ -215,8 +215,266 @@ var init_dist = __esmMin((() => {
|
|
|
215
215
|
retention: { kind: "compacted" }
|
|
216
216
|
});
|
|
217
217
|
}));
|
|
218
|
+
// src/busy.ts
|
|
219
|
+
function createBusy(now = () => performance.now()) {
|
|
220
|
+
let frame = null;
|
|
221
|
+
const spent = /* @__PURE__ */ new Map();
|
|
222
|
+
const containers = /* @__PURE__ */ new WeakMap();
|
|
223
|
+
const bill = (key, ms) => {
|
|
224
|
+
spent.set(key, (spent.get(key) ?? 0) + ms);
|
|
225
|
+
};
|
|
226
|
+
const settle = (f) => {
|
|
227
|
+
const fallback = f.owner.key === PENDING ? null : f.owner.key;
|
|
228
|
+
if (f.pending) for (const o of f.pending) o.key = fallback;
|
|
229
|
+
if (f.roots && f.roots.length > 0) {
|
|
230
|
+
const unique = [...new Set(f.roots)];
|
|
231
|
+
const share = f.spent / unique.length;
|
|
232
|
+
for (const key of unique) bill(key, share);
|
|
233
|
+
} else if (fallback !== null) bill(fallback, f.spent);
|
|
234
|
+
};
|
|
235
|
+
const busy = {
|
|
236
|
+
run(owner, fn, self, args) {
|
|
237
|
+
const t = now();
|
|
238
|
+
const outer = frame;
|
|
239
|
+
if (outer) outer.spent += t - outer.start;
|
|
240
|
+
const f = {
|
|
241
|
+
owner,
|
|
242
|
+
start: t,
|
|
243
|
+
spent: 0,
|
|
244
|
+
parent: outer,
|
|
245
|
+
roots: null,
|
|
246
|
+
pending: null
|
|
247
|
+
};
|
|
248
|
+
frame = f;
|
|
249
|
+
try {
|
|
250
|
+
return fn.apply(self, args);
|
|
251
|
+
} finally {
|
|
252
|
+
const end = now();
|
|
253
|
+
f.spent += end - f.start;
|
|
254
|
+
frame = outer;
|
|
255
|
+
if (outer) outer.start = end;
|
|
256
|
+
settle(f);
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
capture() {
|
|
260
|
+
const f = frame;
|
|
261
|
+
if (!f) return { key: null };
|
|
262
|
+
const o = { key: PENDING };
|
|
263
|
+
(f.pending ??= []).push(o);
|
|
264
|
+
return o;
|
|
265
|
+
},
|
|
266
|
+
of: (key) => ({ key }),
|
|
267
|
+
bind(fn) {
|
|
268
|
+
const owner = busy.capture();
|
|
269
|
+
return (...args) => busy.run(owner, fn, void 0, args);
|
|
270
|
+
},
|
|
271
|
+
report(key) {
|
|
272
|
+
const f = frame;
|
|
273
|
+
if (!f) return;
|
|
274
|
+
(f.roots ??= []).push(key);
|
|
275
|
+
if (f.pending) {
|
|
276
|
+
for (const o of f.pending) o.key = key;
|
|
277
|
+
f.pending = null;
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
take(key) {
|
|
281
|
+
const ms = spent.get(key) ?? 0;
|
|
282
|
+
spent.delete(key);
|
|
283
|
+
return ms;
|
|
284
|
+
},
|
|
285
|
+
claim(container, key) {
|
|
286
|
+
containers.set(container, key);
|
|
287
|
+
},
|
|
288
|
+
keyOf(container) {
|
|
289
|
+
if (typeof container !== "object" || container === null) return null;
|
|
290
|
+
return containers.has(container) ? containers.get(container) ?? void 0 : null;
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
return busy;
|
|
294
|
+
}
|
|
295
|
+
/** Keeps a wrapper's `name` and `length` the native function's. */
|
|
296
|
+
function like(wrapper, native) {
|
|
297
|
+
Object.defineProperty(wrapper, "name", {
|
|
298
|
+
value: native.name,
|
|
299
|
+
configurable: true
|
|
300
|
+
});
|
|
301
|
+
Object.defineProperty(wrapper, "length", {
|
|
302
|
+
value: native.length,
|
|
303
|
+
configurable: true
|
|
304
|
+
});
|
|
305
|
+
return wrapper;
|
|
306
|
+
}
|
|
307
|
+
/** `f` as a callback, when it is a function: schedulers call any function. */
|
|
308
|
+
function callable(f) {
|
|
309
|
+
return typeof f === "function" ? f : null;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* `cb` run as `owner` when the scheduler calls it, with the scheduler's `this`
|
|
313
|
+
* and arguments. Always in a frame, even for nobody: a shared framework's
|
|
314
|
+
* task (React's scheduler port, set up while its module loaded) still splits
|
|
315
|
+
* among the roots it reports.
|
|
316
|
+
*/
|
|
317
|
+
function billed(busy, owner, cb) {
|
|
318
|
+
return function(...args) {
|
|
319
|
+
return busy.run(owner, cb, this, args);
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Wraps a scheduling function on `scope` so the callback in argument 0 is
|
|
324
|
+
* billed to the owner current at registration. Everything else (the return
|
|
325
|
+
* value, ids, `clear*`, other arguments) is the native's.
|
|
326
|
+
*/
|
|
327
|
+
function wrapScheduler(scope, name, busy) {
|
|
328
|
+
const native = Reflect.get(scope, name);
|
|
329
|
+
if (typeof native !== "function") return;
|
|
330
|
+
const wrapper = like(function(...args) {
|
|
331
|
+
const cb = callable(args[0]);
|
|
332
|
+
if (cb) args[0] = billed(busy, busy.capture(), cb);
|
|
333
|
+
return Reflect.apply(native, this ?? scope, args);
|
|
334
|
+
}, native);
|
|
335
|
+
Reflect.set(scope, name, wrapper);
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Wraps `MessagePort`'s `onmessage` and its `message` listeners, so a port
|
|
339
|
+
* a member creates (a scheduler's `MessageChannel`, say) bills its messages
|
|
340
|
+
* to that member. `onmessage` still reads back the function that was set,
|
|
341
|
+
* and `removeEventListener` still removes what `addEventListener` added.
|
|
342
|
+
*/
|
|
343
|
+
function wrapPorts(scope, busy) {
|
|
344
|
+
const Port = Reflect.get(scope, "MessagePort");
|
|
345
|
+
if (typeof Port !== "function") return;
|
|
346
|
+
const proto = Reflect.get(Port, "prototype");
|
|
347
|
+
if (typeof proto !== "object" || proto === null) return;
|
|
348
|
+
const on = Object.getOwnPropertyDescriptor(proto, "onmessage");
|
|
349
|
+
const get = callable(on ? Reflect.get(on, "get") : void 0);
|
|
350
|
+
const set = callable(on ? Reflect.get(on, "set") : void 0);
|
|
351
|
+
if (on && get && set) {
|
|
352
|
+
/** What each port's `onmessage` was set to, as read back. */
|
|
353
|
+
const assigned = /* @__PURE__ */ new WeakMap();
|
|
354
|
+
Object.defineProperty(proto, "onmessage", {
|
|
355
|
+
configurable: true,
|
|
356
|
+
enumerable: on.enumerable ?? true,
|
|
357
|
+
get: like(function() {
|
|
358
|
+
if (typeof this === "object" && this !== null && assigned.has(this)) return assigned.get(this);
|
|
359
|
+
return get.call(this);
|
|
360
|
+
}, get),
|
|
361
|
+
set: like(function(fn) {
|
|
362
|
+
if (typeof this === "object" && this !== null) assigned.set(this, fn);
|
|
363
|
+
const cb = callable(fn);
|
|
364
|
+
set.call(this, cb ? billed(busy, busy.capture(), cb) : fn);
|
|
365
|
+
}, set)
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
const add = callable(Reflect.get(proto, "addEventListener"));
|
|
369
|
+
const remove = callable(Reflect.get(proto, "removeEventListener"));
|
|
370
|
+
if (!add || !remove) return;
|
|
371
|
+
/** Per port, per listener and capture flag: the wrapper `addEventListener` was given. */
|
|
372
|
+
const added = /* @__PURE__ */ new WeakMap();
|
|
373
|
+
Object.defineProperty(proto, "addEventListener", {
|
|
374
|
+
configurable: true,
|
|
375
|
+
writable: true,
|
|
376
|
+
enumerable: true,
|
|
377
|
+
value: like(function(...args) {
|
|
378
|
+
const [type, listener, options] = args;
|
|
379
|
+
if (type !== "message" && type !== "messageerror" || !isListener(listener) || typeof this !== "object" || this === null) return add.apply(this, args);
|
|
380
|
+
const capture = captureOf(options);
|
|
381
|
+
let byListener = added.get(this);
|
|
382
|
+
if (!byListener) added.set(this, byListener = /* @__PURE__ */ new Map());
|
|
383
|
+
let byCapture = byListener.get(listener);
|
|
384
|
+
if (!byCapture) byListener.set(listener, byCapture = /* @__PURE__ */ new Map());
|
|
385
|
+
let wrapper = byCapture.get(capture);
|
|
386
|
+
if (!wrapper) {
|
|
387
|
+
const fn = callable(listener);
|
|
388
|
+
const call = function(e) {
|
|
389
|
+
if (fn) return fn.call(this, e);
|
|
390
|
+
return callable(Reflect.get(listener, "handleEvent"))?.call(listener, e);
|
|
391
|
+
};
|
|
392
|
+
wrapper = billed(busy, busy.capture(), call);
|
|
393
|
+
byCapture.set(capture, wrapper);
|
|
394
|
+
}
|
|
395
|
+
return add.apply(this, [
|
|
396
|
+
type,
|
|
397
|
+
wrapper,
|
|
398
|
+
...args.slice(2)
|
|
399
|
+
]);
|
|
400
|
+
}, add)
|
|
401
|
+
});
|
|
402
|
+
Object.defineProperty(proto, "removeEventListener", {
|
|
403
|
+
configurable: true,
|
|
404
|
+
writable: true,
|
|
405
|
+
enumerable: true,
|
|
406
|
+
value: like(function(...args) {
|
|
407
|
+
const [type, listener, options] = args;
|
|
408
|
+
const byCapture = typeof this === "object" && this !== null && isListener(listener) ? added.get(this)?.get(listener) : void 0;
|
|
409
|
+
const capture = captureOf(options);
|
|
410
|
+
const wrapper = byCapture?.get(capture);
|
|
411
|
+
if (!wrapper || type !== "message" && type !== "messageerror") return remove.apply(this, args);
|
|
412
|
+
byCapture?.delete(capture);
|
|
413
|
+
return remove.apply(this, [
|
|
414
|
+
type,
|
|
415
|
+
wrapper,
|
|
416
|
+
...args.slice(2)
|
|
417
|
+
]);
|
|
418
|
+
}, remove)
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* React's devtools hook, the smallest one react-dom accepts (production
|
|
423
|
+
* builds call it too): it reports each committed root's member, and each
|
|
424
|
+
* root whose passive effects just ran, to the current frame.
|
|
425
|
+
*/
|
|
426
|
+
function installReactHook(scope, busy) {
|
|
427
|
+
const KEY = "__REACT_DEVTOOLS_GLOBAL_HOOK__";
|
|
428
|
+
if (Reflect.get(scope, KEY) !== void 0) return;
|
|
429
|
+
let renderers = 0;
|
|
430
|
+
const committed = (_renderer, root) => {
|
|
431
|
+
if (typeof root !== "object" || root === null) return;
|
|
432
|
+
const key = busy.keyOf(Reflect.get(root, "containerInfo"));
|
|
433
|
+
if (key !== null) busy.report(key);
|
|
434
|
+
};
|
|
435
|
+
Reflect.set(scope, KEY, {
|
|
436
|
+
supportsFiber: true,
|
|
437
|
+
inject: () => ++renderers,
|
|
438
|
+
onCommitFiberRoot: committed,
|
|
439
|
+
onPostCommitFiberRoot: committed,
|
|
440
|
+
onCommitFiberUnmount: () => {},
|
|
441
|
+
checkDCE: () => {}
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Installs attribution on a worker's global, once, before any member loads:
|
|
446
|
+
* the scheduling functions, `MessagePort` listeners and React's hook.
|
|
447
|
+
*/
|
|
448
|
+
function installBusy(scope, busy) {
|
|
449
|
+
const q = Reflect.get(scope, "queueMicrotask");
|
|
450
|
+
if (typeof q === "function") nativeQueueMicrotask = (fn) => {
|
|
451
|
+
Reflect.apply(q, scope, [fn]);
|
|
452
|
+
};
|
|
453
|
+
for (const name of SCHEDULERS) wrapScheduler(scope, name, busy);
|
|
454
|
+
wrapPorts(scope, busy);
|
|
455
|
+
installReactHook(scope, busy);
|
|
456
|
+
}
|
|
457
|
+
var PENDING, SCHEDULERS, captureOf, isListener, nativeQueueMicrotask, busy, BUSY;
|
|
458
|
+
var init_busy = __esmMin((() => {
|
|
459
|
+
PENDING = Symbol("pending");
|
|
460
|
+
SCHEDULERS = [
|
|
461
|
+
"setTimeout",
|
|
462
|
+
"setInterval",
|
|
463
|
+
"requestAnimationFrame",
|
|
464
|
+
"queueMicrotask",
|
|
465
|
+
"requestIdleCallback"
|
|
466
|
+
];
|
|
467
|
+
captureOf = (options) => typeof options === "boolean" ? options : typeof options === "object" && options !== null && Reflect.get(options, "capture") === true;
|
|
468
|
+
isListener = (l) => typeof l === "function" || typeof l === "object" && l !== null && typeof Reflect.get(l, "handleEvent") === "function";
|
|
469
|
+
nativeQueueMicrotask = (fn) => {
|
|
470
|
+
queueMicrotask(fn);
|
|
471
|
+
};
|
|
472
|
+
busy = createBusy();
|
|
473
|
+
BUSY = Symbol.for("isoloop.busy");
|
|
474
|
+
}));
|
|
218
475
|
// src/freeze.ts
|
|
219
476
|
init_dist();
|
|
477
|
+
init_busy();
|
|
220
478
|
/** Constructors whose `prototype` and statics are frozen, if this engine has them. */
|
|
221
479
|
const NAMES = [
|
|
222
480
|
"Object",
|
|
@@ -261,35 +519,86 @@ const NAMES = [
|
|
|
261
519
|
/**
|
|
262
520
|
* Freezing a prototype makes its data properties read-only, and in strict
|
|
263
521
|
* code assigning a property of the same name on an instance then throws (the
|
|
264
|
-
* "override mistake": `this.name = 'MyError'`, `Foo.prototype.toString =
|
|
265
|
-
* As Hardened JS does, each data
|
|
266
|
-
*
|
|
267
|
-
* while the prototype itself
|
|
522
|
+
* "override mistake": `this.name = 'MyError'`, `Foo.prototype.toString = …`,
|
|
523
|
+
* or `Object.assign({}, { constructor: x })`). As Hardened JS does, each data
|
|
524
|
+
* property becomes an accessor whose setter defines the property on the
|
|
525
|
+
* instance instead, so overriding still works while the prototype itself
|
|
526
|
+
* can't change.
|
|
527
|
+
*
|
|
528
|
+
* Except the properties V8 guards with "protectors": while they are still the
|
|
529
|
+
* original data properties, V8 keeps fast paths for iteration (`for…of`,
|
|
530
|
+
* spread, destructuring), species lookups and promise resolution. An accessor
|
|
531
|
+
* returning the same function is enough to turn a protector off for the whole
|
|
532
|
+
* worker: array iteration alone made a pool's app code 1.4–2.5× slower
|
|
533
|
+
* (docs/investigations/plant-one-pool.md). These stay plain frozen data, so
|
|
534
|
+
* overriding one of them on an instance throws in strict code instead
|
|
535
|
+
* (ADR 0001, "Override taming without the protected properties").
|
|
268
536
|
*/
|
|
537
|
+
const ITERATOR = Symbol.iterator;
|
|
538
|
+
/** The protected properties, per prototype, that are left as frozen data. */
|
|
539
|
+
function protectedProperties() {
|
|
540
|
+
const out = /* @__PURE__ */ new Map();
|
|
541
|
+
const add = (o, ...keys) => {
|
|
542
|
+
if (typeof o !== "object" && typeof o !== "function" || o === null) return;
|
|
543
|
+
out.set(o, /* @__PURE__ */ new Set([...out.get(o) ?? [], ...keys]));
|
|
544
|
+
};
|
|
545
|
+
const typedArrayCtor = Object.getPrototypeOf(Int8Array);
|
|
546
|
+
const typedArray = typeof typedArrayCtor === "function" ? Reflect.get(typedArrayCtor, "prototype") : void 0;
|
|
547
|
+
add(Array.prototype, ITERATOR, "constructor");
|
|
548
|
+
add(Object.getPrototypeOf([][Symbol.iterator]()), "next");
|
|
549
|
+
add(Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())), ITERATOR);
|
|
550
|
+
add(typedArray, ITERATOR, "constructor");
|
|
551
|
+
for (const c of [
|
|
552
|
+
Int8Array,
|
|
553
|
+
Uint8Array,
|
|
554
|
+
Uint8ClampedArray,
|
|
555
|
+
Int16Array,
|
|
556
|
+
Uint16Array,
|
|
557
|
+
Int32Array,
|
|
558
|
+
Uint32Array,
|
|
559
|
+
Float32Array,
|
|
560
|
+
Float64Array,
|
|
561
|
+
BigInt64Array,
|
|
562
|
+
BigUint64Array
|
|
563
|
+
]) add(c.prototype, "constructor");
|
|
564
|
+
add(Map.prototype, ITERATOR);
|
|
565
|
+
add(Object.getPrototypeOf((/* @__PURE__ */ new Map())[Symbol.iterator]()), "next");
|
|
566
|
+
add(Set.prototype, ITERATOR);
|
|
567
|
+
add(Object.getPrototypeOf((/* @__PURE__ */ new Set())[Symbol.iterator]()), "next");
|
|
568
|
+
add(String.prototype, ...Reflect.ownKeys(String.prototype));
|
|
569
|
+
add(Object.getPrototypeOf(""[Symbol.iterator]()), "next");
|
|
570
|
+
add(Promise.prototype, "then", "constructor");
|
|
571
|
+
add(RegExp.prototype, ...Reflect.ownKeys(RegExp.prototype));
|
|
572
|
+
return out;
|
|
573
|
+
}
|
|
574
|
+
let skip;
|
|
269
575
|
function tameOverrides(proto) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
576
|
+
skip ??= protectedProperties();
|
|
577
|
+
const keep = skip.get(proto);
|
|
578
|
+
for (const key of Reflect.ownKeys(proto)) if (!keep?.has(key)) tameOverride(proto, key);
|
|
579
|
+
}
|
|
580
|
+
function tameOverride(proto, key) {
|
|
581
|
+
const d = Object.getOwnPropertyDescriptor(proto, key);
|
|
582
|
+
if (!d || !("value" in d) || !d.configurable) return;
|
|
583
|
+
const value = d.value;
|
|
584
|
+
Object.defineProperty(proto, key, {
|
|
585
|
+
get() {
|
|
586
|
+
return value;
|
|
587
|
+
},
|
|
588
|
+
set(v) {
|
|
589
|
+
if (this === proto) throw new TypeError(`Cannot assign to read only property '${String(key)}' of a frozen built-in`);
|
|
590
|
+
if (typeof this !== "object" && typeof this !== "function") return;
|
|
591
|
+
if (this === null) return;
|
|
592
|
+
Object.defineProperty(this, key, {
|
|
593
|
+
value: v,
|
|
594
|
+
writable: true,
|
|
595
|
+
enumerable: true,
|
|
596
|
+
configurable: true
|
|
597
|
+
});
|
|
598
|
+
},
|
|
599
|
+
enumerable: d.enumerable ?? false,
|
|
600
|
+
configurable: false
|
|
601
|
+
});
|
|
293
602
|
}
|
|
294
603
|
function hardenProto(proto) {
|
|
295
604
|
if (typeof proto !== "object" && typeof proto !== "function" || proto === null) return;
|
|
@@ -1888,7 +2197,18 @@ function sharedDom(scope) {
|
|
|
1888
2197
|
* `pooled`, the isolate gets its own root in the worker's shared document
|
|
1889
2198
|
* instead (ADR 0015).
|
|
1890
2199
|
*/
|
|
1891
|
-
function createRender(post, scope, pooled = false) {
|
|
2200
|
+
function createRender(post, scope, pooled = false, member = null) {
|
|
2201
|
+
const owner = member === null ? null : busy.of(member);
|
|
2202
|
+
const schedule = (flush) => {
|
|
2203
|
+
if (owner === null || member === null) {
|
|
2204
|
+
queueMicrotask(flush);
|
|
2205
|
+
return;
|
|
2206
|
+
}
|
|
2207
|
+
busy.report(member);
|
|
2208
|
+
nativeQueueMicrotask(() => {
|
|
2209
|
+
busy.run(owner, flush, void 0, []);
|
|
2210
|
+
});
|
|
2211
|
+
};
|
|
1892
2212
|
let pendingStyles = [];
|
|
1893
2213
|
let pendingHead = [];
|
|
1894
2214
|
/** Head tags already reported: each goes to the host once. */
|
|
@@ -1928,19 +2248,22 @@ function createRender(post, scope, pooled = false) {
|
|
|
1928
2248
|
};
|
|
1929
2249
|
let target;
|
|
1930
2250
|
if (pooled) {
|
|
1931
|
-
const
|
|
2251
|
+
const own = sharedDom(scope).addRoot({
|
|
2252
|
+
onBatch: postPatch,
|
|
2253
|
+
schedule
|
|
2254
|
+
});
|
|
1932
2255
|
headListeners.add(onHead);
|
|
1933
2256
|
target = {
|
|
1934
|
-
...
|
|
2257
|
+
...own,
|
|
1935
2258
|
dispose() {
|
|
1936
2259
|
headListeners.delete(onHead);
|
|
1937
|
-
|
|
2260
|
+
own.dispose();
|
|
1938
2261
|
}
|
|
1939
2262
|
};
|
|
1940
2263
|
} else {
|
|
1941
2264
|
const dom = createDom({
|
|
1942
2265
|
onBatch: postPatch,
|
|
1943
|
-
schedule
|
|
2266
|
+
schedule,
|
|
1944
2267
|
onHead
|
|
1945
2268
|
});
|
|
1946
2269
|
if (scope) install(dom, scope);
|
|
@@ -2007,8 +2330,8 @@ function defineIsolate(def) {
|
|
|
2007
2330
|
};
|
|
2008
2331
|
}
|
|
2009
2332
|
/** Builds the context for `start(ctx)`. `scope` is the worker global the DOM subset installs on. */
|
|
2010
|
-
function createVanillaIsolate(init, post, scope, pooled = false) {
|
|
2011
|
-
const render = init.renderFmt === "none" ? null : createRender(post, scope, pooled);
|
|
2333
|
+
function createVanillaIsolate(init, post, scope, pooled = false, member = null) {
|
|
2334
|
+
const render = init.renderFmt === "none" ? null : createRender(post, scope, pooled, member);
|
|
2012
2335
|
const calls = createHostCalls({
|
|
2013
2336
|
post,
|
|
2014
2337
|
beforeCall: () => render?.flush(),
|
|
@@ -2070,6 +2393,15 @@ function createVanillaIsolate(init, post, scope, pooled = false) {
|
|
|
2070
2393
|
},
|
|
2071
2394
|
keepAwake: () => awake.hold()
|
|
2072
2395
|
};
|
|
2396
|
+
if (member !== null) Object.defineProperty(ctx, BUSY, {
|
|
2397
|
+
value: {
|
|
2398
|
+
claim: (container) => {
|
|
2399
|
+
busy.claim(container, member);
|
|
2400
|
+
},
|
|
2401
|
+
bind: (fn) => busy.bind(fn)
|
|
2402
|
+
},
|
|
2403
|
+
enumerable: false
|
|
2404
|
+
});
|
|
2073
2405
|
Object.defineProperty(ctx, "portalRoot", {
|
|
2074
2406
|
enumerable: false,
|
|
2075
2407
|
get() {
|
|
@@ -2116,6 +2448,7 @@ var REQUIRES;
|
|
|
2116
2448
|
var init_vanilla = __esmMin((() => {
|
|
2117
2449
|
init_dist();
|
|
2118
2450
|
init_bus_client();
|
|
2451
|
+
init_busy();
|
|
2119
2452
|
init_host_calls();
|
|
2120
2453
|
init_keep_awake();
|
|
2121
2454
|
init_worker();
|
|
@@ -2124,6 +2457,7 @@ var init_vanilla = __esmMin((() => {
|
|
|
2124
2457
|
}));
|
|
2125
2458
|
// src/bootstrap.ts
|
|
2126
2459
|
init_dist();
|
|
2460
|
+
init_busy();
|
|
2127
2461
|
let mode = null;
|
|
2128
2462
|
const members = /* @__PURE__ */ new Map();
|
|
2129
2463
|
const postWorker = (msg) => {
|
|
@@ -2187,7 +2521,8 @@ function startHeartbeat(everyMs) {
|
|
|
2187
2521
|
function shutdownMember(m, graceMs, reason) {
|
|
2188
2522
|
if (members.get(m.name) === m) members.delete(m.name);
|
|
2189
2523
|
m.receive = null;
|
|
2190
|
-
const
|
|
2524
|
+
const hook = m.shutdownHook;
|
|
2525
|
+
const done = (hook ? busy.run(busy.of(m.name), hook, void 0, [reason]) : void 0) ?? Promise.resolve();
|
|
2191
2526
|
const grace = new Promise((r) => setTimeout(r, Math.max(0, graceMs - 50)));
|
|
2192
2527
|
Promise.race([done, grace]).finally(() => {
|
|
2193
2528
|
m.post({
|
|
@@ -2203,7 +2538,7 @@ function route(msg) {
|
|
|
2203
2538
|
const m = members.get(mode === "pool" ? msg.isolate : void 0);
|
|
2204
2539
|
if (!m) return;
|
|
2205
2540
|
if (msg.t === "shutdown") shutdownMember(m, msg.p.graceMs, msg.p.reason);
|
|
2206
|
-
else m.receive
|
|
2541
|
+
else if (m.receive) busy.run(busy.of(m.name), m.receive, void 0, [msg]);
|
|
2207
2542
|
}
|
|
2208
2543
|
async function boot(m, init) {
|
|
2209
2544
|
if (init.protocol.min > PROTOCOL.v || init.protocol.max < PROTOCOL.min) {
|
|
@@ -2212,7 +2547,13 @@ async function boot(m, init) {
|
|
|
2212
2547
|
}
|
|
2213
2548
|
startHeartbeat(init.heartbeatMs);
|
|
2214
2549
|
const { createVanillaIsolate } = await Promise.resolve().then(() => (init_vanilla(), vanilla_exports));
|
|
2215
|
-
const runtime =
|
|
2550
|
+
const runtime = busy.run(busy.of(m.name), createVanillaIsolate, void 0, [
|
|
2551
|
+
init,
|
|
2552
|
+
m.post,
|
|
2553
|
+
self,
|
|
2554
|
+
mode === "pool",
|
|
2555
|
+
m.name
|
|
2556
|
+
]);
|
|
2216
2557
|
let mod;
|
|
2217
2558
|
try {
|
|
2218
2559
|
mod = await import(
|
|
@@ -2241,7 +2582,10 @@ async function startIsolate(m, runtime, start, requires) {
|
|
|
2241
2582
|
runtime.receive(msg);
|
|
2242
2583
|
};
|
|
2243
2584
|
m.shutdownHook = (reason) => runtime.shutdown(reason);
|
|
2244
|
-
m.idleReport = () =>
|
|
2585
|
+
m.idleReport = () => ({
|
|
2586
|
+
...runtime.idle(),
|
|
2587
|
+
busyMs: busy.take(m.name)
|
|
2588
|
+
});
|
|
2245
2589
|
m.post({
|
|
2246
2590
|
v: 1,
|
|
2247
2591
|
t: "ready",
|
|
@@ -2253,7 +2597,7 @@ async function startIsolate(m, runtime, start, requires) {
|
|
|
2253
2597
|
}
|
|
2254
2598
|
});
|
|
2255
2599
|
try {
|
|
2256
|
-
await start
|
|
2600
|
+
await busy.run(busy.of(m.name), start, void 0, [runtime.ctx]);
|
|
2257
2601
|
} catch (err) {
|
|
2258
2602
|
fatal(m, `start() threw: ${err instanceof Error ? err.message : String(err)}`);
|
|
2259
2603
|
}
|
|
@@ -2288,6 +2632,7 @@ function onMessage(e) {
|
|
|
2288
2632
|
}
|
|
2289
2633
|
const STARTED = Symbol.for("isoloop.bootstrap");
|
|
2290
2634
|
function startOnce() {
|
|
2635
|
+
installBusy(self, busy);
|
|
2291
2636
|
self.addEventListener("message", onMessage);
|
|
2292
2637
|
}
|
|
2293
2638
|
if (Reflect.get(self, STARTED) === true) {
|
package/dist/worker.js
CHANGED
|
@@ -8,14 +8,14 @@ var __esmMin = (fn, res, err) => () => {
|
|
|
8
8
|
throw err = [e], e;
|
|
9
9
|
}
|
|
10
10
|
};
|
|
11
|
-
var __exportAll = (all,
|
|
12
|
-
let target = {};
|
|
11
|
+
var __exportAll = (all, flags) => {
|
|
12
|
+
let target = Object.setPrototypeOf({}, null);
|
|
13
13
|
for (var name in all) __defProp(target, name, {
|
|
14
14
|
get: all[name],
|
|
15
15
|
enumerable: true
|
|
16
16
|
});
|
|
17
|
-
if (!
|
|
18
|
-
return target;
|
|
17
|
+
if (!(flags & 1)) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
18
|
+
return flags & 2 ? target : Object.preventExtensions(target);
|
|
19
19
|
};
|
|
20
20
|
// ../protocol/dist/index.js
|
|
21
21
|
/**
|
|
@@ -215,39 +215,327 @@ var init_dist = __esmMin((() => {
|
|
|
215
215
|
retention: { kind: "compacted" }
|
|
216
216
|
});
|
|
217
217
|
}));
|
|
218
|
-
// src/
|
|
218
|
+
// src/busy.ts
|
|
219
|
+
function createBusy(now = () => performance.now()) {
|
|
220
|
+
let frame = null;
|
|
221
|
+
const spent = /* @__PURE__ */ new Map();
|
|
222
|
+
const containers = /* @__PURE__ */ new WeakMap();
|
|
223
|
+
const bill = (key, ms) => {
|
|
224
|
+
spent.set(key, (spent.get(key) ?? 0) + ms);
|
|
225
|
+
};
|
|
226
|
+
const settle = (f) => {
|
|
227
|
+
const fallback = f.owner.key === PENDING ? null : f.owner.key;
|
|
228
|
+
if (f.pending) for (const o of f.pending) o.key = fallback;
|
|
229
|
+
if (f.roots && f.roots.length > 0) {
|
|
230
|
+
const unique = [...new Set(f.roots)];
|
|
231
|
+
const share = f.spent / unique.length;
|
|
232
|
+
for (const key of unique) bill(key, share);
|
|
233
|
+
} else if (fallback !== null) bill(fallback, f.spent);
|
|
234
|
+
};
|
|
235
|
+
const busy = {
|
|
236
|
+
run(owner, fn, self, args) {
|
|
237
|
+
const t = now();
|
|
238
|
+
const outer = frame;
|
|
239
|
+
if (outer) outer.spent += t - outer.start;
|
|
240
|
+
const f = {
|
|
241
|
+
owner,
|
|
242
|
+
start: t,
|
|
243
|
+
spent: 0,
|
|
244
|
+
parent: outer,
|
|
245
|
+
roots: null,
|
|
246
|
+
pending: null
|
|
247
|
+
};
|
|
248
|
+
frame = f;
|
|
249
|
+
try {
|
|
250
|
+
return fn.apply(self, args);
|
|
251
|
+
} finally {
|
|
252
|
+
const end = now();
|
|
253
|
+
f.spent += end - f.start;
|
|
254
|
+
frame = outer;
|
|
255
|
+
if (outer) outer.start = end;
|
|
256
|
+
settle(f);
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
capture() {
|
|
260
|
+
const f = frame;
|
|
261
|
+
if (!f) return { key: null };
|
|
262
|
+
const o = { key: PENDING };
|
|
263
|
+
(f.pending ??= []).push(o);
|
|
264
|
+
return o;
|
|
265
|
+
},
|
|
266
|
+
of: (key) => ({ key }),
|
|
267
|
+
bind(fn) {
|
|
268
|
+
const owner = busy.capture();
|
|
269
|
+
return (...args) => busy.run(owner, fn, void 0, args);
|
|
270
|
+
},
|
|
271
|
+
report(key) {
|
|
272
|
+
const f = frame;
|
|
273
|
+
if (!f) return;
|
|
274
|
+
(f.roots ??= []).push(key);
|
|
275
|
+
if (f.pending) {
|
|
276
|
+
for (const o of f.pending) o.key = key;
|
|
277
|
+
f.pending = null;
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
take(key) {
|
|
281
|
+
const ms = spent.get(key) ?? 0;
|
|
282
|
+
spent.delete(key);
|
|
283
|
+
return ms;
|
|
284
|
+
},
|
|
285
|
+
claim(container, key) {
|
|
286
|
+
containers.set(container, key);
|
|
287
|
+
},
|
|
288
|
+
keyOf(container) {
|
|
289
|
+
if (typeof container !== "object" || container === null) return null;
|
|
290
|
+
return containers.has(container) ? containers.get(container) ?? void 0 : null;
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
return busy;
|
|
294
|
+
}
|
|
295
|
+
/** Keeps a wrapper's `name` and `length` the native function's. */
|
|
296
|
+
function like(wrapper, native) {
|
|
297
|
+
Object.defineProperty(wrapper, "name", {
|
|
298
|
+
value: native.name,
|
|
299
|
+
configurable: true
|
|
300
|
+
});
|
|
301
|
+
Object.defineProperty(wrapper, "length", {
|
|
302
|
+
value: native.length,
|
|
303
|
+
configurable: true
|
|
304
|
+
});
|
|
305
|
+
return wrapper;
|
|
306
|
+
}
|
|
307
|
+
/** `f` as a callback, when it is a function: schedulers call any function. */
|
|
308
|
+
function callable(f) {
|
|
309
|
+
return typeof f === "function" ? f : null;
|
|
310
|
+
}
|
|
219
311
|
/**
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* defines the property on the instance instead, so overriding still works
|
|
225
|
-
* while the prototype itself can't change.
|
|
312
|
+
* `cb` run as `owner` when the scheduler calls it, with the scheduler's `this`
|
|
313
|
+
* and arguments. Always in a frame, even for nobody: a shared framework's
|
|
314
|
+
* task (React's scheduler port, set up while its module loaded) still splits
|
|
315
|
+
* among the roots it reports.
|
|
226
316
|
*/
|
|
227
|
-
function
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
317
|
+
function billed(busy, owner, cb) {
|
|
318
|
+
return function(...args) {
|
|
319
|
+
return busy.run(owner, cb, this, args);
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Wraps a scheduling function on `scope` so the callback in argument 0 is
|
|
324
|
+
* billed to the owner current at registration. Everything else (the return
|
|
325
|
+
* value, ids, `clear*`, other arguments) is the native's.
|
|
326
|
+
*/
|
|
327
|
+
function wrapScheduler(scope, name, busy) {
|
|
328
|
+
const native = Reflect.get(scope, name);
|
|
329
|
+
if (typeof native !== "function") return;
|
|
330
|
+
const wrapper = like(function(...args) {
|
|
331
|
+
const cb = callable(args[0]);
|
|
332
|
+
if (cb) args[0] = billed(busy, busy.capture(), cb);
|
|
333
|
+
return Reflect.apply(native, this ?? scope, args);
|
|
334
|
+
}, native);
|
|
335
|
+
Reflect.set(scope, name, wrapper);
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Wraps `MessagePort`'s `onmessage` and its `message` listeners, so a port
|
|
339
|
+
* a member creates (a scheduler's `MessageChannel`, say) bills its messages
|
|
340
|
+
* to that member. `onmessage` still reads back the function that was set,
|
|
341
|
+
* and `removeEventListener` still removes what `addEventListener` added.
|
|
342
|
+
*/
|
|
343
|
+
function wrapPorts(scope, busy) {
|
|
344
|
+
const Port = Reflect.get(scope, "MessagePort");
|
|
345
|
+
if (typeof Port !== "function") return;
|
|
346
|
+
const proto = Reflect.get(Port, "prototype");
|
|
347
|
+
if (typeof proto !== "object" || proto === null) return;
|
|
348
|
+
const on = Object.getOwnPropertyDescriptor(proto, "onmessage");
|
|
349
|
+
const get = callable(on ? Reflect.get(on, "get") : void 0);
|
|
350
|
+
const set = callable(on ? Reflect.get(on, "set") : void 0);
|
|
351
|
+
if (on && get && set) {
|
|
352
|
+
/** What each port's `onmessage` was set to, as read back. */
|
|
353
|
+
const assigned = /* @__PURE__ */ new WeakMap();
|
|
354
|
+
Object.defineProperty(proto, "onmessage", {
|
|
355
|
+
configurable: true,
|
|
356
|
+
enumerable: on.enumerable ?? true,
|
|
357
|
+
get: like(function() {
|
|
358
|
+
if (typeof this === "object" && this !== null && assigned.has(this)) return assigned.get(this);
|
|
359
|
+
return get.call(this);
|
|
360
|
+
}, get),
|
|
361
|
+
set: like(function(fn) {
|
|
362
|
+
if (typeof this === "object" && this !== null) assigned.set(this, fn);
|
|
363
|
+
const cb = callable(fn);
|
|
364
|
+
set.call(this, cb ? billed(busy, busy.capture(), cb) : fn);
|
|
365
|
+
}, set)
|
|
249
366
|
});
|
|
250
367
|
}
|
|
368
|
+
const add = callable(Reflect.get(proto, "addEventListener"));
|
|
369
|
+
const remove = callable(Reflect.get(proto, "removeEventListener"));
|
|
370
|
+
if (!add || !remove) return;
|
|
371
|
+
/** Per port, per listener and capture flag: the wrapper `addEventListener` was given. */
|
|
372
|
+
const added = /* @__PURE__ */ new WeakMap();
|
|
373
|
+
Object.defineProperty(proto, "addEventListener", {
|
|
374
|
+
configurable: true,
|
|
375
|
+
writable: true,
|
|
376
|
+
enumerable: true,
|
|
377
|
+
value: like(function(...args) {
|
|
378
|
+
const [type, listener, options] = args;
|
|
379
|
+
if (type !== "message" && type !== "messageerror" || !isListener(listener) || typeof this !== "object" || this === null) return add.apply(this, args);
|
|
380
|
+
const capture = captureOf(options);
|
|
381
|
+
let byListener = added.get(this);
|
|
382
|
+
if (!byListener) added.set(this, byListener = /* @__PURE__ */ new Map());
|
|
383
|
+
let byCapture = byListener.get(listener);
|
|
384
|
+
if (!byCapture) byListener.set(listener, byCapture = /* @__PURE__ */ new Map());
|
|
385
|
+
let wrapper = byCapture.get(capture);
|
|
386
|
+
if (!wrapper) {
|
|
387
|
+
const fn = callable(listener);
|
|
388
|
+
const call = function(e) {
|
|
389
|
+
if (fn) return fn.call(this, e);
|
|
390
|
+
return callable(Reflect.get(listener, "handleEvent"))?.call(listener, e);
|
|
391
|
+
};
|
|
392
|
+
wrapper = billed(busy, busy.capture(), call);
|
|
393
|
+
byCapture.set(capture, wrapper);
|
|
394
|
+
}
|
|
395
|
+
return add.apply(this, [
|
|
396
|
+
type,
|
|
397
|
+
wrapper,
|
|
398
|
+
...args.slice(2)
|
|
399
|
+
]);
|
|
400
|
+
}, add)
|
|
401
|
+
});
|
|
402
|
+
Object.defineProperty(proto, "removeEventListener", {
|
|
403
|
+
configurable: true,
|
|
404
|
+
writable: true,
|
|
405
|
+
enumerable: true,
|
|
406
|
+
value: like(function(...args) {
|
|
407
|
+
const [type, listener, options] = args;
|
|
408
|
+
const byCapture = typeof this === "object" && this !== null && isListener(listener) ? added.get(this)?.get(listener) : void 0;
|
|
409
|
+
const capture = captureOf(options);
|
|
410
|
+
const wrapper = byCapture?.get(capture);
|
|
411
|
+
if (!wrapper || type !== "message" && type !== "messageerror") return remove.apply(this, args);
|
|
412
|
+
byCapture?.delete(capture);
|
|
413
|
+
return remove.apply(this, [
|
|
414
|
+
type,
|
|
415
|
+
wrapper,
|
|
416
|
+
...args.slice(2)
|
|
417
|
+
]);
|
|
418
|
+
}, remove)
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* React's devtools hook, the smallest one react-dom accepts (production
|
|
423
|
+
* builds call it too): it reports each committed root's member, and each
|
|
424
|
+
* root whose passive effects just ran, to the current frame.
|
|
425
|
+
*/
|
|
426
|
+
function installReactHook(scope, busy) {
|
|
427
|
+
const KEY = "__REACT_DEVTOOLS_GLOBAL_HOOK__";
|
|
428
|
+
if (Reflect.get(scope, KEY) !== void 0) return;
|
|
429
|
+
let renderers = 0;
|
|
430
|
+
const committed = (_renderer, root) => {
|
|
431
|
+
if (typeof root !== "object" || root === null) return;
|
|
432
|
+
const key = busy.keyOf(Reflect.get(root, "containerInfo"));
|
|
433
|
+
if (key !== null) busy.report(key);
|
|
434
|
+
};
|
|
435
|
+
Reflect.set(scope, KEY, {
|
|
436
|
+
supportsFiber: true,
|
|
437
|
+
inject: () => ++renderers,
|
|
438
|
+
onCommitFiberRoot: committed,
|
|
439
|
+
onPostCommitFiberRoot: committed,
|
|
440
|
+
onCommitFiberUnmount: () => {},
|
|
441
|
+
checkDCE: () => {}
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Installs attribution on a worker's global, once, before any member loads:
|
|
446
|
+
* the scheduling functions, `MessagePort` listeners and React's hook.
|
|
447
|
+
*/
|
|
448
|
+
function installBusy(scope, busy) {
|
|
449
|
+
const q = Reflect.get(scope, "queueMicrotask");
|
|
450
|
+
if (typeof q === "function") nativeQueueMicrotask = (fn) => {
|
|
451
|
+
Reflect.apply(q, scope, [fn]);
|
|
452
|
+
};
|
|
453
|
+
for (const name of SCHEDULERS) wrapScheduler(scope, name, busy);
|
|
454
|
+
wrapPorts(scope, busy);
|
|
455
|
+
installReactHook(scope, busy);
|
|
456
|
+
}
|
|
457
|
+
var PENDING, SCHEDULERS, captureOf, isListener, nativeQueueMicrotask, busy, BUSY;
|
|
458
|
+
var init_busy = __esmMin((() => {
|
|
459
|
+
PENDING = Symbol("pending");
|
|
460
|
+
SCHEDULERS = [
|
|
461
|
+
"setTimeout",
|
|
462
|
+
"setInterval",
|
|
463
|
+
"requestAnimationFrame",
|
|
464
|
+
"queueMicrotask",
|
|
465
|
+
"requestIdleCallback"
|
|
466
|
+
];
|
|
467
|
+
captureOf = (options) => typeof options === "boolean" ? options : typeof options === "object" && options !== null && Reflect.get(options, "capture") === true;
|
|
468
|
+
isListener = (l) => typeof l === "function" || typeof l === "object" && l !== null && typeof Reflect.get(l, "handleEvent") === "function";
|
|
469
|
+
nativeQueueMicrotask = (fn) => {
|
|
470
|
+
queueMicrotask(fn);
|
|
471
|
+
};
|
|
472
|
+
busy = createBusy();
|
|
473
|
+
BUSY = Symbol.for("isoloop.busy");
|
|
474
|
+
}));
|
|
475
|
+
// src/freeze.ts
|
|
476
|
+
/** The protected properties, per prototype, that are left as frozen data. */
|
|
477
|
+
function protectedProperties() {
|
|
478
|
+
const out = /* @__PURE__ */ new Map();
|
|
479
|
+
const add = (o, ...keys) => {
|
|
480
|
+
if (typeof o !== "object" && typeof o !== "function" || o === null) return;
|
|
481
|
+
out.set(o, /* @__PURE__ */ new Set([...out.get(o) ?? [], ...keys]));
|
|
482
|
+
};
|
|
483
|
+
const typedArrayCtor = Object.getPrototypeOf(Int8Array);
|
|
484
|
+
const typedArray = typeof typedArrayCtor === "function" ? Reflect.get(typedArrayCtor, "prototype") : void 0;
|
|
485
|
+
add(Array.prototype, ITERATOR, "constructor");
|
|
486
|
+
add(Object.getPrototypeOf([][Symbol.iterator]()), "next");
|
|
487
|
+
add(Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())), ITERATOR);
|
|
488
|
+
add(typedArray, ITERATOR, "constructor");
|
|
489
|
+
for (const c of [
|
|
490
|
+
Int8Array,
|
|
491
|
+
Uint8Array,
|
|
492
|
+
Uint8ClampedArray,
|
|
493
|
+
Int16Array,
|
|
494
|
+
Uint16Array,
|
|
495
|
+
Int32Array,
|
|
496
|
+
Uint32Array,
|
|
497
|
+
Float32Array,
|
|
498
|
+
Float64Array,
|
|
499
|
+
BigInt64Array,
|
|
500
|
+
BigUint64Array
|
|
501
|
+
]) add(c.prototype, "constructor");
|
|
502
|
+
add(Map.prototype, ITERATOR);
|
|
503
|
+
add(Object.getPrototypeOf((/* @__PURE__ */ new Map())[Symbol.iterator]()), "next");
|
|
504
|
+
add(Set.prototype, ITERATOR);
|
|
505
|
+
add(Object.getPrototypeOf((/* @__PURE__ */ new Set())[Symbol.iterator]()), "next");
|
|
506
|
+
add(String.prototype, ...Reflect.ownKeys(String.prototype));
|
|
507
|
+
add(Object.getPrototypeOf(""[Symbol.iterator]()), "next");
|
|
508
|
+
add(Promise.prototype, "then", "constructor");
|
|
509
|
+
add(RegExp.prototype, ...Reflect.ownKeys(RegExp.prototype));
|
|
510
|
+
return out;
|
|
511
|
+
}
|
|
512
|
+
function tameOverrides(proto) {
|
|
513
|
+
skip ??= protectedProperties();
|
|
514
|
+
const keep = skip.get(proto);
|
|
515
|
+
for (const key of Reflect.ownKeys(proto)) if (!keep?.has(key)) tameOverride(proto, key);
|
|
516
|
+
}
|
|
517
|
+
function tameOverride(proto, key) {
|
|
518
|
+
const d = Object.getOwnPropertyDescriptor(proto, key);
|
|
519
|
+
if (!d || !("value" in d) || !d.configurable) return;
|
|
520
|
+
const value = d.value;
|
|
521
|
+
Object.defineProperty(proto, key, {
|
|
522
|
+
get() {
|
|
523
|
+
return value;
|
|
524
|
+
},
|
|
525
|
+
set(v) {
|
|
526
|
+
if (this === proto) throw new TypeError(`Cannot assign to read only property '${String(key)}' of a frozen built-in`);
|
|
527
|
+
if (typeof this !== "object" && typeof this !== "function") return;
|
|
528
|
+
if (this === null) return;
|
|
529
|
+
Object.defineProperty(this, key, {
|
|
530
|
+
value: v,
|
|
531
|
+
writable: true,
|
|
532
|
+
enumerable: true,
|
|
533
|
+
configurable: true
|
|
534
|
+
});
|
|
535
|
+
},
|
|
536
|
+
enumerable: d.enumerable ?? false,
|
|
537
|
+
configurable: false
|
|
538
|
+
});
|
|
251
539
|
}
|
|
252
540
|
function hardenProto(proto) {
|
|
253
541
|
if (typeof proto !== "object" && typeof proto !== "function" || proto === null) return;
|
|
@@ -284,7 +572,7 @@ function freezeBuiltins() {
|
|
|
284
572
|
Object.freeze(JSON);
|
|
285
573
|
Object.freeze(Reflect);
|
|
286
574
|
}
|
|
287
|
-
var NAMES;
|
|
575
|
+
var NAMES, ITERATOR, skip;
|
|
288
576
|
var init_freeze = __esmMin((() => {
|
|
289
577
|
NAMES = [
|
|
290
578
|
"Object",
|
|
@@ -326,6 +614,7 @@ var init_freeze = __esmMin((() => {
|
|
|
326
614
|
"BigInt64Array",
|
|
327
615
|
"BigUint64Array"
|
|
328
616
|
];
|
|
617
|
+
ITERATOR = Symbol.iterator;
|
|
329
618
|
}));
|
|
330
619
|
// src/bus-client.ts
|
|
331
620
|
function createBusClient(first, o = {}) {
|
|
@@ -1890,7 +2179,18 @@ function sharedDom(scope) {
|
|
|
1890
2179
|
* `pooled`, the isolate gets its own root in the worker's shared document
|
|
1891
2180
|
* instead (ADR 0015).
|
|
1892
2181
|
*/
|
|
1893
|
-
function createRender(post, scope, pooled = false) {
|
|
2182
|
+
function createRender(post, scope, pooled = false, member = null) {
|
|
2183
|
+
const owner = member === null ? null : busy.of(member);
|
|
2184
|
+
const schedule = (flush) => {
|
|
2185
|
+
if (owner === null || member === null) {
|
|
2186
|
+
queueMicrotask(flush);
|
|
2187
|
+
return;
|
|
2188
|
+
}
|
|
2189
|
+
busy.report(member);
|
|
2190
|
+
nativeQueueMicrotask(() => {
|
|
2191
|
+
busy.run(owner, flush, void 0, []);
|
|
2192
|
+
});
|
|
2193
|
+
};
|
|
1894
2194
|
let pendingStyles = [];
|
|
1895
2195
|
let pendingHead = [];
|
|
1896
2196
|
/** Head tags already reported: each goes to the host once. */
|
|
@@ -1930,19 +2230,22 @@ function createRender(post, scope, pooled = false) {
|
|
|
1930
2230
|
};
|
|
1931
2231
|
let target;
|
|
1932
2232
|
if (pooled) {
|
|
1933
|
-
const
|
|
2233
|
+
const own = sharedDom(scope).addRoot({
|
|
2234
|
+
onBatch: postPatch,
|
|
2235
|
+
schedule
|
|
2236
|
+
});
|
|
1934
2237
|
headListeners.add(onHead);
|
|
1935
2238
|
target = {
|
|
1936
|
-
...
|
|
2239
|
+
...own,
|
|
1937
2240
|
dispose() {
|
|
1938
2241
|
headListeners.delete(onHead);
|
|
1939
|
-
|
|
2242
|
+
own.dispose();
|
|
1940
2243
|
}
|
|
1941
2244
|
};
|
|
1942
2245
|
} else {
|
|
1943
2246
|
const dom = createDom({
|
|
1944
2247
|
onBatch: postPatch,
|
|
1945
|
-
schedule
|
|
2248
|
+
schedule,
|
|
1946
2249
|
onHead
|
|
1947
2250
|
});
|
|
1948
2251
|
if (scope) install(dom, scope);
|
|
@@ -1986,6 +2289,7 @@ function createRender(post, scope, pooled = false) {
|
|
|
1986
2289
|
var nodeId, shared, headListeners;
|
|
1987
2290
|
var init_render = __esmMin((() => {
|
|
1988
2291
|
init_worker();
|
|
2292
|
+
init_busy();
|
|
1989
2293
|
nodeId = (s) => /^\d+$/.test(s) ? Number(s) : void 0;
|
|
1990
2294
|
shared = null;
|
|
1991
2295
|
headListeners = /* @__PURE__ */ new Set();
|
|
@@ -2009,8 +2313,8 @@ function defineIsolate(def) {
|
|
|
2009
2313
|
};
|
|
2010
2314
|
}
|
|
2011
2315
|
/** Builds the context for `start(ctx)`. `scope` is the worker global the DOM subset installs on. */
|
|
2012
|
-
function createVanillaIsolate(init, post, scope, pooled = false) {
|
|
2013
|
-
const render = init.renderFmt === "none" ? null : createRender(post, scope, pooled);
|
|
2316
|
+
function createVanillaIsolate(init, post, scope, pooled = false, member = null) {
|
|
2317
|
+
const render = init.renderFmt === "none" ? null : createRender(post, scope, pooled, member);
|
|
2014
2318
|
const calls = createHostCalls({
|
|
2015
2319
|
post,
|
|
2016
2320
|
beforeCall: () => render?.flush(),
|
|
@@ -2072,6 +2376,15 @@ function createVanillaIsolate(init, post, scope, pooled = false) {
|
|
|
2072
2376
|
},
|
|
2073
2377
|
keepAwake: () => awake.hold()
|
|
2074
2378
|
};
|
|
2379
|
+
if (member !== null) Object.defineProperty(ctx, BUSY, {
|
|
2380
|
+
value: {
|
|
2381
|
+
claim: (container) => {
|
|
2382
|
+
busy.claim(container, member);
|
|
2383
|
+
},
|
|
2384
|
+
bind: (fn) => busy.bind(fn)
|
|
2385
|
+
},
|
|
2386
|
+
enumerable: false
|
|
2387
|
+
});
|
|
2075
2388
|
Object.defineProperty(ctx, "portalRoot", {
|
|
2076
2389
|
enumerable: false,
|
|
2077
2390
|
get() {
|
|
@@ -2118,6 +2431,7 @@ var REQUIRES;
|
|
|
2118
2431
|
var init_vanilla = __esmMin((() => {
|
|
2119
2432
|
init_dist();
|
|
2120
2433
|
init_bus_client();
|
|
2434
|
+
init_busy();
|
|
2121
2435
|
init_host_calls();
|
|
2122
2436
|
init_keep_awake();
|
|
2123
2437
|
init_worker();
|
|
@@ -2163,7 +2477,8 @@ function startHeartbeat(everyMs) {
|
|
|
2163
2477
|
function shutdownMember(m, graceMs, reason) {
|
|
2164
2478
|
if (members.get(m.name) === m) members.delete(m.name);
|
|
2165
2479
|
m.receive = null;
|
|
2166
|
-
const
|
|
2480
|
+
const hook = m.shutdownHook;
|
|
2481
|
+
const done = (hook ? busy.run(busy.of(m.name), hook, void 0, [reason]) : void 0) ?? Promise.resolve();
|
|
2167
2482
|
const grace = new Promise((r) => setTimeout(r, Math.max(0, graceMs - 50)));
|
|
2168
2483
|
Promise.race([done, grace]).finally(() => {
|
|
2169
2484
|
m.post({
|
|
@@ -2179,7 +2494,7 @@ function route(msg) {
|
|
|
2179
2494
|
const m = members.get(mode === "pool" ? msg.isolate : void 0);
|
|
2180
2495
|
if (!m) return;
|
|
2181
2496
|
if (msg.t === "shutdown") shutdownMember(m, msg.p.graceMs, msg.p.reason);
|
|
2182
|
-
else m.receive
|
|
2497
|
+
else if (m.receive) busy.run(busy.of(m.name), m.receive, void 0, [msg]);
|
|
2183
2498
|
}
|
|
2184
2499
|
async function boot(m, init) {
|
|
2185
2500
|
if (init.protocol.min > PROTOCOL.v || init.protocol.max < PROTOCOL.min) {
|
|
@@ -2188,7 +2503,13 @@ async function boot(m, init) {
|
|
|
2188
2503
|
}
|
|
2189
2504
|
startHeartbeat(init.heartbeatMs);
|
|
2190
2505
|
const { createVanillaIsolate } = await Promise.resolve().then(() => (init_vanilla(), vanilla_exports));
|
|
2191
|
-
const runtime =
|
|
2506
|
+
const runtime = busy.run(busy.of(m.name), createVanillaIsolate, void 0, [
|
|
2507
|
+
init,
|
|
2508
|
+
m.post,
|
|
2509
|
+
self,
|
|
2510
|
+
mode === "pool",
|
|
2511
|
+
m.name
|
|
2512
|
+
]);
|
|
2192
2513
|
let mod;
|
|
2193
2514
|
try {
|
|
2194
2515
|
mod = await import(
|
|
@@ -2217,7 +2538,10 @@ async function startIsolate(m, runtime, start, requires) {
|
|
|
2217
2538
|
runtime.receive(msg);
|
|
2218
2539
|
};
|
|
2219
2540
|
m.shutdownHook = (reason) => runtime.shutdown(reason);
|
|
2220
|
-
m.idleReport = () =>
|
|
2541
|
+
m.idleReport = () => ({
|
|
2542
|
+
...runtime.idle(),
|
|
2543
|
+
busyMs: busy.take(m.name)
|
|
2544
|
+
});
|
|
2221
2545
|
m.post({
|
|
2222
2546
|
v: 1,
|
|
2223
2547
|
t: "ready",
|
|
@@ -2229,7 +2553,7 @@ async function startIsolate(m, runtime, start, requires) {
|
|
|
2229
2553
|
}
|
|
2230
2554
|
});
|
|
2231
2555
|
try {
|
|
2232
|
-
await start
|
|
2556
|
+
await busy.run(busy.of(m.name), start, void 0, [runtime.ctx]);
|
|
2233
2557
|
} catch (err) {
|
|
2234
2558
|
fatal(m, `start() threw: ${err instanceof Error ? err.message : String(err)}`);
|
|
2235
2559
|
}
|
|
@@ -2263,11 +2587,13 @@ function onMessage(e) {
|
|
|
2263
2587
|
});
|
|
2264
2588
|
}
|
|
2265
2589
|
function startOnce() {
|
|
2590
|
+
installBusy(self, busy);
|
|
2266
2591
|
self.addEventListener("message", onMessage);
|
|
2267
2592
|
}
|
|
2268
2593
|
var mode, members, postWorker, memberPost, fatal, beating, STARTED;
|
|
2269
2594
|
var init_bootstrap = __esmMin((() => {
|
|
2270
2595
|
init_dist();
|
|
2596
|
+
init_busy();
|
|
2271
2597
|
init_freeze();
|
|
2272
2598
|
mode = null;
|
|
2273
2599
|
members = /* @__PURE__ */ new Map();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@isoloop/runtime",
|
|
3
|
-
"version": "0.0.0-next.
|
|
3
|
+
"version": "0.0.0-next.g3322248",
|
|
4
4
|
"description": "Isolate-side runtime that runs inside each worker.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@isoloop/dom": "0.0.0-next.
|
|
39
|
-
"@isoloop/protocol": "0.0.0-next.
|
|
38
|
+
"@isoloop/dom": "0.0.0-next.g3322248",
|
|
39
|
+
"@isoloop/protocol": "0.0.0-next.g3322248"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"typescript": "7.0.2"
|