Skip to content

Commit 939f268

Browse files
committed
调整API输出结果
1 parent 6986d56 commit 939f268

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,24 +1385,26 @@ export default class GMApi extends GM_Base {
13851385
}
13861386

13871387
@GMContext.API({ alias: "GM_runExclusive" })
1388-
["GM.runExclusive"](lockKey: string, cb: (...args: any) => any, timeout: number = -1) {
1388+
["GM.runExclusive"]<T>(lockKey: string, cb: () => T | PromiseLike<T>, timeout: number = -1): Promise<T> {
13891389
lockKey = `${lockKey}`; // 转化为字串
13901390
if (!lockKey || !this.scriptRes) {
1391-
throw new Error("Invalid Calling");
1391+
throw new Error("GM.runExclusive: Invalid Calling");
13921392
}
13931393
const key = `${getStorageName(this.scriptRes).replace(/:/g, ":_")}::${lockKey.replace(/:/g, ":_")}`;
1394-
return new Promise((resolve) => {
1394+
return new Promise((resolve, reject) => {
13951395
let killConn: (() => any) | null | undefined = undefined;
13961396
let error: any;
13971397
let result: any;
13981398
let done = false;
13991399
const onDisconnected = () => {
14001400
killConn = null; // before resolve, set disconnectFn to null
1401-
resolve({
1402-
result,
1403-
error,
1404-
done,
1405-
});
1401+
if (error) {
1402+
reject(error);
1403+
} else if (!done) {
1404+
reject(new Error("GM.runExclusive: Incomplete Action"));
1405+
} else {
1406+
resolve(result);
1407+
}
14061408
};
14071409
const onStart = async (con: MessageConnect) => {
14081410
try {
@@ -1438,7 +1440,7 @@ export default class GMApi extends GM_Base {
14381440
});
14391441
if (timeout > 0) {
14401442
setTimeout(() => {
1441-
error = new Error("timeout");
1443+
error = new Error("GM.runExclusive: Timeout Error");
14421444
killConn?.();
14431445
onDisconnected(); // in case .disconnect() not working
14441446
}, timeout);

0 commit comments

Comments
 (0)