7 changed files with 100 additions and 17 deletions
@ -0,0 +1,7 @@ |
|||
import { Tabs } from "@/platform/Tabs" |
|||
|
|||
export function usePlatForm() { |
|||
return { |
|||
Tabs: Tabs.getInstance<Tabs>(), |
|||
} |
|||
} |
@ -0,0 +1,9 @@ |
|||
import { _Base } from "./_Base" |
|||
|
|||
class PlatForm extends _Base { |
|||
constructor() { |
|||
super() |
|||
} |
|||
} |
|||
|
|||
export { PlatForm } |
@ -0,0 +1,54 @@ |
|||
import { _Base } from "./_Base" |
|||
|
|||
export class Tabs extends _Base { |
|||
constructor() { |
|||
super() |
|||
} |
|||
|
|||
private isListen: boolean = false |
|||
|
|||
private execUpdate = (...args) => { |
|||
this.#fnList.forEach(v => v(...args)) |
|||
} |
|||
|
|||
#fnList: ((...args) => void)[] = [] |
|||
listenUpdate(cb: (...args) => void) { |
|||
if (!this.isListen) { |
|||
api.on("main:TabsCommand.update", this.execUpdate) |
|||
this.isListen = true |
|||
} |
|||
this.#fnList.push(cb) |
|||
} |
|||
|
|||
unListenUpdate(fn: (...args) => void) { |
|||
this.#fnList = this.#fnList.filter(v => { |
|||
return v !== fn |
|||
}) |
|||
if (!this.#fnList.length) { |
|||
api.off("main:TabsCommand.update", this.execUpdate) |
|||
this.isListen = false |
|||
} |
|||
} |
|||
|
|||
bindPosition(data) { |
|||
api.call("TabsCommand.bindElement", data) |
|||
} |
|||
|
|||
closeAll() { |
|||
api.call("TabsCommand.closeAll") |
|||
} |
|||
|
|||
sync() { |
|||
api.call("TabsCommand.sync") |
|||
} |
|||
|
|||
unListenerAll() { |
|||
this.#fnList = [] |
|||
api.offAll("main:TabsCommand.update") |
|||
} |
|||
|
|||
async getAllTabs() { |
|||
const res = await api.call("TabsCommand.getAllTabs") |
|||
return res |
|||
} |
|||
} |
@ -0,0 +1,12 @@ |
|||
export abstract class _Base { |
|||
static instance |
|||
|
|||
static getInstance<T>(): T { |
|||
if (!this.instance) { |
|||
// 如果实例不存在,则创建一个新的实例
|
|||
// @ts-ignore ...
|
|||
this.instance = new this() |
|||
} |
|||
return this.instance |
|||
} |
|||
} |
Loading…
Reference in new issue