recovery.ts 1.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import { formatLog } from '@dcloudio/uni-shared'
import { ON_WEBVIEW_READY } from '../../../../../constants'
import UniPageNode from '../../../dom/Page'
import { getPageById } from '../../../page/getCurrentPages'

export function onWebviewRecovery(webview: PlusWebviewWebviewObject) {
  if ((webview as any).nvue) {
    return
  }
  const webviewId = webview.id
  const { subscribe, unsubscribe } = UniServiceJSBridge
  const onWebviewRecoveryReady = (_: unknown, pageId: string) => {
    if (webviewId !== pageId) {
      return
    }
    unsubscribe(ON_WEBVIEW_READY, onWebviewRecoveryReady)
    if (__DEV__) {
      console.log(formatLog(`Recovery`, webviewId, 'ready'))
    }
    const page = getPageById(parseInt(pageId))
    if (page) {
      const pageNode = (page as any).__page_container__ as UniPageNode
      pageNode.restore()
    }
  }

  // @ts-expect-error
  webview.addEventListener('recovery', () => {
    if (__DEV__) {
      console.log(formatLog('Recovery', webview.id))
    }
    subscribe(ON_WEBVIEW_READY, onWebviewRecoveryReady)
  })
}