@forgeax/engine-host 0.1.27 → 0.1.28

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.mjs CHANGED
@@ -350,6 +350,30 @@ function modulesFromCatalog(catalog, realm, version = "static") {
350
350
  ...record.digest === void 0 ? {} : { digest: record.digest }
351
351
  }));
352
352
  }
353
+ async function createHostStartup(options = {}) {
354
+ const context = options.context ?? new Context();
355
+ const ownedContext = options.context === void 0;
356
+ const fibers = [];
357
+ let disposed = false;
358
+ try {
359
+ for (const plugin of options.startupPlugins ?? []) fibers.push(await context.plugin(plugin));
360
+ } catch (error) {
361
+ for (const fiber of fibers.reverse()) await fiber.dispose();
362
+ if (ownedContext) await context.fiber.dispose();
363
+ throw error;
364
+ }
365
+ return {
366
+ context,
367
+ ownedContext,
368
+ fibers,
369
+ async dispose() {
370
+ if (disposed) return;
371
+ disposed = true;
372
+ for (const fiber of [...fibers].reverse()) await fiber.dispose();
373
+ if (ownedContext) await context.fiber.dispose();
374
+ }
375
+ };
376
+ }
353
377
 
354
378
  // src/transport.ts
355
379
  var HOST_ASSEMBLY_SERVICE = "host/assembly.get";
@@ -973,7 +997,6 @@ function assertBackendCatalogIdentity(catalog, modules) {
973
997
  }
974
998
  async function createBackendHost(options = {}) {
975
999
  const context = options.context ?? new Context();
976
- const ownedContext = options.context === void 0;
977
1000
  const realm = options.realm ?? "engine";
978
1001
  const catalog = options.catalog;
979
1002
  const pairedBackendEntries = options.pairs?.flatMap((pair) => pair.backend === void 0 ? [] : [pair.backend.entry]) ?? [];
@@ -989,7 +1012,7 @@ async function createBackendHost(options = {}) {
989
1012
  });
990
1013
  const checkedInitial = validateHostAssembly(initialAssembly);
991
1014
  if (!checkedInitial.ok) {
992
- if (ownedContext) await context.fiber.dispose();
1015
+ if (options.context === void 0) await context.fiber.dispose();
993
1016
  throw checkedInitial.error;
994
1017
  }
995
1018
  const backendModules = options.pairs === void 0 ? modules : options.pairs.flatMap((pair) => pair.backend === void 0 ? [] : [pair.backend.module]);
