Skip to content

Commit 2d1584c

Browse files
Merge pull request #92 from bootgs/max/next
Max/next
2 parents 0761c65 + d0a7f0c commit 2d1584c

2 files changed

Lines changed: 54 additions & 36 deletions

File tree

src/controller/BootApplication.ts

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { isString } from "apps-script-utils";
22
import { ApplicationConfig, AppsScriptMenuProxy, InjectionToken, Newable } from "../domain/types";
33
import { AppsScriptEventType, RequestMethod } from "../domain/enums";
4-
import {
5-
EventDispatcher,
6-
RequestFactory,
7-
Resolver,
8-
ResponseBuilder,
9-
Router,
10-
RouterExplorer
11-
} from "../service";
4+
import { EventDispatcher, RequestFactory, Resolver, ResponseBuilder, Router, RouterExplorer } from "../service";
125

136
/**
147
* Main application class for bootstrapping and handling Google Apps Script events.
@@ -59,6 +52,40 @@ export class BootApplication {
5952
this._eventDispatcher = new EventDispatcher(this._resolver, this._controllers);
6053
}
6154

55+
/**
56+
* Returns a Proxy object that can be used to handle Google Apps Script menu actions.
57+
*
58+
* @returns {AppsScriptMenuProxy} A Proxy object.
59+
*/
60+
public get onMenu(): AppsScriptMenuProxy {
61+
const handler = () => proxy;
62+
63+
const proxy = new Proxy(handler, {
64+
get: (target, prop, receiver) => {
65+
if (!isString(prop)) {
66+
return Reflect.get(target, prop, receiver);
67+
}
68+
69+
if (prop === "inspect") {
70+
return Reflect.get(target, prop, receiver);
71+
}
72+
73+
if (prop === "apply" || prop === "call" || prop === "bind") {
74+
return Reflect.get(target, prop, receiver);
75+
}
76+
77+
return (event: GoogleAppsScript.Events.AppsScriptEvent) => {
78+
return this._eventDispatcher.dispatchByName(prop, event);
79+
};
80+
},
81+
apply: (target, thisArg, argArray) => {
82+
return Reflect.apply(target, thisArg, argArray);
83+
}
84+
}) as unknown as AppsScriptMenuProxy;
85+
86+
return proxy;
87+
}
88+
6289
/**
6390
* Handles Google Apps Script doGet events.
6491
*
@@ -109,6 +136,11 @@ export class BootApplication {
109136
await this._eventDispatcher.dispatch(AppsScriptEventType.EDIT, event);
110137
}
111138

139+
// TODO: onSelectionChange
140+
// public async onSelectionChange(event: GoogleAppsScript.Events.SheetsOnSelectionChange) {
141+
// await this.eventDispatcher.dispatch(AppsScriptEventType.SELECTION_CHANGE, event);
142+
// }
143+
112144
/**
113145
* Handles Google Apps Script onChange events.
114146
*
@@ -119,11 +151,6 @@ export class BootApplication {
119151
await this._eventDispatcher.dispatch(AppsScriptEventType.CHANGE, event);
120152
}
121153

122-
// TODO: onSelectionChange
123-
// public async onSelectionChange(event: GoogleAppsScript.Events.SheetsOnSelectionChange) {
124-
// await this.eventDispatcher.dispatch(AppsScriptEventType.SELECTION_CHANGE, event);
125-
// }
126-
127154
/**
128155
* Handles Google Apps Script onFormSubmit events.
129156
*
@@ -134,29 +161,6 @@ export class BootApplication {
134161
await this._eventDispatcher.dispatch(AppsScriptEventType.FORM_SUBMIT, event);
135162
}
136163

137-
/**
138-
* Returns a Proxy object that can be used to handle Google Apps Script menu actions.
139-
*
140-
* @returns {AppsScriptMenuProxy} A Proxy object.
141-
*/
142-
public onMenu(): AppsScriptMenuProxy {
143-
return new Proxy(this, {
144-
get(target, prop, receiver) {
145-
if (!isString(prop)) {
146-
return Reflect.get(target, prop, receiver);
147-
}
148-
149-
if (prop === "inspect") {
150-
return Reflect.get(target, prop, receiver);
151-
}
152-
153-
return (event: GoogleAppsScript.Events.AppsScriptEvent) => {
154-
return target._eventDispatcher.dispatchByName(prop, event);
155-
};
156-
}
157-
}) as unknown as AppsScriptMenuProxy;
158-
}
159-
160164
/**
161165
* Handles incoming HTTP requests and routes them to the appropriate controller.
162166
*

test/unit/controller/BootApplication/BootApplication.menu.unit.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,18 @@ describe("BootApplication: Menu Handling", () => {
5757

5858
expect(capturedEvent).toBe(event);
5959
});
60+
61+
it("should return proxy when accessed as a property", () => {
62+
const app = new BootApplication({ controllers: [], providers: [] });
63+
const menu = (app as any).onMenu;
64+
expect(typeof menu).toBe("function");
65+
expect(typeof menu.openSidebarHome).toBe("function");
66+
});
67+
68+
it("should return same proxy when called as a function", () => {
69+
const app = new BootApplication({ controllers: [], providers: [] });
70+
const menu = app.onMenu;
71+
const result = (menu as any)();
72+
expect(result).toBe(menu);
73+
});
6074
});

0 commit comments

Comments
 (0)