@async/framework 0.11.9 → 0.11.11

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/server.js CHANGED
@@ -133,9 +133,9 @@ const __asyncSignalModule = (() => {
133
133
 
134
134
  snapshot() {
135
135
  return {
136
- value,
136
+ value: value === undefined && error !== null ? null : value,
137
137
  loading,
138
- error,
138
+ error: serializeAsyncError(error),
139
139
  status,
140
140
  version
141
141
  };
@@ -152,7 +152,7 @@ const __asyncSignalModule = (() => {
152
152
  cancelCurrentRun(new Error(`Async signal "${registeredId}" restored from snapshot.`));
153
153
  value = snapshot.value;
154
154
  loading = Boolean(snapshot.loading);
155
- error = snapshot.error ?? null;
155
+ error = restoreAsyncError(snapshot.error);
156
156
  status = typeof snapshot.status === "string" ? snapshot.status : inferStatus({ value, loading, error });
157
157
  if (Number.isFinite(snapshot.version)) {
158
158
  version = snapshot.version;
@@ -404,6 +404,51 @@ const __asyncSignalModule = (() => {
404
404
  return value === undefined ? "idle" : "ready";
405
405
  }
406
406
 
407
+ function serializeAsyncError(value) {
408
+ if (value == null) {
409
+ return null;
410
+ }
411
+
412
+ const record = {
413
+ name: readErrorName(value),
414
+ message: readErrorMessage(value)
415
+ };
416
+ const code = readErrorCode(value);
417
+ if (code !== undefined) {
418
+ record.code = code;
419
+ }
420
+ return record;
421
+ }
422
+
423
+ function restoreAsyncError(value) {
424
+ return serializeAsyncError(value);
425
+ }
426
+
427
+ function readErrorName(value) {
428
+ if (value && typeof value === "object" && typeof value.name === "string" && value.name.length > 0) {
429
+ return value.name;
430
+ }
431
+ return "Error";
432
+ }
433
+
434
+ function readErrorMessage(value) {
435
+ if (value instanceof Error) {
436
+ return value.message;
437
+ }
438
+ if (value && typeof value === "object" && typeof value.message === "string") {
439
+ return value.message;
440
+ }
441
+ return String(value);
442
+ }
443
+
444
+ function readErrorCode(value) {
445
+ if (!value || typeof value !== "object" || !Object.hasOwn(value, "code")) {
446
+ return undefined;
447
+ }
448
+ const code = value.code;
449
+ return typeof code === "string" || typeof code === "number" ? code : undefined;
450
+ }
451
+
407
452
  function attachCancel(signal, controller, onCancel) {
408
453
  Object.defineProperty(signal, "cancel", {
409
454
  configurable: true,
@@ -3241,7 +3286,7 @@ const __schedulerModule = (() => {
3241
3286
  scheduled = true;
3242
3287
  scheduleMicrotask(() => {
3243
3288
  if (!destroyed) {
3244
- void api.flush();
3289
+ void api.flush().catch(reportAutomaticFlushError);
3245
3290
  }
3246
3291
  });
3247
3292
  }
@@ -3277,12 +3322,22 @@ const __schedulerModule = (() => {
3277
3322
  if (onError) {
3278
3323
  onError(error, job);
3279
3324
  } else {
3280
- throw error;
3325
+ throw annotateSchedulerError(error, job);
3281
3326
  }
3282
3327
  }
3283
3328
  }
3284
3329
  }
3285
3330
 
3331
+ function reportAutomaticFlushError(error) {
3332
+ if (typeof globalThis.reportError === "function") {
3333
+ globalThis.reportError(error);
3334
+ return;
3335
+ }
3336
+ setTimeout(() => {
3337
+ throw error;
3338
+ }, 0);
3339
+ }
3340
+
3286
3341
  function hasJobs() {
3287
3342
  for (const queue of queues.values()) {
3288
3343
  if (queue.some((job) => !job.canceled)) {
@@ -3341,6 +3396,25 @@ const __schedulerModule = (() => {
3341
3396
  return (typeof scope === "object" && scope !== null) || typeof scope === "function";
3342
3397
  }
3343
3398
 
3399
+ function annotateSchedulerError(error, job) {
3400
+ if (!error || (typeof error !== "object" && typeof error !== "function")) {
3401
+ return error;
3402
+ }
3403
+ try {
3404
+ Object.defineProperty(error, "scheduler", {
3405
+ configurable: true,
3406
+ value: {
3407
+ phase: job.phase,
3408
+ scope: job.scope,
3409
+ key: job.key
3410
+ }
3411
+ });
3412
+ } catch {
3413
+ // Non-extensible thrown values still need to propagate through the chosen error channel.
3414
+ }
3415
+ return error;
3416
+ }
3417
+
3344
3418
  function scheduleMicrotask(fn) {
3345
3419
  if (typeof queueMicrotask === "function") {
3346
3420
  queueMicrotask(fn);