Skip to content

Commit

Permalink
异步函数防止重入
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jul 4, 2024
1 parent de157a6 commit 4eec7c6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/editorTable/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -247,7 +247,7 @@ class Env {
}, 100);
}

@throttle(1000)
@queue()
public async updateEditor(askUser = false) {
let editorUri = await this.searchEditorUri(askUser);
if (!editorUri) {
Expand All @@ -262,14 +262,15 @@ class Env {
this.fireOnDidReload();
}

@queue()
public async editorReady(askUser = false) {
if (this.editorUri) {
return;
}
await this.updateEditor(askUser);
}

@throttle(1000)
@queue()
public async updateMap(search: boolean, askUser: boolean) {
let mapUri = await this.searchProjectPath(search, askUser);
if (!mapUri) {
Expand All @@ -294,6 +295,7 @@ class Env {
this.fireOnDidReload();
}

@queue()
public async mapReady(askUser = false) {
if (this.mapUri) {
return;
Expand Down
41 changes: 40 additions & 1 deletion src/utility/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// 每隔一段时间只能执行一次
export function throttle(wait: number) {
return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
Expand All @@ -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<any> = descriptor.value;
let runningMap = new Map<any, (() => void)[]>();

descriptor.value = async function(...args: any[]) {
let queue = runningMap.get(this);
if (!queue) {
queue = [];
runningMap.set(this, queue);
}

let myTurn = new Promise<void>((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;
};
};
}

0 comments on commit 4eec7c6

Please sign in to comment.