梳理事件流

This commit is contained in:
xuyanfeng
2021-04-28 13:59:16 +08:00
parent 7b3ee52a6d
commit c4fb53646f
9 changed files with 167 additions and 141 deletions

View File

@@ -1,29 +0,0 @@
export const Page = {
Inject: "Inject",
Devtools: "Devtools",
Content: "Content",
Popup: "Popup",
Options: "Options",
}
export const Msg = {
NodeInfo: "node-info",// 具体的节点信息
TreeInfo: "tree-info",// 节点树信息
Support: "game-support",// 游戏支持信息
MemoryInfo: "memory-info",//
TabsInfo: "tabs_info", // 当前页面信息
UrlChange: "url_change", // 网址发生变化
SetProperty: "set-property", // 设置node属性
}
export function MsgInclude(msg: string) {
for (let key in Msg) {
if (Msg.hasOwnProperty(key)) {
//@ts-ignore
let m = Msg[key] as string;
if (m === msg) {
return true
}
}
}
return false;
}

56
source/src/core/types.ts Normal file
View File

@@ -0,0 +1,56 @@
export enum Page {
Inject = "Inject",
Devtools = "Devtools",
Background = 'Background',
Content = "Content",
Popup = "Popup",
Options = "Options",
}
export enum Msg {
NodeInfo = "node-info",// 具体的节点信息
TreeInfo = "tree-info",// 节点树信息
Support = "game-support",// 游戏支持信息
MemoryInfo = "memory-info",//
TabsInfo = "tabs_info", // 当前页面信息
UrlChange = "url_change", // 网址发生变化
SetProperty = "set-property", // 设置node属性
}
export class PluginEvent {
msg: Msg | null = null;
data: any = null;
target: Page | null = null; // 事件要发送的目标
constructor(target: Page, msg: Msg, data?: any) {
if (PageInclude(target)) {
this.target = target;
this.msg = msg;
this.data = data || null;
} else {
console.warn(`无效的target: ${target}`)
}
}
}
function inEnum(enumValues: any, value: Page | Msg) {
for (let key in enumValues) {
if (enumValues.hasOwnProperty(key)) {
//@ts-ignore
let itemEnum = enumValues[key] as string;
if (itemEnum === value) {
return true
}
}
}
return false;
}
export function PageInclude(page: Page) {
return inEnum(Page, page);
}
export function MsgInclude(msg: Msg) {
return inEnum(Msg, msg)
}