diff --git a/src/metaBuilder/baseBuilder.ts b/src/metaBuilder/baseBuilder.ts index 8edc1ef..73d4749 100644 --- a/src/metaBuilder/baseBuilder.ts +++ b/src/metaBuilder/baseBuilder.ts @@ -1,45 +1,19 @@ import * as vscode from 'vscode'; import { env } from '../env'; import * as tools from '../tools'; +import { throttle } from '../utility/decorators'; export abstract class BaseBuilder { - constructor(public path: string) { - } - protected _onDidChange = new vscode.EventEmitter(); - - public onDidChange = this._onDidChange.event; - - public exists = false; + constructor(public path: string) { } - private debounceTimer?: NodeJS.Timeout; - public update() { - if (this.debounceTimer) { - clearTimeout(this.debounceTimer); - } - this.debounceTimer = setTimeout(() => { - this.debounceTimer = undefined; - this.didUpdate(); - }, 1000); - } - - private async didUpdate() { + @throttle(500) + public async update() { if (!env.scriptUri) { - this.updateExists(false); return; } let code = await this.make(); if (code) { - this.updateExists(await tools.fs.writeFile(env.scriptUri, this.path, code)); - } else { - tools.fs.removeFile(env.scriptUri, this.path); - this.updateExists(false); - } - } - - private updateExists(exists: boolean) { - if (exists !== this.exists) { - this.exists = exists; - this._onDidChange.fire(); + await tools.fs.writeFile(env.scriptUri, this.path, code); } } diff --git a/src/metaBuilder/initBuilder.ts b/src/metaBuilder/initBuilder.ts index 38f3ea6..4bef669 100644 --- a/src/metaBuilder/initBuilder.ts +++ b/src/metaBuilder/initBuilder.ts @@ -24,7 +24,6 @@ class InitBuilder extends BaseBuilder { return; } let codes = this.builders - .filter((builder) => builder.exists) .map((builder) => { // 将正斜杠和反斜杠替换为点号 let name = builder.path @@ -41,10 +40,6 @@ class InitBuilder extends BaseBuilder { public addChild(builder: any, fileName: string) { let instance = new builder(luaPath + '/' + fileName); this.addFile(instance); - instance.onDidChange(() => { - this.update(); - }); - } } @@ -60,5 +55,9 @@ export function init() { initBuilder.addChild(Font, 'font.lua'); //initBuilder.addChild(Objects, 'objects.lua'); + env.onDidChange(() => { + initBuilder.update(); + }); + new TS(tsPath + '/map-declare.d.ts'); }