Skip to content

Commit a959a79

Browse files
authored
feat(ios): addDelegateHandler to add App Delegate handlers (#10371)
1 parent 8d25d25 commit a959a79

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

apps/automated/src/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ if (Application.ios) {
1212
Application.ios.addNotificationObserver(UIApplicationDidFinishLaunchingNotification, (notification: NSNotification) => {
1313
console.log('UIApplicationDidFinishLaunchingNotification:', notification);
1414
});
15+
16+
// Make sure we can add multiple handlers for the same event.
17+
for (let i = 0; i < 10; i++) {
18+
Application.ios.addDelegateHandler('applicationDidBecomeActive', (application: UIApplication) => {
19+
console.log('applicationDidBecomeActive', i, application);
20+
});
21+
}
1522
}
1623

1724
// Common events for both Android and iOS.

packages/core/application/application.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ export class iOSApplication extends ApplicationCommon {
167167
*/
168168
set delegate(value: UIApplicationDelegate | unknown);
169169

170+
/**
171+
* Adds a delegate handler for the specified delegate method name. This method does not replace an existing handler,
172+
* but rather adds the new handler to the existing chain of handlers.
173+
* @param methodName The name of the delegate method to add a handler for.
174+
* @param handler A function that will be called when the specified delegate method is called.
175+
*/
176+
addDelegateHandler<T extends keyof UIApplicationDelegate>(methodName: T, handler: (typeof UIApplicationDelegate.prototype)[T]): void;
177+
170178
/**
171179
* Adds an observer to the default notification center for the specified notification.
172180
* For more information, please visit 'https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:'

packages/core/application/application.ios.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class Responder extends UIResponder implements UIApplicationDelegate {
7474

7575
export class iOSApplication extends ApplicationCommon implements IiOSApplication {
7676
private _delegate: UIApplicationDelegate;
77+
private _delegateHandlers = new Map<string, Array<Function>>();
7778
private _window: UIWindow;
7879
private _notificationObservers: NotificationObserver[] = [];
7980
private _rootView: View;
@@ -255,6 +256,44 @@ export class iOSApplication extends ApplicationCommon implements IiOSApplication
255256
}
256257
}
257258

259+
addDelegateHandler<T extends keyof UIApplicationDelegate>(methodName: T, handler: (typeof UIApplicationDelegate.prototype)[T]): void {
260+
// safe-guard against invalid handlers
261+
if (typeof handler !== 'function') {
262+
return;
263+
}
264+
265+
// ensure we have a delegate
266+
this.delegate ??= Responder as any;
267+
268+
const handlers = this._delegateHandlers.get(methodName) ?? [];
269+
270+
if (!this._delegateHandlers.has(methodName)) {
271+
const originalHandler = this.delegate.prototype[methodName];
272+
273+
if (originalHandler) {
274+
// if there is an original handler, we add it to the handlers array to be called first.
275+
handlers.push(originalHandler as Function);
276+
}
277+
278+
// replace the original method implementation with one that will call all handlers.
279+
this.delegate.prototype[methodName] = function (...args: any[]) {
280+
let res: any;
281+
for (const handler of handlers) {
282+
if (typeof handler !== 'function') {
283+
continue;
284+
}
285+
res = handler.apply(this, args);
286+
}
287+
return res;
288+
} as (typeof UIApplicationDelegate.prototype)[T];
289+
290+
// store the handlers
291+
this._delegateHandlers.set(methodName, handlers);
292+
}
293+
294+
handlers.push(handler);
295+
}
296+
258297
getNativeApplication() {
259298
return this.nativeApp;
260299
}

0 commit comments

Comments
 (0)