nfkit 1.0.29 → 1.0.30

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.cjs CHANGED
@@ -1303,6 +1303,45 @@ var AppContextCore = class {
1303
1303
  this.registry = /* @__PURE__ */ new Map();
1304
1304
  this.objectSteps = [];
1305
1305
  this.started = false;
1306
+ this.starting = false;
1307
+ this.startingEntries = null;
1308
+ this.createdRecords = null;
1309
+ this.createdEntries = null;
1310
+ this.loadingRecords = /* @__PURE__ */ new Set();
1311
+ }
1312
+ findProvideRecord(key) {
1313
+ for (let i = 0; i < this.provideRecords.length; i += 1) {
1314
+ const record = this.provideRecords[i];
1315
+ if (record.classRef === key && (!this.createdRecords || !this.createdRecords.has(record))) {
1316
+ return record;
1317
+ }
1318
+ }
1319
+ return void 0;
1320
+ }
1321
+ createEntryFromRecord(record) {
1322
+ const existing = this.createdEntries?.get(record);
1323
+ if (existing) {
1324
+ return existing;
1325
+ }
1326
+ if (this.loadingRecords.has(record)) {
1327
+ throw new Error(
1328
+ `Circular dependency detected while providing: ${record.classRef?.name ?? "UnknownService"}`
1329
+ );
1330
+ }
1331
+ this.loadingRecords.add(record);
1332
+ try {
1333
+ const entry = {
1334
+ classRef: record.classRef,
1335
+ inst: record.factory(this)
1336
+ };
1337
+ this.createdRecords?.add(record);
1338
+ this.createdEntries?.set(record, entry);
1339
+ this.registry.set(record.classRef, entry);
1340
+ this.startingEntries?.push(entry);
1341
+ return entry;
1342
+ } finally {
1343
+ this.loadingRecords.delete(record);
1344
+ }
1306
1345
  }
1307
1346
  provide(cls, ...args) {
1308
1347
  const last = args[args.length - 1];
@@ -1373,6 +1412,12 @@ var AppContextCore = class {
1373
1412
  if (!this.registry.has(key) && typeof cls === "function" && !cls.prototype) {
1374
1413
  key = cls();
1375
1414
  }
1415
+ if (!this.registry.has(key) && this.starting) {
1416
+ const record = this.findProvideRecord(key);
1417
+ if (record) {
1418
+ this.createEntryFromRecord(record);
1419
+ }
1420
+ }
1376
1421
  if (!this.registry.has(key)) {
1377
1422
  throw new Error(`Service not provided: ${key?.name ?? cls.name}`);
1378
1423
  }
@@ -1421,31 +1466,36 @@ var AppContextCore = class {
1421
1466
  }
1422
1467
  const startedEntries = [];
1423
1468
  const preloadedKeys = new Set(this.registry.keys());
1424
- for (const record of this.provideRecords) {
1425
- if (preloadedKeys.has(record.classRef)) {
1426
- continue;
1469
+ this.starting = true;
1470
+ this.startingEntries = startedEntries;
1471
+ this.createdRecords = /* @__PURE__ */ new Set();
1472
+ this.createdEntries = /* @__PURE__ */ new Map();
1473
+ try {
1474
+ for (const record of this.provideRecords) {
1475
+ if (preloadedKeys.has(record.classRef)) {
1476
+ continue;
1477
+ }
1478
+ this.createEntryFromRecord(record);
1427
1479
  }
1428
- const inst = record.factory(this);
1429
- const entry = {
1430
- classRef: record.classRef,
1431
- inst
1432
- };
1433
- this.registry.set(record.classRef, entry);
1434
- startedEntries.push(entry);
1435
- }
1436
- for (const entry of startedEntries) {
1437
- if (entry.inst && typeof entry.inst.then === "function") {
1438
- entry.inst = await entry.inst;
1480
+ for (const entry of startedEntries) {
1481
+ if (entry.inst && typeof entry.inst.then === "function") {
1482
+ entry.inst = await entry.inst;
1483
+ }
1439
1484
  }
1440
- }
1441
- for (const entry of startedEntries) {
1442
- const inst = entry.inst;
1443
- if (inst && typeof inst.init === "function") {
1444
- await inst.init();
1485
+ for (const entry of startedEntries) {
1486
+ const inst = entry.inst;
1487
+ if (inst && typeof inst.init === "function") {
1488
+ await inst.init();
1489
+ }
1445
1490
  }
1491
+ this.started = true;
1492
+ return this;
1493
+ } finally {
1494
+ this.createdEntries = null;
1495
+ this.createdRecords = null;
1496
+ this.startingEntries = null;
1497
+ this.starting = false;
1446
1498
  }
1447
- this.started = true;
1448
- return this;
1449
1499
  }
1450
1500
  };
1451
1501
  var createAppContext = () => new AppContextCore();