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

refactor(v3): subscribe-handlers

上级 b616d099
......@@ -6246,7 +6246,10 @@ var serviceContext = (function () {
function createPreloadWebview () {
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
}
......@@ -6311,6 +6314,9 @@ var serviceContext = (function () {
{
// 创建预加载
const preloadWebview = createPreloadWebview();
if (process.env.NODE_ENV !== 'production') {
console.log(`navigateFinish.preloadWebview:${preloadWebview.id}`);
}
if (!todoNavigator) {
return
}
......@@ -8730,6 +8736,9 @@ var serviceContext = (function () {
}
function onWebviewReady (data, pageId) {
if (process.env.NODE_ENV !== 'production') {
console.log('[uni-app] onWebviewReady.preloadWebview' + (preloadWebview && preloadWebview.id));
}
const isLaunchWebview = pageId === '1';
if (isLaunchWebview) { // 首页
setPreloadWebview(plus.webview.getLaunchWebview());
......@@ -8737,7 +8746,7 @@ var serviceContext = (function () {
setPreloadWebview(plus.webview.getWebviewById(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
......@@ -8754,8 +8763,8 @@ var serviceContext = (function () {
})
}
}
}
}
const vdSyncHandlers = Object.create(null);
function registerVdSync (pageId, callback) {
......@@ -8778,8 +8787,8 @@ var serviceContext = (function () {
} else {
console.error(`vdSync[${pageId}] not found`);
}
}
}
const vdSyncCallbacks = []; // 数据同步 callback
function onVdSyncCallback () {
......@@ -8788,8 +8797,8 @@ var serviceContext = (function () {
for (let i = 0; i < copies.length; i++) {
copies[i]();
}
}
}
function initSubscribeHandlers () {
const {
subscribe,
......@@ -9001,7 +9010,7 @@ var serviceContext = (function () {
const handleVdData = {
[UI_EVENT]: function onUIEvent (vdBatchEvent, vd) {
vdBatchEvent.forEach(([cid, nid, event]) => {
vdBatchEvent.forEach(([cid, nid, event]) => {
nid = String(nid);
console.log(`[EVENT]`, cid, nid, event);
event.preventDefault = noop;
......
......@@ -339,7 +339,8 @@ module.exports = function (pagesJson, userManifestJson) {
const uniApp = require('../../../package.json')['uni-app']
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'].renderer = appJson.renderer
if (flexDir) {
......
......@@ -52,6 +52,9 @@ export function navigateFinish () {
if (__PLATFORM__ === 'app-plus') {
// 创建预加载
const preloadWebview = createPreloadWebview()
if (process.env.NODE_ENV !== 'production') {
console.log(`navigateFinish.preloadWebview:${preloadWebview.id}`)
}
if (!todoNavigator) {
return
}
......
......@@ -10,14 +10,17 @@ import {
} from '../../../constants'
import {
vdSyncCallbacks,
removeVdSync,
registerVdSync
} from '../subscribe-handlers'
} from '../subscribe-handlers/on-vd-sync'
import {
vdSyncCallbacks
} from '../subscribe-handlers/on-vd-sync-callback'
const handleVdData = {
[UI_EVENT]: function onUIEvent (vdBatchEvent, vd) {
vdBatchEvent.forEach(([cid, nid, event]) => {
vdBatchEvent.forEach(([cid, nid, event]) => {
nid = String(nid)
console.log(`[EVENT]`, cid, nid, event)
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 {
preloadWebview,
setPreloadWebview,
consumeWebviewReady
} from './webview'
import {
registerPlusMessage
} from './plus-message'
} from '../webview'
import {
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'
if (isLaunchWebview) { // 首页
setPreloadWebview(plus.webview.getLaunchWebview())
......@@ -26,7 +19,7 @@ function onWebviewReady (data, pageId) {
setPreloadWebview(plus.webview.getWebviewById(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
......@@ -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) {
export function createPreloadWebview () {
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
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册