-
Notifications
You must be signed in to change notification settings - Fork 314
[v1.3] 改良代码 - subscribeMessage & HookManager #1210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
5dbb99b
dad4722
077e641
5ee099d
0bacd55
0845e0b
80b1378
f4da477
bde58fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,4 +10,13 @@ export const globalCache = new Map<string, any>(); | |
| export const message = new ExtensionMessage(); | ||
| export const systemClient = new SystemClient(message); | ||
|
|
||
| export const subscribeMessage = <T>(topic: string, handler: (msg: T) => void) => { | ||
| return messageQueue.subscribe<T & { myMessage?: T }>(topic, (data) => { | ||
| const message = data?.myMessage || data; | ||
| if (typeof message === "object") { | ||
CodFrm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| handler(message as T); | ||
CodFrm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }); | ||
| }; | ||
|
|
||
| initLocales(systemConfig); | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,13 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| export class HookManager { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| public isMounted: boolean = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| private unhooks: ((...args: any) => any)[] | null = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| public append(...fns: ((...args: any) => any)[]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.unhooks!.push(...fns); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| public readonly unhook = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.isMounted = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const unhook of this.unhooks!) unhook(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.unhooks!.length = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| private unhooks: ((...args: any) => any)[] | null = []; | |
| public append(...fns: ((...args: any) => any)[]) { | |
| this.unhooks!.push(...fns); | |
| } | |
| public readonly unhook = () => { | |
| this.isMounted = false; | |
| for (const unhook of this.unhooks!) unhook(); | |
| this.unhooks!.length = 0; | |
| // 存储卸载时调用的钩子函数;unhook 后置为 null | |
| private unhooks: (() => void)[] | null = []; | |
| public append(...fns: (() => void)[]) { | |
| // 已经 unhook 的情况下保持幂等,直接忽略追加 | |
| if (!this.unhooks) return; | |
| this.unhooks.push(...fns); | |
| } | |
| public readonly unhook = () => { | |
| // 已经 unhook 过则保持幂等 | |
| if (!this.unhooks) return; | |
| this.isMounted = false; | |
| for (const unhook of this.unhooks) { | |
| unhook(); | |
| } | |
| this.unhooks.length = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
雖然好像沒必要。但小心為上吧
80b1378
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
就一个subscribe,为了统一的话,问题不大
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
实例化一个HookManager可比closure GC开销大多了