|
@@ -27,21 +27,17 @@ window.postMessage = (message: IPostMessage) => {
|
|
|
type CallBack = (evt?: IPostMessage) => void
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
|
-const loop = () => { }
|
|
|
+const loop = () => {}
|
|
|
|
|
|
const calls: { [key: string]: CallBack | CallBack[] } = {}
|
|
|
|
|
|
const browserInfo = browser()
|
|
|
|
|
|
if (browserInfo.isApp) {
|
|
|
- window.addEventListener('message', evt => {
|
|
|
+ window.addEventListener('message', (evt) => {
|
|
|
try {
|
|
|
console.log('message', evt.data)
|
|
|
- const data = evt.data
|
|
|
- ? typeof evt.data === 'object'
|
|
|
- ? evt.data
|
|
|
- : JSON.parse(evt.data)
|
|
|
- : {}
|
|
|
+ const data = evt.data ? (typeof evt.data === 'object' ? evt.data : JSON.parse(evt.data)) : {}
|
|
|
const uuid = data.content?.uuid || data.uuid
|
|
|
// console.log(uuid, data.content, 'uuid')
|
|
|
try {
|
|
@@ -55,9 +51,7 @@ if (browserInfo.isApp) {
|
|
|
// console.log('data', data)
|
|
|
}
|
|
|
if (!uuid) {
|
|
|
- const keys = Object.keys(calls).filter(
|
|
|
- key => key.indexOf(data.api) === 0
|
|
|
- )
|
|
|
+ const keys = Object.keys(calls).filter((key) => key.indexOf(data.api) === 0)
|
|
|
// console.log(keys, 'keys')
|
|
|
// console.log(data, 'data')
|
|
|
for (const key of keys) {
|
|
@@ -85,7 +79,6 @@ export const postMessage = (data: IPostMessage, callback?: CallBack) => {
|
|
|
calls[uuid] = callback || loop
|
|
|
data.content = data.content ? { ...data.content, uuid } : { uuid }
|
|
|
instance.postMessage(JSON.stringify(data))
|
|
|
- console.log('send:', JSON.stringify(data))
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -111,15 +104,13 @@ export const removeListenerMessage = (api: string, callback: CallBack) => {
|
|
|
const uuid = api
|
|
|
if (Array.isArray(calls[uuid])) {
|
|
|
const indexOf = (calls[uuid] as CallBack[]).indexOf(callback)
|
|
|
- ; (calls[uuid] as CallBack[]).splice(indexOf, 1)
|
|
|
+ ;(calls[uuid] as CallBack[]).splice(indexOf, 1)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export const promisefiyPostMessage = (
|
|
|
- data: IPostMessage
|
|
|
-): Promise<IPostMessage | undefined> => {
|
|
|
- return new Promise(resolve => {
|
|
|
- postMessage(data, res => resolve(res))
|
|
|
+export const promisefiyPostMessage = (data: IPostMessage): Promise<IPostMessage | undefined> => {
|
|
|
+ return new Promise((resolve) => {
|
|
|
+ postMessage(data, (res) => resolve(res))
|
|
|
})
|
|
|
}
|