From 4eec7c613eb4c3a97e3b49d54e6e4de89ba08f26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 4 Jul 2024 17:50:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=82=E6=AD=A5=E5=87=BD=E6=95=B0=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E9=87=8D=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/editorTable/language.ts | 2 +- src/env.ts | 8 +++++--- src/utility/decorators.ts | 41 ++++++++++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/editorTable/language.ts b/src/editorTable/language.ts index de8fb68..a469ee7 100644 --- a/src/editorTable/language.ts +++ b/src/editorTable/language.ts @@ -73,7 +73,7 @@ class Language extends vscode.Disposable { return hash(value); } - @throttle(1000) + @throttle(500) private updateFile() { let content = JSON.stringify(this._language, null, 4); y3.fs.writeFile(this.uri!, content); diff --git a/src/env.ts b/src/env.ts index 88becc9..267e46d 100644 --- a/src/env.ts +++ b/src/env.ts @@ -5,7 +5,7 @@ import path from 'path'; import * as tools from './tools'; import { Template } from './constants'; import { isPathValid } from './utility'; -import { throttle } from './utility/decorators'; +import { throttle, queue } from './utility/decorators'; type EditorVersion = '1.0' | '2.0' | 'unknown'; @@ -247,7 +247,7 @@ class Env { }, 100); } - @throttle(1000) + @queue() public async updateEditor(askUser = false) { let editorUri = await this.searchEditorUri(askUser); if (!editorUri) { @@ -262,6 +262,7 @@ class Env { this.fireOnDidReload(); } + @queue() public async editorReady(askUser = false) { if (this.editorUri) { return; @@ -269,7 +270,7 @@ class Env { await this.updateEditor(askUser); } - @throttle(1000) + @queue() public async updateMap(search: boolean, askUser: boolean) { let mapUri = await this.searchProjectPath(search, askUser); if (!mapUri) { @@ -294,6 +295,7 @@ class Env { this.fireOnDidReload(); } + @queue() public async mapReady(askUser = false) { if (this.mapUri) { return; diff --git a/src/utility/decorators.ts b/src/utility/decorators.ts index 2697849..d981da0 100644 --- a/src/utility/decorators.ts +++ b/src/utility/decorators.ts @@ -1,4 +1,3 @@ - // 每隔一段时间只能执行一次 export function throttle(wait: number) { return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) { @@ -17,3 +16,43 @@ export function throttle(wait: number) { }; }; } + +// 请求需要排队,禁止重入(只能用于异步函数) +export function queue() { + return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) { + const originalMethod: (...args: any[]) => Promise = descriptor.value; + let runningMap = new Map void)[]>(); + + descriptor.value = async function(...args: any[]) { + let queue = runningMap.get(this); + if (!queue) { + queue = []; + runningMap.set(this, queue); + } + + let myTurn = new Promise((resolve) => { + queue.push(resolve); + }); + + let promise = new Promise(async (resolve) => { + // 等待轮到自己 + await myTurn; + // 执行真正的操作 + let result = await originalMethod.apply(this, args); + // 返回当前的请求 + resolve(result); + // 把自己移出队列 + queue.shift(); + // 处理下一个请求 + queue[0]?.(); + }); + + if (queue.length === 1) { + // 处理第一个请求 + queue[0]?.(); + } + + return promise; + }; + }; +}