Skip to content

Commit c7bb5b4

Browse files
committed
GM.runExclusive -> GM.takeTurn
1 parent 6832ca8 commit c7bb5b4

5 files changed

Lines changed: 19 additions & 19 deletions

File tree

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// ==UserScript==
2-
// @name GM.runExclusive Demo
2+
// @name GM.takeTurn Demo
33
// @namespace https://docs.scriptcat.org/
44
// @version 0.1.2
5-
// @match https://example.com/*?runExclusive*
6-
// @grant GM.runExclusive
5+
// @match https://example.com/*?takeTurn*
6+
// @grant GM.takeTurn
77
// @grant GM.setValue
88
// @grant GM.getValue
99
// @run-at document-start
@@ -13,7 +13,7 @@
1313
(async function () {
1414
'use strict';
1515

16-
const delayMatch = location.href.match(/runExclusive(\d+)_(\d*)/);
16+
const delayMatch = location.href.match(/takeTurn(\d+)_(\d*)/);
1717
const timeDelay = delayMatch ? +delayMatch[1] : 0;
1818
const timeoutValue = (delayMatch ? +delayMatch[2] : 0) || -1;
1919
const isWorker = !!timeDelay;
@@ -68,7 +68,7 @@
6868
if (!isWorker) {
6969
panel.style.width = "480px";
7070
panel.innerHTML = `
71-
<h3 style="margin-top:0">GM.runExclusive Demo</h3>
71+
<h3 style="margin-top:0">GM.takeTurn Demo</h3>
7272
<p>Pick worker durations (ms):</p>
7373
<div style="display:flex; flex-direction:row; gap: 4px;">
7474
<input id="durations" value="1200,2400,3800,400"
@@ -107,7 +107,7 @@
107107

108108
delays.forEach(delay => {
109109
const iframe = document.createElement('iframe');
110-
iframe.src = `${location.pathname}?runExclusive${delay}_${timeoutQ}`;
110+
iframe.src = `${location.pathname}?takeTurn${delay}_${timeoutQ}`;
111111
iframe.style.width = '100%';
112112
iframe.style.height = '160px';
113113
iframe.style.border = '1px solid #444';
@@ -120,7 +120,7 @@
120120
if (e.data?.type !== 'close-worker') return;
121121
const iframes = iframeContainer.querySelectorAll('iframe');
122122
for (const iframe of iframes) {
123-
if (iframe.src.includes(`runExclusive${e.data.delay}_`)) {
123+
if (iframe.src.includes(`takeTurn${e.data.delay}_`)) {
124124
iframe.remove();
125125
log(`Closed worker ${e.data.delay}ms`, '#ff9800');
126126
return;
@@ -157,7 +157,7 @@
157157
const startWait = performance.now();
158158

159159
try {
160-
const result = await GM.runExclusive('demo-lock-key', async () => {
160+
const result = await GM.takeTurn('demo-lock-key', async () => {
161161
const waited = Math.round(performance.now() - startWait);
162162

163163
const order = (await GM.getValue('order')) + 1;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,11 +1544,11 @@ export default class GMApi extends GM_Base {
15441544
return this.loadScriptPromise;
15451545
}
15461546

1547-
@GMContext.API({ alias: "GM_runExclusive" })
1548-
["GM.runExclusive"]<T>(lockKey: string, cb: () => T | PromiseLike<T>, timeout: number = -1): Promise<T> {
1547+
@GMContext.API({ alias: "GM_takeTurn" })
1548+
["GM.takeTurn"]<T>(lockKey: string, cb: () => T | PromiseLike<T>, timeout: number = -1): Promise<T> {
15491549
lockKey = `${lockKey}`; // 转化为字串
15501550
if (!lockKey || !this.scriptRes) {
1551-
throw new Error("GM.runExclusive: Invalid Calling");
1551+
throw new Error("GM.takeTurn: Invalid Calling");
15521552
}
15531553
const key = `${getStorageName(this.scriptRes).replace(/:/g, ":_")}::${lockKey.replace(/:/g, ":_")}`;
15541554

@@ -1563,7 +1563,7 @@ export default class GMApi extends GM_Base {
15631563
if (error) {
15641564
reject(error);
15651565
} else if (state !== 2) {
1566-
reject(new Error("GM.runExclusive: Incomplete Action"));
1566+
reject(new Error("GM.takeTurn: Incomplete Action"));
15671567
} else {
15681568
resolve(result);
15691569
}
@@ -1590,7 +1590,7 @@ export default class GMApi extends GM_Base {
15901590
con.disconnect();
15911591
onDisconnected(); // in case .disconnect() not working
15921592
};
1593-
this.connect("runExclusive", [key]).then((con) => {
1593+
this.connect("takeTurn", [key]).then((con) => {
15941594
if (killConn === null || state > 0) {
15951595
// already resolved (unexpected or by timeout)
15961596
con.disconnect();
@@ -1611,15 +1611,15 @@ export default class GMApi extends GM_Base {
16111611
if (timeout > 0) {
16121612
setTimeout(() => {
16131613
if (killConn === null || state > 0) return; // 执行开始了就不进行 timeout 操作
1614-
error = new Error("GM.runExclusive: Timeout Error");
1614+
error = new Error("GM.takeTurn: Timeout Error");
16151615
killConn?.();
16161616
onDisconnected(); // in case .disconnect() not working
16171617
}, timeout);
16181618
}
16191619
});
16201620

16211621
return new Promise((resolve, reject) => {
1622-
stackAsyncTask(`runExclusive::${key}`, async () => {
1622+
stackAsyncTask(`takeTurn::${key}`, async () => {
16231623
try {
16241624
resolve(await taskAsync());
16251625
} catch (e) {

src/app/service/service_worker/gm_api/gm_api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,8 +1388,8 @@ export default class GMApi {
13881388
}
13891389
}
13901390

1391-
@PermissionVerify.API({ link: ["GM.runExclusive", "GM_runExclusive"] })
1392-
runExclusive(request: GMApiRequest<[string]>, sender: IGetSender) {
1391+
@PermissionVerify.API({ link: ["GM.takeTurn", "GM_takeTurn"] })
1392+
takeTurn(request: GMApiRequest<[string]>, sender: IGetSender) {
13931393
if (!request.params || request.params.length < 1) {
13941394
throw new Error("param is failed");
13951395
}

src/template/scriptcat.d.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ declare const GM: {
306306
cookie(action: GMTypes.CookieAction, details: GMTypes.CookieDetails): Promise<GMTypes.Cookie[]>;
307307

308308
/** cross-context exclusive execution */
309-
runExclusive<T>(key: string, callback: () => T | PromiseLike<T>, timeout?: number): Promise<T>;
309+
takeTurn<T>(key: string, callback: () => T | PromiseLike<T>, timeout?: number): Promise<T>;
310310
};
311311

312312
/**

src/types/scriptcat.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ declare const GM: {
306306
cookie(action: GMTypes.CookieAction, details: GMTypes.CookieDetails): Promise<GMTypes.Cookie[]>;
307307

308308
/** cross-context exclusive execution */
309-
runExclusive<T>(key: string, callback: () => T | PromiseLike<T>, timeout?: number): Promise<T>;
309+
takeTurn<T>(key: string, callback: () => T | PromiseLike<T>, timeout?: number): Promise<T>;
310310
};
311311

312312
/**

0 commit comments

Comments
 (0)