Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mall-webview
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
frontend
mall-webview
Commits
8a3af690
Commit
8a3af690
authored
May 21, 2026
by
hewenhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(native-bridge): 增加invokeNativeMethod超时控制功能
为invokeNativeMethod函数添加可选的timeout参数,默认值为10秒, 允许调用者自定义超时时间。当timeout大于0时启用超时机制, 小于等于0时禁用超时。同时修复了定时器清理逻辑, 确保在回调执行时正确清除定时器。
parent
2792a20a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
5 deletions
+10
-5
use-native-bridge.ts
src/util/use-native-bridge.ts
+10
-5
No files found.
src/util/use-native-bridge.ts
View file @
8a3af690
...
@@ -87,20 +87,25 @@ export { useNativeBridge }
...
@@ -87,20 +87,25 @@ export { useNativeBridge }
const
INVOKE_NATIVE_METHOD_TIMEOUT
=
10000
const
INVOKE_NATIVE_METHOD_TIMEOUT
=
10000
function
invokeNativeMethod
<
T
=
any
,
P
=
any
>
(
arg
:
{
type
:
string
;
data
?:
P
}):
Promise
<
T
>
{
function
invokeNativeMethod
<
T
=
any
,
P
=
any
>
(
arg
:
{
type
:
string
;
data
?:
P
}
,
timeout
=
INVOKE_NATIVE_METHOD_TIMEOUT
):
Promise
<
T
>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
callbackName
=
`
${
arg
.
type
}
_callback_
${
Math
.
random
()}
`
.
replace
(
/
\.
/
,
''
)
const
callbackName
=
`
${
arg
.
type
}
_callback_
${
Math
.
random
()}
`
.
replace
(
/
\.
/
,
''
)
let
timer
:
ReturnType
<
typeof
setTimeout
>
|
null
=
null
const
timer
=
setTimeout
(()
=>
{
// 如果 timeout 大于 0,则设置一个定时器,如果超时则拒绝请求
if
(
timeout
>
0
)
{
timer
=
setTimeout
(()
=>
{
reject
(
new
Error
(
`invoke
${
arg
.
type
}
timeout`
))
reject
(
new
Error
(
`invoke
${
arg
.
type
}
timeout`
))
eventBus
.
off
(
GEEK_NATIVE_NAME
,
onCallback
)
eventBus
.
off
(
GEEK_NATIVE_NAME
,
onCallback
)
},
INVOKE_NATIVE_METHOD_TIMEOUT
)
},
timeout
)
}
eventBus
.
on
(
GEEK_NATIVE_NAME
,
onCallback
)
eventBus
.
on
(
GEEK_NATIVE_NAME
,
onCallback
)
function
onCallback
(
params
?:
NativeInvokeFuncParams
)
{
function
onCallback
(
params
?:
NativeInvokeFuncParams
)
{
if
(
params
?.
method
===
callbackName
)
{
if
(
params
?.
method
===
callbackName
)
{
if
(
timer
)
{
clearTimeout
(
timer
)
clearTimeout
(
timer
)
}
if
(
params
.
error
)
{
if
(
params
.
error
)
{
reject
(
new
Error
(
params
.
error
))
reject
(
new
Error
(
params
.
error
))
}
else
{
}
else
{
...
@@ -165,5 +170,5 @@ export interface WechatOpenBusinessViewResp extends WechatBaseResp {
...
@@ -165,5 +170,5 @@ export interface WechatOpenBusinessViewResp extends WechatBaseResp {
}
}
export
function
requestMerchantTransfer
(
params
:
RequestMerchantTransferParams
)
{
export
function
requestMerchantTransfer
(
params
:
RequestMerchantTransferParams
)
{
return
invokeNativeMethod
<
WechatOpenBusinessViewResp
,
RequestMerchantTransferParams
>
({
type
:
'requestMerchantTransfer'
,
data
:
params
})
return
invokeNativeMethod
<
WechatOpenBusinessViewResp
,
RequestMerchantTransferParams
>
({
type
:
'requestMerchantTransfer'
,
data
:
params
}
,
-
1
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment