Skip to content

Commit 17fcdb4

Browse files
Merge pull request #8 from MaksymStoianov/max/next
fix: controllers
2 parents 282e9e5 + 79339ae commit 17fcdb4

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

src/App.ts

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
isFunctionLike,
3-
isObject,
4-
isString,
5-
normalize
6-
} from "appsscript-utils";
1+
import { isFunctionLike, isObject, isString, isUndefined, normalize } from "appsscript-utils";
72
import {
83
APPSSCRIPT_EVENT_METADATA,
94
APPSSCRIPT_OPTIONS_METADATA,
@@ -41,7 +36,7 @@ import {
4136
export class App {
4237
private static instance: App | null = null;
4338

44-
private readonly _controllers: Newable[] = [];
39+
private readonly _controllers = new Map<Newable, unknown>();
4540
private readonly _providers = new Map<Newable, unknown>();
4641
private readonly _routes: RouteMetadata[] = [];
4742

@@ -56,13 +51,25 @@ export class App {
5651
return App.instance;
5752
}
5853

59-
this._controllers = controllers ?? [];
60-
this._routes = RouterExplorer.explore(controllers ?? []);
54+
for (const controller of controllers ?? []) {
55+
if (!isFunctionLike(controller)) continue;
56+
57+
const isController = Reflect.getMetadata(
58+
CONTROLLER_WATERMARK,
59+
controller
60+
);
61+
62+
if (!isController) continue;
63+
64+
this._controllers.set(controller, null);
65+
}
66+
67+
this._routes = RouterExplorer.explore(this._controllers);
6168

6269
for (const provider of providers ?? []) {
63-
if (isFunctionLike(provider)) {
64-
this._providers.set(provider, undefined);
65-
}
70+
if (!isFunctionLike(provider)) continue;
71+
72+
this._providers.set(provider, null);
6673
}
6774

6875
App.instance = this;
@@ -364,20 +371,9 @@ export class App {
364371
event: any,
365372
eventType: AppsScriptEventType
366373
): void {
367-
for (const ControllerClass of this._controllers) {
368-
const isController = Reflect.getMetadata(
369-
CONTROLLER_WATERMARK,
370-
ControllerClass
371-
);
372-
373-
if (!isController) {
374-
continue;
375-
}
376-
377-
const controllerType: string | undefined = Reflect.getMetadata(
378-
CONTROLLER_TYPE_METADATA,
379-
ControllerClass
380-
);
374+
for (const controller of this._controllers.keys()) {
375+
const controllerType: string | null =
376+
Reflect.getMetadata(CONTROLLER_TYPE_METADATA, controller) || null;
381377

382378
const isAppsScriptController =
383379
isString(controllerType) && controllerType.startsWith("appsscript");
@@ -386,7 +382,7 @@ export class App {
386382
continue;
387383
}
388384

389-
const controllerInstance = this.resolve(ControllerClass);
385+
const controllerInstance = this.resolve(controller);
390386
const prototype = Object.getPrototypeOf(controllerInstance);
391387

392388
const methodNames: string[] = [];
@@ -850,6 +846,14 @@ export class App {
850846
* @returns {T} An instance of the target class with all its dependencies injected.
851847
*/
852848
private resolve<T>(target: Newable<T>): T {
849+
if (this._controllers.has(target)) {
850+
const instance = this._controllers.get(target);
851+
852+
if (instance) {
853+
return instance as T;
854+
}
855+
}
856+
853857
if (this._providers.has(target)) {
854858
const instance = this._providers.get(target);
855859

@@ -875,20 +879,29 @@ export class App {
875879
return this.resolve(tokenToResolve);
876880
});
877881

878-
if (deps.some(dep => dep === undefined)) {
882+
if (deps.some(isUndefined)) {
879883
throw new Error(`Could not resolve all dependencies for ${target.name}`);
880884
}
881885

882886
const instance = new target(...deps);
883887

884888
const isController = Reflect.hasMetadata(CONTROLLER_WATERMARK, target);
889+
890+
if (isController) {
891+
this._controllers.set(target, instance);
892+
} else {
893+
console.warn(
894+
`[Resolve WARN] ${target.name} is not registered as a provider and is not marked @Controller().`
895+
);
896+
}
897+
885898
const isInjectable = Reflect.hasMetadata(INJECTABLE_WATERMARK, target);
886899

887-
if (isController || isInjectable) {
900+
if (isInjectable) {
888901
this._providers.set(target, instance);
889902
} else {
890903
console.warn(
891-
`[Resolve WARN] ${target.name} is not registered as a provider and is not marked @Injectable() or @Controller().`
904+
`[Resolve WARN] ${target.name} is not registered as a provider and is not marked @Injectable().`
892905
);
893906
}
894907

src/RouterExplorer.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,18 @@ import { normalize } from "appsscript-utils";
33
import {
44
CONTROLLER_OPTIONS_METADATA,
55
CONTROLLER_TYPE_METADATA,
6-
CONTROLLER_WATERMARK,
76
METHOD_METADATA,
87
PATH_METADATA
98
} from "./config/constants";
109
import { Newable, RouteMetadata } from "./types";
1110

1211
export class RouterExplorer {
13-
static explore(controllers: Newable[] = []): RouteMetadata[] {
12+
static explore(controllers: Map<Newable, unknown>): RouteMetadata[] {
1413
const routes: RouteMetadata[] = [];
1514

16-
for (const controller of controllers) {
17-
const isController = Reflect.getMetadata(
18-
CONTROLLER_WATERMARK,
19-
controller
20-
);
21-
22-
if (!isController) continue;
23-
24-
const controllerType: string | undefined = Reflect.getMetadata(
25-
CONTROLLER_TYPE_METADATA,
26-
controller
27-
);
15+
for (const controller of controllers.keys()) {
16+
const controllerType: string | null =
17+
Reflect.getMetadata(CONTROLLER_TYPE_METADATA, controller) || null;
2818

2919
const isHttpController = controllerType === "http";
3020

0 commit comments

Comments
 (0)