Skip to content

Commit

Permalink
feat(di): provideAtEnd factories introduced which will be executed af…
Browse files Browse the repository at this point in the history
…ter bootstrap is done
  • Loading branch information
Stradivario committed Apr 3, 2024
1 parent c9f2854 commit 087f97e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"editor.formatOnSave": true,
"[javascript]": {
"editor.formatOnSave": true
"editor.formatOnSave": false
},
"[typescript]": {
"editor.formatOnSave": true
"editor.formatOnSave": false
},
"eslint.autoFixOnSave": true,
"eslint.validate": [
Expand Down
20 changes: 9 additions & 11 deletions packages/di/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class InjectionToken<T> {}
let C = new WeakMap<ObjectUnion>();

const safeHandle = <T>(c: ObjectType<T>) =>
c.prototype && c.prototype.constructor ? new c() : c;
c.prototype?.constructor ? new c() : c;

export function get<T>(c: ObjectUnion<T>): T;
export function get<T>(c: ObjectType<T>): T {
Expand All @@ -43,7 +43,7 @@ export interface SystemComponent {
export function remove<T>(c: T | ObjectType<T>): boolean;
export function remove<T>(c: ObjectType<T>) {
const e = get(c as unknown) as SystemComponent;
if (e && e.OnDestroy) {
if (e?.OnDestroy) {
e.OnDestroy();
}
return C.delete(c);
Expand Down Expand Up @@ -104,33 +104,31 @@ const defineMetaInjectors = (

const getReflection = <T>(Base: ObjectType<T>) =>
(
(Reflect['getMetadata'] &&
(Reflect['getMetadata'](
'design:paramtypes',
Base
) as ObjectUnion<T>[])) ||
[]
(Reflect?.['getMetadata']?.(
'design:paramtypes',
Base
) as ObjectUnion<T>[]) || []
).map((identifier, index) => [index, identifier]) as [number, ObjectUnion][];

export const createDecorator =
(options?: Options) =>
<K extends ObjectType>(Base: K) =>
class extends Base {
constructor(...args: any[]) {
if (options && options.before) {
if (options?.before) {
args = options.before(args);
}
if (!args.length) {
defineMetaInjectors(args, getReflection(Base));
defineMetaInjectors(args, Metadata.get(Base));
}
if (options && options.after) {
if (options?.after) {
args = options.after(args);
}
super(...args);
const e = this as unknown as SystemComponent;

if (options && options.meta) {
if (options?.meta) {
options.meta.call(this);
}
if (e.OnInit) {
Expand Down
22 changes: 16 additions & 6 deletions packages/di/src/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export const Module = <T>(
export type WithProviders<T = unknown> = ObjectUnion<T> & {
provide: T | (string & InjectionToken<unknown>);
deps?: (T | (string & InjectionToken<unknown>))[];
useFactory: (...args) => any;
useFactory: (...args: T[]) => any;
provideAtEnd?: boolean;
};

export interface ModuleWithProviders<T = any> {
Expand All @@ -73,12 +74,21 @@ export interface ExtendedFunction<T> {
forRoot?: (...args: any[]) => ModuleWithProviders<T>;
}

async function setProviders(providers: WithProviders[]) {
for (const { useFactory, deps, provide } of providers) {
set(await useFactory(...(deps ?? []).map(set)), provide)
}
}

export async function Bootstrap<T>(app: T) {
setImport(app as unknown as ModuleWithProviders);
await Promise.all(
[...ProvidersMetadata.values()].map(async (value) =>
set(await value.useFactory(...(value.deps || []).map(set)), value.provide)
)
);

const providers = [...ProvidersMetadata.values()];

await setProviders(providers.filter(p => !p.provideAtEnd));

[...BootstrapsMetadata.values()].map((value) => set(value));

await setProviders(providers.filter(p => p.provideAtEnd));

}
10 changes: 8 additions & 2 deletions packages/di/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"esModuleInterop": true,
"target": "es6",
"outDir": "dist",
"typeRoots": ["node_modules/@types", "./typings.d.ts"],
"typeRoots": [
"node_modules/@types",
"./typings.d.ts"
],
"lib": [
"es2015",
"es2016",
Expand All @@ -23,5 +26,8 @@
"include": [
"./typings.d.ts",
"src/**/*"
],
"exclude": [
"src/**/*.spec.ts"
]
}
}

0 comments on commit 087f97e

Please sign in to comment.