feat(ios): addDelegateHandler to add App Delegate handlers (#10371)

This commit is contained in:
Igor Randjelovic
2023-09-02 05:37:31 +02:00
committed by GitHub
parent 8d25d251cd
commit a959a797df
3 changed files with 54 additions and 0 deletions

View File

@ -74,6 +74,7 @@ class Responder extends UIResponder implements UIApplicationDelegate {
export class iOSApplication extends ApplicationCommon implements IiOSApplication {
private _delegate: UIApplicationDelegate;
private _delegateHandlers = new Map<string, Array<Function>>();
private _window: UIWindow;
private _notificationObservers: NotificationObserver[] = [];
private _rootView: View;
@ -255,6 +256,44 @@ export class iOSApplication extends ApplicationCommon implements IiOSApplication
}
}
addDelegateHandler<T extends keyof UIApplicationDelegate>(methodName: T, handler: (typeof UIApplicationDelegate.prototype)[T]): void {
// safe-guard against invalid handlers
if (typeof handler !== 'function') {
return;
}
// ensure we have a delegate
this.delegate ??= Responder as any;
const handlers = this._delegateHandlers.get(methodName) ?? [];
if (!this._delegateHandlers.has(methodName)) {
const originalHandler = this.delegate.prototype[methodName];
if (originalHandler) {
// if there is an original handler, we add it to the handlers array to be called first.
handlers.push(originalHandler as Function);
}
// replace the original method implementation with one that will call all handlers.
this.delegate.prototype[methodName] = function (...args: any[]) {
let res: any;
for (const handler of handlers) {
if (typeof handler !== 'function') {
continue;
}
res = handler.apply(this, args);
}
return res;
} as (typeof UIApplicationDelegate.prototype)[T];
// store the handlers
this._delegateHandlers.set(methodName, handlers);
}
handlers.push(handler);
}
getNativeApplication() {
return this.nativeApp;
}