You need to sign in or sign up before continuing.
Commit c23ef591 authored by nanahira's avatar nanahira

fix createEntryFromRecord async

parent e7283a63
...@@ -12,6 +12,7 @@ lerna-debug.log* ...@@ -12,6 +12,7 @@ lerna-debug.log*
# OS # OS
.DS_Store .DS_Store
._*
# Tests # Tests
/coverage /coverage
......
...@@ -10,4 +10,6 @@ Dockerfile ...@@ -10,4 +10,6 @@ Dockerfile
/coverage /coverage
/tests /tests
/dist/tests /dist/tests
/build.js /build.js
\ No newline at end of file .DS_Store
._*
import { AnyClass, Empty } from '../types'; import { AnyClass, Awaitable, Empty } from '../types';
import { import {
AppContext, AppContext,
AppServiceClass, AppServiceClass,
...@@ -60,7 +60,7 @@ export class AppContextCore<Cur = Empty, Req = Empty> { ...@@ -60,7 +60,7 @@ export class AppContextCore<Cur = Empty, Req = Empty> {
return undefined; return undefined;
} }
private createEntryFromRecord(record: ProvideRecord): LoadEntry { private createEntryFromRecord(record: ProvideRecord): Awaitable<LoadEntry> {
const existing = this.createdEntries?.get(record); const existing = this.createdEntries?.get(record);
if (existing) { if (existing) {
return existing; return existing;
...@@ -82,6 +82,12 @@ export class AppContextCore<Cur = Empty, Req = Empty> { ...@@ -82,6 +82,12 @@ export class AppContextCore<Cur = Empty, Req = Empty> {
this.createdEntries?.set(record, entry); this.createdEntries?.set(record, entry);
this.registry.set(record.classRef, entry); this.registry.set(record.classRef, entry);
this.startingEntries?.push(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; return entry;
} finally { } finally {
this.loadingRecords.delete(record); this.loadingRecords.delete(record);
...@@ -217,15 +223,12 @@ export class AppContextCore<Cur = Empty, Req = Empty> { ...@@ -217,15 +223,12 @@ export class AppContextCore<Cur = Empty, Req = Empty> {
return entry.inst as any; 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>( async getAsync<R>(
cls: cls:
| AppServiceClass<Cur, Req, any, R> | AppServiceClass<Cur, Req, any, R>
| (() => AppServiceClass<Cur, Req, any, R>), | (() => AppServiceClass<Cur, Req, any, R>),
): Promise<R> { ): Promise<R> {
return this.get(cls); return await this.get(cls);
} }
use<const Ctxes extends AppContext<any, any>[]>( use<const Ctxes extends AppContext<any, any>[]>(
...@@ -289,14 +292,7 @@ export class AppContextCore<Cur = Empty, Req = Empty> { ...@@ -289,14 +292,7 @@ export class AppContextCore<Cur = Empty, Req = Empty> {
if (preloadedKeys.has(record.classRef)) { if (preloadedKeys.has(record.classRef)) {
continue; continue;
} }
this.createEntryFromRecord(record); await 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;
}
} }
// Init only instances created in this start(). // 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