mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-08 22:51:25 +08:00
添加setTimeout和clearTimeout支持
This commit is contained in:
parent
60ac9fbc81
commit
cf37411717
@ -42,7 +42,6 @@ const quickcommand = {
|
|||||||
|
|
||||||
// setTimout 不能在 vm2 中使用,同时在 electron 中有 bug
|
// setTimout 不能在 vm2 中使用,同时在 electron 中有 bug
|
||||||
sleep: function (ms) {
|
sleep: function (ms) {
|
||||||
var start = new Date().getTime();
|
|
||||||
try {
|
try {
|
||||||
// node 16.13.1
|
// node 16.13.1
|
||||||
child_process.execSync(getSleepCodeByShell(ms), {
|
child_process.execSync(getSleepCodeByShell(ms), {
|
||||||
@ -50,23 +49,26 @@ const quickcommand = {
|
|||||||
windowsHide: true,
|
windowsHide: true,
|
||||||
});
|
});
|
||||||
} catch (ex) {}
|
} catch (ex) {}
|
||||||
var end = new Date().getTime();
|
return;
|
||||||
return end - start;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// 重写 setTimeout
|
// 重写 setTimeout
|
||||||
setTimeout: function (callback, ms) {
|
setTimeout: function (callback, ms) {
|
||||||
var start = new Date().getTime();
|
const child = child_process.exec(
|
||||||
child_process.exec(
|
|
||||||
getSleepCodeByShell(ms),
|
getSleepCodeByShell(ms),
|
||||||
{
|
{
|
||||||
timeout: ms,
|
timeout: ms,
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
var end = new Date().getTime();
|
if (child.signalCode === "SIGKILL") return;
|
||||||
callback(end - start);
|
callback();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
return child.pid;
|
||||||
|
},
|
||||||
|
|
||||||
|
clearTimeout: function (pid) {
|
||||||
|
kill(pid, "SIGKILL");
|
||||||
},
|
},
|
||||||
|
|
||||||
asyncSleep: async function (ms) {
|
asyncSleep: async function (ms) {
|
||||||
|
@ -137,6 +137,9 @@ let getSandboxFuns = () => {
|
|||||||
os,
|
os,
|
||||||
child_process,
|
child_process,
|
||||||
quickcomposer,
|
quickcomposer,
|
||||||
|
// timeout
|
||||||
|
setTimeout: quickcommand.setTimeout,
|
||||||
|
clearTimeout: quickcommand.clearTimeout,
|
||||||
};
|
};
|
||||||
Object.keys(shortCodes).forEach((f) => {
|
Object.keys(shortCodes).forEach((f) => {
|
||||||
sandbox[f] = shortCodes[f];
|
sandbox[f] = shortCodes[f];
|
||||||
|
3
src/plugins/monaco/types/common.d.ts
vendored
3
src/plugins/monaco/types/common.d.ts
vendored
@ -88,3 +88,6 @@ declare var console: {
|
|||||||
log(message?: any): void,
|
log(message?: any): void,
|
||||||
error(message?: any): void
|
error(message?: any): void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare var setTimeout: (callback: () => void, ms: number) => number;
|
||||||
|
declare var clearTimeout: (timeoutId: number) => void;
|
||||||
|
13
src/plugins/monaco/types/quickcommand.api.d.ts
vendored
13
src/plugins/monaco/types/quickcommand.api.d.ts
vendored
@ -235,6 +235,19 @@ interface quickcommandApi {
|
|||||||
*/
|
*/
|
||||||
setTimeout(callback: () => void, ms);
|
setTimeout(callback: () => void, ms);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除异步等待
|
||||||
|
*
|
||||||
|
* @param timeoutId 等待的timeoutId
|
||||||
|
* ```js
|
||||||
|
* const timeoutId = quickcommand.setTimeout(()=>{
|
||||||
|
* console.log('这条内容不会被打印')
|
||||||
|
* }, 2000)
|
||||||
|
* quickcommand.clearTimeout(timeoutId)
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clearTimeout(timeoutId: number): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* async 等待
|
* async 等待
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user