Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 部分接口注释修以及request接口js实现添加onHeadersReceived和offHeadersReceived监听 #15522

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1754,7 +1754,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 @@ -2,14 +2,15 @@
import { RequestTask } from './request'

// @ts-ignore
const syncAndRelease = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: false, autoRelease: true }) || (target => target)

Check warning on line 5 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 5 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 5 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 5 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 5 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 5 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 5 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 5 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations
// @ts-ignore
const syncAndNotRelease = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: false, autoRelease: false }) || (target => target)

Check warning on line 7 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 7 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 7 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 7 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 7 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 7 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 7 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 7 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations
// @ts-ignore
const asyncAndRelease = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: true, autoRelease: true }) || (target => target)
// @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 @@
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
Loading