消息通讯
This commit is contained in:
@ -5,18 +5,22 @@ export class Server {
|
||||
|
||||
constructor(private env: string) {
|
||||
chrome.runtime.onConnect.addListener((port) => {
|
||||
const handler = (msg: { action: string }) => {
|
||||
const handler = (msg: { action: string; data: any }) => {
|
||||
port.onMessage.removeListener(handler);
|
||||
this.connectHandle(msg.action, msg, port);
|
||||
this.connectHandle(msg.action, msg.data, port);
|
||||
};
|
||||
port.onMessage.addListener(handler);
|
||||
});
|
||||
|
||||
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
||||
this.messageHandle(msg.action, msg, sender, sendResponse);
|
||||
this.messageHandle(msg.action, msg.data, sender, sendResponse);
|
||||
});
|
||||
}
|
||||
|
||||
group(name: string) {
|
||||
return new Group(this, name);
|
||||
}
|
||||
|
||||
on(name: string, func: ApiFunction) {
|
||||
this.apiFunctionMap.set(name, func);
|
||||
}
|
||||
@ -45,3 +49,22 @@ export class Server {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class Group {
|
||||
constructor(
|
||||
private server: Server,
|
||||
private name: string
|
||||
) {
|
||||
if (!name.endsWith("/")) {
|
||||
this.name += "/";
|
||||
}
|
||||
}
|
||||
|
||||
group(name: string) {
|
||||
return new Group(this.server, `${this.name}${name}`);
|
||||
}
|
||||
|
||||
on(name: string, func: ApiFunction) {
|
||||
this.server.on(`${this.name}${name}`, func);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user