Skip to content

Commit

Permalink
暂存
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jun 28, 2024
1 parent 0a40549 commit 74caef8
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/editorTable/EXCEL/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import * as y3 from 'y3-helper';

export async function test() {
let table = y3.table.open('单位');
await table.get(134218426);
let obj = await table.get(134218426);
}
22 changes: 21 additions & 1 deletion src/editorTable/editorTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,30 @@ import { env } from "../env";
import * as vscode from "vscode";
import * as y3 from 'y3-helper';

type ObjectShape = {
"name": string | number,
[key: string]: ItemShape,
};

type ItemShape = string | boolean | number | TupleShape | MapShape | ArrayShape;
type ArrayShape = ItemShape[];

type TupleShape = {
"__tuple__": true,
"items": ItemShape[],
};

type MapShape = {
[key: string]: any,
};

class EditorObject {
public raw: Object;
public raw: ObjectShape;
public name: string;
constructor(public key: number, public uri: vscode.Uri, json: string) {
this.raw = JSON.parse(json);
let name = this.raw['name'];
this.name = y3.language.get(name) ?? '<未知>';
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/editorTable/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode';
import { env } from '../env';
import * as y3 from 'y3-helper';
import { hash } from '../utility';
import { throttle } from '../utility/decorators';

class Language extends vscode.Disposable {
private disposeList: vscode.Disposable[] = [];
Expand Down Expand Up @@ -44,12 +45,12 @@ class Language extends vscode.Disposable {
return this._language[key];
}

// TODO: 未来支持修改语言文件
set(key: string, value: string) {
this._language[key] = value;
if (this._reverse) {
this._reverse[value] = key;
}
this.updateFile();
}

fetch(value: string): string {
Expand All @@ -71,6 +72,12 @@ class Language extends vscode.Disposable {
private makeKey(value: string): string {
return hash(value).toString();
}

@throttle(1000)
private updateFile() {
let content = JSON.stringify(this._language, null, 4);
y3.fs.writeFile(this.uri!, content);
}
}

let language: Language;
Expand Down
23 changes: 3 additions & 20 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,13 @@ import * as vscode from 'vscode';
import * as os from 'os';
import winreg from 'winreg';
import path from 'path';
import util from 'util';
import * as tools from './tools';
import { Template } from './constants';
import { isPathValid } from './utility';
import { throttle } from './utility/decorators';

type EditorVersion = '1.0' | '2.0' | 'unknown';

function rePrepare(method: Function, context: ClassMethodDecoratorContext) {
let running = false;
return async function (this: any, ...args: any[]) {
if (running) {
while (running) {
await util.promisify(setTimeout)(100);
}
}
running = true;
try {
await method.apply(this, args);
} finally {
running = false;
}
};
}

class Env {
private envChangeEmitter = new vscode.EventEmitter<void>();
public onDidChange = this.envChangeEmitter.event;
Expand Down Expand Up @@ -264,7 +247,7 @@ class Env {
}, 100);
}

@rePrepare
@throttle(1000)
public async updateEditor(askUser = false) {
let editorUri = await this.searchEditorUri(askUser);
if (!editorUri) {
Expand All @@ -286,7 +269,7 @@ class Env {
await this.updateEditor(askUser);
}

@rePrepare
@throttle(1000)
public async updateMap(search: boolean, askUser: boolean) {
let mapUri = await this.searchProjectPath(search, askUser);
if (!mapUri) {
Expand Down
17 changes: 17 additions & 0 deletions src/utility/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

// 每隔一段时间只能执行一次
export function throttle(wait: number) {
return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
let isThrottled = false;

descriptor.value = function(...args: any[]) {
if (isThrottled) {
return;
};
isThrottled = true;
setTimeout(() => isThrottled = false, wait);
return originalMethod.apply(this, args);
};
};
}
7 changes: 4 additions & 3 deletions src/utility/spinLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import util from 'util';
*/
export class SpinLock {
private locked: boolean = false;
private waitTime=0;
private waitTime = 0;

constructor(waitTime?:number) {
constructor(waitTime?: number) {
if (waitTime) {
this.waitTime = waitTime;
}
}
public async acquire(){

public async acquire() {
while (this.locked) {
// 自旋等待直到锁可用
await util.promisify(setTimeout)(this.waitTime);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"paths": {
"y3-helper": ["./src/y3-helper"],
},
"experimentalDecorators": true,
},

// 忽略对template/excel/importRules.ts的编译 因为它只是个模板 生成给用户用
Expand Down

0 comments on commit 74caef8

Please sign in to comment.