@@ -998,18 +1021,21 @@ async function createBackendHost(options = {}) {
998
1021
  const assembly = authority.authority;
999
1022
  const bootstrapRevision = checkedInitial.value.revision;
1000
1023
  const transport = options.transport ?? createHostTransport();
1001
- let foundationFiber;
1024
+ let startup;
1002
1025
  let loaderFiber;
1003
1026
  let loader;
1004
1027
  let backendEntries = options.pairs === void 0 ? options.entries ?? options.assembly?.entries ?? [] : pairedBackendEntries;
1005
- const startupFibers = [];
1006
1028
  let unregisterAssemblyService;
1007
1029
  let unregisterActivationService;
1008
1030
  let unsubscribeAssembly;
1009
1031
  try {
1010
- foundationFiber = await context.plugin(hostFoundationPlugin(assembly, transport));
1011
- for (const plugin of options.startupPlugins ?? [])
1012
- startupFibers.push(await context.plugin(plugin));
1032
+ startup = await createHostStartup({
1033
+ context,
1034
+ startupPlugins: [
1035
+ hostFoundationPlugin(assembly, transport),
1036
+ ...options.startupPlugins ?? []
1037
+ ]
1038
+ });
1013
1039
  if (catalog !== void 0) {
1014
1040
  const bootstrapped = await bootstrapCatalogLoader(context, catalog, realm, {
1015
1041
  catalogDigest: checkedInitial.value.revision,
@@ -1048,9 +1074,7 @@ async function createBackendHost(options = {}) {
1048
1074
  unregisterAssemblyService?.();
1049
1075
  unsubscribeAssembly?.();
1050
1076
  await loaderFiber?.dispose();
1051
- for (const fiber of startupFibers.reverse()) await fiber.dispose();
1052
- await foundationFiber?.dispose();
1053
- if (ownedContext) await context.fiber.dispose();
1077
+ await startup?.dispose();
1054
1078
  throw error;
1055
1079
  }
1056
1080
  let disposed = false;
@@ -1059,7 +1083,7 @@ async function createBackendHost(options = {}) {
1059
1083
  ...loader === void 0 ? {} : { loader },
1060
1084
  assembly,
1061
1085
  transport,
1062
- ownedContext,
1086
+ ownedContext: options.context === void 0,
1063
1087
  async update(nextInput) {
1064
1088
  if (disposed) {
1065
1089
  throw new HostAssemblyError(
@@ -1107,9 +1131,7 @@ async function createBackendHost(options = {}) {
1107
1131
  unregisterAssemblyService?.();
1108
1132
  transport.close();
1109
1133
  await loaderFiber?.dispose();
1110
- for (const fiber of startupFibers.reverse()) await fiber.dispose();
1111
- await foundationFiber?.dispose();
1112
- if (ownedContext) await context.fiber.dispose();
1134
+ await startup?.dispose();
1113
1135
  }
1114
1136
  };
1115
1137
  return host;
@@ -1287,8 +1309,7 @@ async function createFrontendHost(options = {}) {
1287
1309
  return status;
1288
1310
  }
1289
1311
  };
1290
- const startupFibers = [];
1291
- let foundationFiber;
1312
+ let startup;
1292
1313
  let loaderFiber;
1293
1314
  let loader;
1294
1315
  let disposed = false;
@@ -1332,9 +1353,13 @@ async function createFrontendHost(options = {}) {
1332
1353
  });
1333
1354
  }
1334
1355
  try {
1335
- foundationFiber = await context.plugin(hostFoundationPlugin2(state, options.transport));
1336
- for (const plugin of options.startupPlugins ?? [])
1337
- startupFibers.push(await context.plugin(plugin));
1356
+ startup = await createHostStartup({
1357
+ context,
1358
+ startupPlugins: [
1359
+ hostFoundationPlugin2(state, options.transport),
1360
+ ...options.startupPlugins ?? []
1361
+ ]
1362
+ });
1338
1363
  if (options.autoActivate !== false) {
1339
1364
  const host2 = {
1340
1365
  context,
@@ -1370,9 +1395,7 @@ async function createFrontendHost(options = {}) {
1370
1395
  } catch (error) {
1371
1396
  removeTransportDisconnect?.();
1372
1397
  await loaderFiber?.dispose();
1373
- for (const fiber of startupFibers.reverse()) await fiber.dispose();
1374
- await foundationFiber?.dispose();
1375
- if (ownedContext) await context.fiber.dispose();
1398
+ await startup?.dispose();
1376
1399
  throw error;
1377
1400
  }
1378
1401
  const host = {
@@ -1410,9 +1433,7 @@ async function createFrontendHost(options = {}) {
1410
1433
  disposed = true;
1411
1434
  removeTransportDisconnect?.();
1412
1435
  await loaderFiber?.dispose();
1413
- for (const fiber of startupFibers.reverse()) await fiber.dispose();
1414
- await foundationFiber?.dispose();
1415
- if (ownedContext) await context.fiber.dispose();
1436
+ await startup?.dispose();
1416
1437
  status = { state: "disposed", revision: current.revision };
1417
1438
  }
1418
1439
  };
@@ -1468,6 +1489,6 @@ async function activateFrontendHost(host, next, options, realm, getLoader, setLo
1468
1489
  }
1469
1490
  }
1470
1491
 
1471
- export { HOST_ACTIVATION_REPORT_SERVICE, HOST_ASSEMBLY_CHANGED_TOPIC, HOST_ASSEMBLY_SCHEMA_VERSION, HOST_ASSEMBLY_SERVICE, HostAssemblyError, attachHostWebSocketServer, canonicalHostJson, connectHostWebSocket, createBackendHost, createFrontendHost, createHostAssembly, createHostTransport, createHostWebSocketClient, hostRevision, modulesFromCatalog, validateHostAssembly };
1492
+ export { HOST_ACTIVATION_REPORT_SERVICE, HOST_ASSEMBLY_CHANGED_TOPIC, HOST_ASSEMBLY_SCHEMA_VERSION, HOST_ASSEMBLY_SERVICE, HostAssemblyError, attachHostWebSocketServer, canonicalHostJson, connectHostWebSocket, createBackendHost, createFrontendHost, createHostAssembly, createHostStartup, createHostTransport, createHostWebSocketClient, hostRevision, modulesFromCatalog, validateHostAssembly };
1472
1493
  //# sourceMappingURL=index.mjs.map
1473
1494
  //# sourceMappingURL=index.mjs.map