Skip to content

Commit

Permalink
修改router逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
wangcuijuan committed Jan 14, 2025
1 parent 5c6bdc6 commit 638bc95
Showing 1 changed file with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions packages/api-proxy/src/platform/api/route/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,100 +33,99 @@ function resolvePath (relative, base) {
}
return stack.join('/')
}
function isLock(navigationHelper, type, options) {
if (navigationHelper.lastSuccessCallback && navigationHelper.lastFailCallback) {
const res = { errMsg: `${type}:fail the previous routing event didn't complete` }
failHandle(res, options.fail, options.complete)
return true
}
setTimeout(() => {
if (navigationHelper.lastFailCallback) {
navigationHelper.lastFailCallback('timeout')
navigationHelper.lastFailCallback = null
}
}, 350)
return false
}

let toPending = false
function navigateTo (options = {}) {
if (toPending) {
function navigateTo (options = {}) {
const navigationHelper = global.__navigationHelper
if (isLock(navigationHelper, 'navigateTo', options)) {
return
}
toPending = true
const navigationHelper = global.__navigationHelper
const navigation = Object.values(global.__mpxPagesMap || {})[0]?.[1]
if (navigation && navigationHelper) {
const { path, queryObj } = parseUrl(options.url)
const basePath = getBasePath(navigation)
const finalPath = resolvePath(path, basePath).slice(1)
navigation.push(finalPath, queryObj)
navigationHelper.lastSuccessCallback = () => {
toPending = false
const res = { errMsg: 'navigateTo:ok' }
successHandle(res, options.success, options.complete)
}
navigationHelper.lastFailCallback = (msg) => {
toPending = false
const res = { errMsg: `navigateTo:fail ${msg}` }
failHandle(res, options.fail, options.complete)
}
}
}

let redirectPending = false
function redirectTo (options = {}) {
if (redirectPending) {
return
}
redirectPending = true
const navigation = Object.values(global.__mpxPagesMap || {})[0]?.[1]
const navigationHelper = global.__navigationHelper
if (isLock(navigationHelper, 'redirectTo', options)) {
return
}
if (navigation && navigationHelper) {
const { path, queryObj } = parseUrl(options.url)
const basePath = getBasePath(navigation)
const finalPath = resolvePath(path, basePath).slice(1)
navigation.replace(finalPath, queryObj)
navigationHelper.lastSuccessCallback = () => {
redirectPending = false
const res = { errMsg: 'redirectTo:ok' }
successHandle(res, options.success, options.complete)
}
navigationHelper.lastFailCallback = (msg) => {
redirectPending = false
const res = { errMsg: `redirectTo:fail ${msg}` }
failHandle(res, options.fail, options.complete)
}
}
}

let backPending = false
function navigateBack (options = {}) {
if (backPending) {
return
}
backPending = true
const navigation = Object.values(global.__mpxPagesMap || {})[0]?.[1]
const navigationHelper = global.__navigationHelper
if (isLock(navigationHelper, 'navigateBack', options)) {
return
}
if (navigation && navigationHelper) {
const delta = options.delta || 1
const routeLength = navigation.getState().routes.length
navigationHelper.lastSuccessCallback = () => {
const res = { errMsg: 'navigateBack:ok' }
successHandle(res, options.success, options.complete)
}
navigationHelper.lastFailCallback = (msg) => {
const res = { errMsg: `navigateBack:fail ${msg}` }
failHandle(res, options.fail, options.complete)
}
if (delta >= routeLength && global.__mpx?.config.rnConfig.onAppBack?.(delta - routeLength + 1)) {
nextTick(() => {
backPending = false
const res = { errMsg: 'navigateBack:ok' }
successHandle(res, options.success, options.complete)
})
} else {
navigation.pop(delta)
navigationHelper.lastSuccessCallback = () => {
backPending = false
const res = { errMsg: 'navigateBack:ok' }
successHandle(res, options.success, options.complete)
}
navigationHelper.lastFailCallback = (msg) => {
backPending = false
const res = { errMsg: `navigateBack:fail ${msg}` }
failHandle(res, options.fail, options.complete)
}
}
}
}

let reLaunchPending = false
function reLaunch (options = {}) {
if (reLaunchPending) {
return
}
reLaunchPending = true
const navigation = Object.values(global.__mpxPagesMap || {})[0]?.[1]
const navigationHelper = global.__navigationHelper
if (isLock(navigationHelper, 'reLaunch', options)) {
return
}
if (navigation && navigationHelper) {
const { path, queryObj } = parseUrl(options.url)
const basePath = getBasePath(navigation)
Expand All @@ -141,12 +140,10 @@ function reLaunch (options = {}) {
]
})
navigationHelper.lastSuccessCallback = () => {
reLaunchPending = false
const res = { errMsg: 'redirectTo:ok' }
successHandle(res, options.success, options.complete)
}
navigationHelper.lastFailCallback = (msg) => {
reLaunchPending = false
const res = { errMsg: `redirectTo:fail ${msg}` }
failHandle(res, options.fail, options.complete)
}
Expand Down

0 comments on commit 638bc95

Please sign in to comment.