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

feat(v3): add navigator lock

上级 c9677d39
......@@ -20,7 +20,9 @@ import validateParam from './params'
function invokeCallbackHandlerFail (err, apiName, callbackId) {
const errMsg = `${apiName}:fail ${err}`
console.error(errMsg)
if (process.env.NODE_ENV !== 'production') {
console.error(errMsg)
}
if (callbackId === -1) {
throw new Error(errMsg)
}
......@@ -38,6 +40,23 @@ const callbackApiParamTypes = [{
required: true
}]
// 目前已用到的仅这三个
// 完整的可能包含:
// beforeValidate,
// beforeSuccess,
// afterSuccess,
// beforeFail,
// afterFail,
// beforeCancel,
// afterCancel,
// beforeAll,
// afterAll
const IGNORE_KEYS = [
'beforeValidate',
'beforeAll',
'beforeSuccess'
]
function validateParams (apiName, paramsData, callbackId) {
let paramTypes = protocol[apiName]
if (!paramTypes && isCallbackApi(apiName)) {
......@@ -67,7 +86,7 @@ function validateParams (apiName, paramsData, callbackId) {
const keys = Object.keys(paramTypes)
for (let i = 0; i < keys.length; i++) {
if (keys[i] === 'beforeValidate') {
if (IGNORE_KEYS.indexOf(keys[i]) !== -1) {
continue
}
const err = validateParam(keys[i], paramTypes, paramsData)
......@@ -149,6 +168,7 @@ function createApiCallback (apiName, params = {}, extras = {}) {
afterFail,
beforeCancel,
afterCancel,
beforeAll,
afterAll
} = wrapperCallbacks
......@@ -172,6 +192,8 @@ function createApiCallback (apiName, params = {}, extras = {}) {
res.errMsg = apiName + ':fail' + errDetail
}
isFn(beforeAll) && beforeAll(res)
const errMsg = res.errMsg
if (errMsg.indexOf(apiName + ':ok') === 0) {
......@@ -255,6 +277,7 @@ export function wrapperUnimplemented (name) {
function wrapperExtras (name, extras) {
const protocolOptions = protocol[name]
if (protocolOptions) {
isFn(protocolOptions.beforeAll) && (extras.beforeAll = protocolOptions.beforeAll)
isFn(protocolOptions.beforeSuccess) && (extras.beforeSuccess = protocolOptions.beforeSuccess)
}
}
......
......@@ -72,15 +72,30 @@ function createValidator (type) {
// 参数格式化
params.url = encodeQueryString(url)
// 主要拦截目标为用户快速点击时触发的多次跳转,该情况,通常前后 url 是一样的
if (navigatorLock === url) {
return `${navigatorLock} locked`
}
// 至少 onLaunch 之后,再启用lock逻辑(onLaunch之前可能开发者手动调用路由API,来提前跳转)
// enableNavigatorLock 临时开关(不对外开放),避免该功能上线后,有部分情况异常,可以让开发者临时关闭 lock 功能
if (__uniConfig.ready && __uniConfig.enableNavigatorLock !== false) {
navigatorLock = url
}
}
}
let navigatorLock
function createProtocol (type, extras = {}) {
return Object.assign({
url: {
type: String,
required: true,
validator: createValidator(type)
},
beforeAll () {
navigatorLock = ''
}
}, extras)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册