preload.ts 841 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { formatLog } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
2
import { VIEW_WEBVIEW_PATH } from '../../constants'
fxy060608's avatar
fxy060608 已提交
3 4
import { genWebviewId } from './utils'

fxy060608's avatar
fxy060608 已提交
5 6 7 8
export let preloadWebview: PlusWebviewWebviewObject & {
  loaded?: boolean
  __uniapp_route?: string
}
fxy060608's avatar
fxy060608 已提交
9 10 11 12 13

export function setPreloadWebview(webview: PlusWebviewWebviewObject) {
  preloadWebview = webview
}

fxy060608's avatar
fxy060608 已提交
14 15 16 17
export function getPreloadWebview() {
  return preloadWebview
}

fxy060608's avatar
fxy060608 已提交
18 19 20 21 22
export function createPreloadWebview() {
  if (!preloadWebview || (preloadWebview as any).__uniapp_route) {
    // 不存在,或已被使用
    preloadWebview = plus.webview.create(
      VIEW_WEBVIEW_PATH,
23 24 25
      String(genWebviewId()),
      // @ts-expect-error
      { contentAdjust: false }
fxy060608's avatar
fxy060608 已提交
26 27
    )
    if (__DEV__) {
fxy060608's avatar
fxy060608 已提交
28
      console.log(formatLog('createPreloadWebview', preloadWebview.id))
fxy060608's avatar
fxy060608 已提交
29 30 31 32
    }
  }
  return preloadWebview
}