Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
handsomeliuyang authored Apr 17, 2024
2 parents c51e4bc + 134812f commit 83aeca6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@
}
},
"getTopStatus": false,
"getUpdateManager": false,
"getUpdateManager": true,
"getUserCryptoManager": false,
"getUserInfo": {
"object": {
Expand Down Expand Up @@ -1741,7 +1741,11 @@
}
},
"setBackgroundColor": false,
"setBackgroundFetchToken": true,
"setBackgroundFetchToken": {
"object": {
"token": true
}
},
"setBackgroundTextStyle": false,
"setClipboardData": {
"object": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const asyncAndRelease = window.MethodChannel && window.MethodChannel.jsBridgeMod
// @ts-ignore
const asyncAndNotRelease = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: true, autoRelease: false }) || (target => target)

export let judgeUseAxios = false
class NativeApi {
// @ts-ignore
@(syncAndRelease)
Expand Down Expand Up @@ -893,6 +894,7 @@ class HybridProxy {
get (_target: any, prop: string) {
return (...args: any) => {
if (this.useAxios && prop === this.requestApi) {
judgeUseAxios = this.useAxios
// @ts-ignore
return new RequestTask(...args)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { NativeUpdateManager } from '../../interface/NativeUpdateManager'
* 获取全局唯一的版本更新管理器
*
* @canUse getUpdateManager
* @null_implementation
*/
export const getUpdateManager: typeof Taro.getUpdateManager = () => {
// 使用native方法
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Taro from '@tarojs/api'
import { isFunction } from '@tarojs/shared'

import { NativeRequest } from '../../interface/NativeRequest'
import native from '../../NativeApi'
import native, { judgeUseAxios } from '../../NativeApi'
import { getParameterError, shouldBeObject } from '../../utils'

export const _request = (options) => {
Expand Down Expand Up @@ -46,7 +46,7 @@ export const _request = (options) => {
reject(res)
},
})
task = NativeRequest.getRequestTask(taskID)
task = judgeUseAxios ? taskID : NativeRequest.getRequestTask(taskID)
}) as any

result.onHeadersReceived = task.onHeadersReceived.bind(task)
Expand Down
56 changes: 28 additions & 28 deletions packages/taro-platform-harmony-hybrid/src/api/apis/request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { CallbackManager } from './utils/handler'

const axios = require('axios').default

const CancelToken = axios.CancelToken
const source = CancelToken.source()
const callbackManager = {
headersReceived: new CallbackManager()
}

const errMsgMap = new Map([
[401, 'Parameter error'],
Expand All @@ -17,12 +22,11 @@ const errMsgMap = new Map([
[999, 'Unknown Other Error'],
])

let isHeaderReceived = false
export class RequestTask {
public responseHeader
public abortFlag
public fail
public complete
public headersCallback
public result
public res
public interceptor
Expand All @@ -33,11 +37,9 @@ export class RequestTask {
let { data } = object || {}
const { success, fail, complete, dataType } = object || {}

this.responseHeader = null
this.abortFlag = false
this.fail = fail
this.complete = complete
this.headersCallback = new Set()
// 使用axios.create来创建axios实例
this.httpRequest = axios.create({
responseType: responseType || 'text',
Expand Down Expand Up @@ -75,6 +77,9 @@ export class RequestTask {
if (response.config.enableCache === false) {
localStorage.setItem(response.config.url, JSON.stringify(response.data))
}
callbackManager.headersReceived.trigger({
header: response.headers
})
return response
},
(error) => {
Expand Down Expand Up @@ -122,7 +127,6 @@ export class RequestTask {
})
.then((response) => {
if (success && !this.abortFlag) {
this.responseHeader = response.headers
let result = response.result
if (response.config.responseType === 'text') {
if (dataType === 'text') {
Expand Down Expand Up @@ -186,22 +190,20 @@ export class RequestTask {
}

onHeadersReceived (callback) {
if (!callback) {
console.error('[AdvancedAPI] Invalid, callback is null')
return
}
const taskCallback = (header) => {
!this.abortFlag && callback({ header })
}
if (!this.headersCallback.has(callback)) {
this.headersCallback.add(taskCallback)
if (this.httpRequest) {
this.interceptor = this.httpRequest.interceptors.response.use((response) => {
taskCallback(this.responseHeader)
return response
})
if (isHeaderReceived === false) {
const taskCallback = (header) => {
!this.abortFlag && callback({ header })
}
taskCallback(this.responseHeader)
if (!callback) {
console.error('[AdvancedAPI] Invalid, callback is null')
return
}
if (callbackManager) {
isHeaderReceived = true
callbackManager.headersReceived.addUnique(taskCallback)
}
} else {
callbackManager.headersReceived.remove(callback)
}
}

Expand All @@ -210,14 +212,12 @@ export class RequestTask {
* remove all if callback is null, otherwise remove the specialized callback
*/
offHeadersReceived (callback) {
if (this.headersCallback.has(callback)) {
if (this.httpRequest) {
this.httpRequest.interceptors.eject(this.interceptor)
}
this.headersCallback.delete(callback)
} else {
// eslint-disable-next-line no-console
console.debug('offHeadersReceived callback invalid')
if (!callback) {
console.error('Invalid, callback is null')
return
}
if (callbackManager && isHeaderReceived) {
callbackManager.headersReceived.remove(callback)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import native from '../NativeApi'
* 拉取 backgroundFetch 客户端缓存数据
*
* @canUse setBackgroundFetchToken
* @null_implementation
* @__object [token]
*/
export const setBackgroundFetchToken: typeof Taro.setBackgroundFetchToken = function (options) {
const name = 'setBackgroundFetchToken'
Expand Down

0 comments on commit 83aeca6

Please sign in to comment.