Skip to content

Commit 87d4970

Browse files
committed
Update gm_api.ts
1 parent c2b3626 commit 87d4970

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/app/service/content/gm_api/gm_api.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,18 +1386,18 @@ export default class GMApi extends GM_Base {
13861386

13871387
@GMContext.API({ alias: "GM_runExclusive" })
13881388
["GM.runExclusive"](lockKey: string, cb: (...args: any) => any, timeout: number = -1) {
1389-
lockKey = `${lockKey}`; // 轉化為字串
1389+
lockKey = `${lockKey}`; // 转化为字串
13901390
if (!lockKey || !this.scriptRes) {
13911391
throw new Error("Invalid Calling");
13921392
}
13931393
const key = `${getStorageName(this.scriptRes).replace(/:/g, ":_")}::${lockKey.replace(/:/g, ":_")}`;
13941394
return new Promise((resolve) => {
1395-
let disconnectFn: (() => any) | null | undefined;
1395+
let killConn: (() => any) | null | undefined = undefined;
13961396
let error: any;
13971397
let result: any;
13981398
let done = false;
13991399
const onDisconnected = () => {
1400-
disconnectFn = null;
1400+
killConn = null; // before resolve, set disconnectFn to null
14011401
resolve({
14021402
result,
14031403
error,
@@ -1419,9 +1419,13 @@ export default class GMApi extends GM_Base {
14191419
onDisconnected(); // in case .disconnect() not working
14201420
};
14211421
this.connect("runExclusive", [key]).then((con) => {
1422-
disconnectFn = () => {
1422+
if (killConn === null) {
1423+
// already resolved
1424+
con.disconnect();
1425+
return;
1426+
}
1427+
killConn = () => {
14231428
con.disconnect();
1424-
onDisconnected(); // in case .disconnect() not working
14251429
};
14261430
con.onDisconnect(onDisconnected);
14271431
con.onMessage((data) => {
@@ -1435,7 +1439,8 @@ export default class GMApi extends GM_Base {
14351439
if (timeout > 0) {
14361440
setTimeout(() => {
14371441
error = new Error("timeout");
1438-
disconnectFn?.();
1442+
killConn?.();
1443+
onDisconnected(); // in case .disconnect() not working
14391444
}, timeout);
14401445
}
14411446
});

0 commit comments

Comments
 (0)