Created
August 30, 2022 12:27
-
-
Save wplong11/ddecf484bf275136ea410b69acbc80a2 to your computer and use it in GitHub Desktop.
NativeMethodInvoker.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type NativeMethodRequest<TParams> = { | |
invocationId: string; | |
methodName: string; | |
params?: TParams; | |
}; | |
type NativeMethodResponse = { | |
invocationId: string; | |
callbackType: 'resolve' | 'reject'; | |
value?: any; | |
}; | |
const handler = "__NativeMethodInvoker__" | |
export class NativeMethodInvoker { | |
private callbacks: { | |
[invocationId: string]: { | |
resolve: (value: any) => void; | |
reject: (reason?: any) => void; | |
}; | |
} = {}; | |
constructor() { | |
window.addEventListener('message', this.handleWindowMessageEvent); | |
} | |
public close() { | |
window.removeEventListener('message', this.handleWindowMessageEvent); | |
this.callbacks = {}; | |
} | |
public invoke<TParams, TReturn>(methodName: string, params: TParams): Promise<TReturn> { | |
const invocationId = this.generateUUID(); | |
const promise = new Promise<TReturn>((resolve, reject) => { | |
this.callbacks[invocationId] = { resolve, reject }; | |
}); | |
this.post({ | |
invocationId, | |
methodName, | |
params, | |
}); | |
return promise; | |
} | |
private generateUUID(): string { | |
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | |
const random = (Math.random() * 16) | 0; | |
const value = c === 'x' ? random : (random & 0x3) | 0x8; | |
return value.toString(16); | |
}); | |
} | |
private handleWindowMessageEvent = (event: MessageEvent) => { | |
if (event.origin !== window.location.origin) return; | |
const response: NativeMethodResponse = event.data; | |
if (!response.invocationId) return; | |
if (!response.callbackType) return; | |
const callback = this.callbacks[response.invocationId]; | |
delete this.callbacks[response.invocationId]; | |
callback[response.callbackType](response.value); | |
}; | |
private post<TParams>(request: NativeMethodRequest<TParams>) { | |
try { | |
const w: any = window; | |
if (w[handler] && w[handler].handle) { | |
w[handler].handle(JSON.stringify(request)); | |
} else if (w.webkit && w.webkit.messageHandlers[handler]) { | |
w.webkit.messageHandlers[handler].postMessage(request); | |
} | |
} catch (err) { | |
console.error(`Fail to post request\n`, err); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample.tsx