Commit c23ef591 authored by nanahira's avatar nanahira

fix createEntryFromRecord async

parent e7283a63
......@@ -12,6 +12,7 @@ lerna-debug.log*
# OS
.DS_Store
._*
# Tests
/coverage
......
......@@ -11,3 +11,5 @@ Dockerfile
/tests
/dist/tests
/build.js
.DS_Store
._*
import { AnyClass, Empty } from '../types';
import { AnyClass, Awaitable, Empty } from '../types';
import {
AppContext,
AppServiceClass,
......@@ -60,7 +60,7 @@ export class AppContextCore<Cur = Empty, Req = Empty> {
return undefined;
}
private createEntryFromRecord(record: ProvideRecord): LoadEntry {
private createEntryFromRecord(record: ProvideRecord): Awaitable<LoadEntry> {
const existing = this.createdEntries?.get(record);
if (existing) {
return existing;
......@@ -82,6 +82,12 @@ export class AppContextCore<Cur = Empty, Req = Empty> {
this.createdEntries?.set(record, entry);
this.registry.set(record.classRef, entry);
this.startingEntries?.push(entry);
if (entry.inst?.then && typeof entry.inst.then === 'function') {
return entry.inst.then((resolvedInst: any) => {
entry.inst = resolvedInst;
return entry;
});
}
return entry;
} finally {
this.loadingRecords.delete(record);
......@@ -217,15 +223,12 @@ export class AppContextCore<Cur = Empty, Req = Empty> {
return entry.inst as any;
}
/**
* @deprecated Use get() instead. getAsync() is no longer needed as all services are loaded synchronously during start().
*/
async getAsync<R>(
cls:
| AppServiceClass<Cur, Req, any, R>
| (() => AppServiceClass<Cur, Req, any, R>),
): Promise<R> {
return this.get(cls);
return await this.get(cls);
}
use<const Ctxes extends AppContext<any, any>[]>(
......@@ -289,14 +292,7 @@ export class AppContextCore<Cur = Empty, Req = Empty> {
if (preloadedKeys.has(record.classRef)) {
continue;
}
this.createEntryFromRecord(record);
}
// Resolve all promises created in this start().
for (const entry of startedEntries) {
if (entry.inst && typeof entry.inst.then === 'function') {
entry.inst = await entry.inst;
}
await this.createEntryFromRecord(record);
}
// Init only instances created in this start().
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment