Skip to content

Commit b455724

Browse files
cyfung1031CodFrm
andauthored
🏷️ typescript 修订 (#1127)
* typescript 修订 * 调整type --------- Co-authored-by: 王一之 <yz@ggnb.top>
1 parent 292549e commit b455724

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

packages/message/message_queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import EventEmitter from "eventemitter3";
22
import LoggerCore from "@App/app/logger/core";
33
import { type TMessage } from "./types";
44

5-
export type TKeyValue = { key: string; value: string };
5+
export type TKeyValue<T = string> = { key: string; value: T };
66

77
// 中间件函数类型
88
type MiddlewareFunction<T = any> = (topic: string, message: T, next: () => void) => void | Promise<void>;

src/pkg/config/config.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,20 @@ export class SystemConfig {
7575
private readonly storage = new ChromeStorage("system", true);
7676

7777
constructor(private mq: IMessageQueue) {
78-
this.mq.subscribe<TKeyValue>(SystemConfigChange, ({ key, value }) => {
78+
this.mq.subscribe<TKeyValue<any>>(SystemConfigChange, ({ key, value }) => {
7979
this.cache.set(key, value);
8080
});
8181
}
8282

83-
addListener(key: string, callback: (value: any) => void) {
84-
this.mq.subscribe<TKeyValue>(SystemConfigChange, (data) => {
83+
addListener<T extends SystemConfigKey>(key: T, callback: (value: SystemConfigValueType<T>) => void) {
84+
this.mq.subscribe<TKeyValue<SystemConfigValueType<T>>>(SystemConfigChange, (data) => {
8585
if (data.key === key) {
8686
callback(data.value);
8787
}
8888
});
8989
}
9090

91-
private _get<T extends string | number | boolean | object>(
91+
private _get<T extends string | number | boolean | object | undefined>(
9292
key: SystemConfigKey,
9393
defaultValue: WithAsyncValue<Exclude<T, undefined>>
9494
): Promise<T> {
@@ -106,20 +106,7 @@ export class SystemConfig {
106106
});
107107
}
108108

109-
public get(key: SystemConfigKey | SystemConfigKey[]): Promise<any | any[]> {
110-
if (Array.isArray(key)) {
111-
const promises = key.map((key) => {
112-
const funcName = `get${toCamelCase(key)}`;
113-
// @ts-ignore
114-
if (typeof this[funcName] === "function") {
115-
// @ts-ignore
116-
return this[funcName]() as Promise<any>;
117-
} else {
118-
throw new Error(`Method ${funcName} does not exist on SystemConfig`);
119-
}
120-
});
121-
return Promise.all(promises);
122-
}
109+
public get<T extends SystemConfigKey>(key: T): Promise<SystemConfigValueType<T>> {
123110
const funcName = `get${toCamelCase(key)}`;
124111
// @ts-ignore
125112
if (typeof this[funcName] === "function") {
@@ -130,7 +117,7 @@ export class SystemConfig {
130117
}
131118
}
132119

133-
public set(key: SystemConfigKey, value: any): void {
120+
public set<T extends SystemConfigKey>(key: T, value: SystemConfigValueType<T>): void {
134121
const funcName = `set${toCamelCase(key)}`;
135122
// @ts-ignore
136123
if (typeof this[funcName] === "function") {
@@ -141,7 +128,7 @@ export class SystemConfig {
141128
}
142129
}
143130

144-
private _set(key: SystemConfigKey, value: any) {
131+
private _set<T extends SystemConfigKey>(key: T, value: SystemConfigValueType<T>) {
145132
if (value === undefined) {
146133
this.cache.delete(key);
147134
this.storage.remove(key);
@@ -150,7 +137,7 @@ export class SystemConfig {
150137
this.storage.set(key, value);
151138
}
152139
// 发送消息通知更新
153-
this.mq.publish<TKeyValue>(SystemConfigChange, {
140+
this.mq.publish<TKeyValue<any>>(SystemConfigChange, {
154141
key,
155142
value,
156143
});
@@ -260,7 +247,7 @@ export class SystemConfig {
260247
}
261248

262249
getCatFileStorage() {
263-
return this._get<CATFileStorage>("cat_file_storage", this.defaultCatFileStorage());
250+
return this._get<CATFileStorage | undefined>("cat_file_storage", this.defaultCatFileStorage());
264251
}
265252

266253
setCatFileStorage(data: CATFileStorage | undefined) {
@@ -276,7 +263,7 @@ export class SystemConfig {
276263
}
277264

278265
getEslintConfig() {
279-
return this._get<string>("eslint_config", defaultConfig);
266+
return this._get<string | undefined>("eslint_config", defaultConfig);
280267
}
281268

282269
setEslintConfig(v: string) {
@@ -289,7 +276,7 @@ export class SystemConfig {
289276
}
290277

291278
getEditorConfig() {
292-
return this._get<string>("editor_config", editorDefaultConfig);
279+
return this._get<string | undefined>("editor_config", editorDefaultConfig);
293280
}
294281

295282
setEditorConfig(v: string) {

0 commit comments

Comments
 (0)