提交 a85a6788 编写于 作者: fxy060608's avatar fxy060608

refactor(v3): subscribe-handlers

上级 b616d099
...@@ -6246,7 +6246,10 @@ var serviceContext = (function () { ...@@ -6246,7 +6246,10 @@ var serviceContext = (function () {
function createPreloadWebview () { function createPreloadWebview () {
if (!preloadWebview || preloadWebview.__uniapp_route) { // 不存在,或已被使用 if (!preloadWebview || preloadWebview.__uniapp_route) { // 不存在,或已被使用
preloadWebview = plus.webview.create(VIEW_WEBVIEW_PATH, String(id++)); preloadWebview = plus.webview.create(VIEW_WEBVIEW_PATH, String(id++));
if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] preloadWebview[${preloadWebview.id}]`);
}
} }
return preloadWebview return preloadWebview
} }
...@@ -6311,6 +6314,9 @@ var serviceContext = (function () { ...@@ -6311,6 +6314,9 @@ var serviceContext = (function () {
{ {
// 创建预加载 // 创建预加载
const preloadWebview = createPreloadWebview(); const preloadWebview = createPreloadWebview();
if (process.env.NODE_ENV !== 'production') {
console.log(`navigateFinish.preloadWebview:${preloadWebview.id}`);
}
if (!todoNavigator) { if (!todoNavigator) {
return return
} }
...@@ -8730,6 +8736,9 @@ var serviceContext = (function () { ...@@ -8730,6 +8736,9 @@ var serviceContext = (function () {
} }
function onWebviewReady (data, pageId) { function onWebviewReady (data, pageId) {
if (process.env.NODE_ENV !== 'production') {
console.log('[uni-app] onWebviewReady.preloadWebview' + (preloadWebview && preloadWebview.id));
}
const isLaunchWebview = pageId === '1'; const isLaunchWebview = pageId === '1';
if (isLaunchWebview) { // 首页 if (isLaunchWebview) { // 首页
setPreloadWebview(plus.webview.getLaunchWebview()); setPreloadWebview(plus.webview.getLaunchWebview());
...@@ -8737,7 +8746,7 @@ var serviceContext = (function () { ...@@ -8737,7 +8746,7 @@ var serviceContext = (function () {
setPreloadWebview(plus.webview.getWebviewById(pageId)); setPreloadWebview(plus.webview.getWebviewById(pageId));
} }
if (preloadWebview.id !== pageId) { if (preloadWebview.id !== pageId) {
return console.error(`webview[${pageId}] not found`) return console.error(`webviewReady[${preloadWebview.id}][${pageId}] not match`)
} }
preloadWebview.loaded = true; // 标记已 ready preloadWebview.loaded = true; // 标记已 ready
...@@ -8754,8 +8763,8 @@ var serviceContext = (function () { ...@@ -8754,8 +8763,8 @@ var serviceContext = (function () {
}) })
} }
} }
} }
const vdSyncHandlers = Object.create(null); const vdSyncHandlers = Object.create(null);
function registerVdSync (pageId, callback) { function registerVdSync (pageId, callback) {
...@@ -8778,8 +8787,8 @@ var serviceContext = (function () { ...@@ -8778,8 +8787,8 @@ var serviceContext = (function () {
} else { } else {
console.error(`vdSync[${pageId}] not found`); console.error(`vdSync[${pageId}] not found`);
} }
} }
const vdSyncCallbacks = []; // 数据同步 callback const vdSyncCallbacks = []; // 数据同步 callback
function onVdSyncCallback () { function onVdSyncCallback () {
...@@ -8788,8 +8797,8 @@ var serviceContext = (function () { ...@@ -8788,8 +8797,8 @@ var serviceContext = (function () {
for (let i = 0; i < copies.length; i++) { for (let i = 0; i < copies.length; i++) {
copies[i](); copies[i]();
} }
} }
function initSubscribeHandlers () { function initSubscribeHandlers () {
const { const {
subscribe, subscribe,
...@@ -9001,7 +9010,7 @@ var serviceContext = (function () { ...@@ -9001,7 +9010,7 @@ var serviceContext = (function () {
const handleVdData = { const handleVdData = {
[UI_EVENT]: function onUIEvent (vdBatchEvent, vd) { [UI_EVENT]: function onUIEvent (vdBatchEvent, vd) {
vdBatchEvent.forEach(([cid, nid, event]) => { vdBatchEvent.forEach(([cid, nid, event]) => {
nid = String(nid); nid = String(nid);
console.log(`[EVENT]`, cid, nid, event); console.log(`[EVENT]`, cid, nid, event);
event.preventDefault = noop; event.preventDefault = noop;
......
...@@ -339,7 +339,8 @@ module.exports = function (pagesJson, userManifestJson) { ...@@ -339,7 +339,8 @@ module.exports = function (pagesJson, userManifestJson) {
const uniApp = require('../../../package.json')['uni-app'] const uniApp = require('../../../package.json')['uni-app']
manifestJson.plus['uni-app'] = uniApp manifestJson.plus['uni-app'] = uniApp
// 控制页类型 // 控制页类型
manifestJson.plus['uni-app'].control = process.env.UNI_USING_V8 ? 'v8' : 'webview' const control = process.env.UNI_USING_V3 ? 'uni-v3' : (process.env.UNI_USING_V8 ? 'v8' : 'webview')
manifestJson.plus['uni-app'].control = control
manifestJson.plus['uni-app'].nvueCompiler = appJson.nvueCompiler manifestJson.plus['uni-app'].nvueCompiler = appJson.nvueCompiler
manifestJson.plus['uni-app'].renderer = appJson.renderer manifestJson.plus['uni-app'].renderer = appJson.renderer
if (flexDir) { if (flexDir) {
......
...@@ -52,6 +52,9 @@ export function navigateFinish () { ...@@ -52,6 +52,9 @@ export function navigateFinish () {
if (__PLATFORM__ === 'app-plus') { if (__PLATFORM__ === 'app-plus') {
// 创建预加载 // 创建预加载
const preloadWebview = createPreloadWebview() const preloadWebview = createPreloadWebview()
if (process.env.NODE_ENV !== 'production') {
console.log(`navigateFinish.preloadWebview:${preloadWebview.id}`)
}
if (!todoNavigator) { if (!todoNavigator) {
return return
} }
......
...@@ -10,14 +10,17 @@ import { ...@@ -10,14 +10,17 @@ import {
} from '../../../constants' } from '../../../constants'
import { import {
vdSyncCallbacks,
removeVdSync, removeVdSync,
registerVdSync registerVdSync
} from '../subscribe-handlers' } from '../subscribe-handlers/on-vd-sync'
import {
vdSyncCallbacks
} from '../subscribe-handlers/on-vd-sync-callback'
const handleVdData = { const handleVdData = {
[UI_EVENT]: function onUIEvent (vdBatchEvent, vd) { [UI_EVENT]: function onUIEvent (vdBatchEvent, vd) {
vdBatchEvent.forEach(([cid, nid, event]) => { vdBatchEvent.forEach(([cid, nid, event]) => {
nid = String(nid) nid = String(nid)
console.log(`[EVENT]`, cid, nid, event) console.log(`[EVENT]`, cid, nid, event)
event.preventDefault = noop event.preventDefault = noop
......
import {
VD_SYNC,
VD_SYNC_CALLBACK,
WEBVIEW_READY
} from '../../../constants'
import {
registerPlusMessage
} from '../plus-message'
import onWebviewReady from './on-webview-ready'
import onVdSync from './on-vd-sync'
import onVdSyncCallback from './on-vd-sync-callback'
export function initSubscribeHandlers () {
const {
subscribe,
subscribeHandler
} = UniServiceJSBridge
registerPlusMessage('subscribeHandler', (data) => {
subscribeHandler(data.type, data.data, data.pageId)
})
// TODO 检测目标 preloadWebview 是否已准备好,因为 preloadWebview 准备好时,此处代码还没执行
subscribe(WEBVIEW_READY, onWebviewReady)
subscribe(VD_SYNC, onVdSync)
subscribe(VD_SYNC_CALLBACK, onVdSyncCallback)
}
export const vdSyncCallbacks = [] // 数据同步 callback
export default function onVdSyncCallback () {
const copies = vdSyncCallbacks.slice(0)
vdSyncCallbacks.length = 0
for (let i = 0; i < copies.length; i++) {
copies[i]()
}
}
const vdSyncHandlers = Object.create(null)
export function registerVdSync (pageId, callback) {
(vdSyncHandlers[pageId] || (vdSyncHandlers[pageId] = [])).push(callback)
}
export function removeVdSync (pageId) {
delete vdSyncHandlers[pageId]
}
export default function onVdSync ({
data,
options
}, pageId) {
const handlers = vdSyncHandlers[pageId]
if (Array.isArray(handlers)) {
handlers.forEach(handler => {
handler(data)
})
} else {
console.error(`vdSync[${pageId}] not found`)
}
}
import {
VD_SYNC,
VD_SYNC_CALLBACK,
WEBVIEW_READY
} from '../../constants'
import { import {
preloadWebview, preloadWebview,
setPreloadWebview, setPreloadWebview,
consumeWebviewReady consumeWebviewReady
} from './webview' } from '../webview'
import {
registerPlusMessage
} from './plus-message'
import { import {
perf perf
} from './perf' } from '../perf'
function onWebviewReady (data, pageId) { export default function onWebviewReady (data, pageId) {
if (process.env.NODE_ENV !== 'production') {
console.log('[uni-app] onWebviewReady.preloadWebview' + (preloadWebview && preloadWebview.id))
}
const isLaunchWebview = pageId === '1' const isLaunchWebview = pageId === '1'
if (isLaunchWebview) { // 首页 if (isLaunchWebview) { // 首页
setPreloadWebview(plus.webview.getLaunchWebview()) setPreloadWebview(plus.webview.getLaunchWebview())
...@@ -26,7 +19,7 @@ function onWebviewReady (data, pageId) { ...@@ -26,7 +19,7 @@ function onWebviewReady (data, pageId) {
setPreloadWebview(plus.webview.getWebviewById(pageId)) setPreloadWebview(plus.webview.getWebviewById(pageId))
} }
if (preloadWebview.id !== pageId) { if (preloadWebview.id !== pageId) {
return console.error(`webview[${pageId}] not found`) return console.error(`webviewReady[${preloadWebview.id}][${pageId}] not match`)
} }
preloadWebview.loaded = true // 标记已 ready preloadWebview.loaded = true // 标记已 ready
...@@ -43,54 +36,4 @@ function onWebviewReady (data, pageId) { ...@@ -43,54 +36,4 @@ function onWebviewReady (data, pageId) {
}) })
} }
} }
}
const vdSyncHandlers = Object.create(null)
export function registerVdSync (pageId, callback) {
(vdSyncHandlers[pageId] || (vdSyncHandlers[pageId] = [])).push(callback)
}
export function removeVdSync (pageId) {
delete vdSyncHandlers[pageId]
}
function onVdSync ({
data,
options
}, pageId) {
const handlers = vdSyncHandlers[pageId]
if (Array.isArray(handlers)) {
handlers.forEach(handler => {
handler(data)
})
} else {
console.error(`vdSync[${pageId}] not found`)
}
}
export const vdSyncCallbacks = [] // 数据同步 callback
function onVdSyncCallback () {
const copies = vdSyncCallbacks.slice(0)
vdSyncCallbacks.length = 0
for (let i = 0; i < copies.length; i++) {
copies[i]()
}
}
export function initSubscribeHandlers () {
const {
subscribe,
subscribeHandler
} = UniServiceJSBridge
registerPlusMessage('subscribeHandler', (data) => {
subscribeHandler(data.type, data.data, data.pageId)
})
// TODO 检测目标 preloadWebview 是否已准备好,因为 preloadWebview 准备好时,此处代码还没执行
subscribe(WEBVIEW_READY, onWebviewReady)
subscribe(VD_SYNC, onVdSync)
subscribe(VD_SYNC_CALLBACK, onVdSyncCallback)
} }
...@@ -102,7 +102,10 @@ export function initWebview (webview, routeOptions) { ...@@ -102,7 +102,10 @@ export function initWebview (webview, routeOptions) {
export function createPreloadWebview () { export function createPreloadWebview () {
if (!preloadWebview || preloadWebview.__uniapp_route) { // 不存在,或已被使用 if (!preloadWebview || preloadWebview.__uniapp_route) { // 不存在,或已被使用
preloadWebview = plus.webview.create(VIEW_WEBVIEW_PATH, String(id++)) preloadWebview = plus.webview.create(VIEW_WEBVIEW_PATH, String(id++))
if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] preloadWebview[${preloadWebview.id}]`)
}
} }
return preloadWebview return preloadWebview
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册