mirror of
https://github.com/ZiuChen/ZiuChen.github.io.git
synced 2025-12-19 17:25:39 +08:00
feat: 新增 Demo 站点
This commit is contained in:
46
docs/demos/promiseify-post-message/sdk.ts
Normal file
46
docs/demos/promiseify-post-message/sdk.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { FuncMap } from './types'
|
||||
|
||||
export interface JSBridgeParams<T extends keyof FuncMap> {
|
||||
namespace: T
|
||||
action: keyof FuncMap[T]
|
||||
payload: FuncMap[T][keyof FuncMap[T]] extends { payload: infer P } ? P : never
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册 SDK 应用
|
||||
*/
|
||||
export function registerSdk() {
|
||||
window.addEventListener('message', messageHandler)
|
||||
}
|
||||
|
||||
const postMessageCallbackMap = new Map()
|
||||
|
||||
function messageHandler({ data, type }: MessageEvent) {
|
||||
if (type === 'message') {
|
||||
const { params, result } = data
|
||||
const key = [params.namespace, params.action, params.id].join('.')
|
||||
const callback = postMessageCallbackMap.get(key)
|
||||
if (callback) {
|
||||
callback(result)
|
||||
postMessageCallbackMap.delete(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let uniqueId = 0
|
||||
|
||||
export function postMessage<T extends keyof FuncMap>(
|
||||
params: JSBridgeParams<T>
|
||||
): Promise<FuncMap[T][keyof FuncMap[T]] extends { result: infer R } ? R : never> {
|
||||
return new Promise((resolve) => {
|
||||
if (window.parent) {
|
||||
const id = uniqueId++
|
||||
const key = [params.namespace, params.action, id].join('.')
|
||||
postMessageCallbackMap.set(key, resolve)
|
||||
const _params = structuredClone(params)
|
||||
// @ts-expect-error - id is private field.
|
||||
_params.id = id
|
||||
window.parent.postMessage(_params, '*')
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user