MWSE/frontend/EventPool.ts

52 lines
1.3 KiB
TypeScript

import MWSE from "./index";
import { Message } from "./WSTSProtocol";
export default class EventPool
{
public wsts : MWSE;
public events : Map<number, [Function,Function]> = new Map();
public signals : Map<string, Function[]> = new Map();
public requests : Map<number, [Function,Function]> = new Map();
public count = 0;
constructor(wsts:MWSE){
this.wsts = wsts;
}
public request(msg: Message) : Promise<any>
{
return new Promise((ok,rej) => {
let id = ++this.count;
this.events.set(id,[
(data:any) => {
ok(data);
},
(data:any) => {
rej(data);
}
]);
this.wsts.WSTSProtocol.SendRequest(msg, id);
})
}
public stream(msg: Message, callback: Function)
{
let id = ++this.count;
this.wsts.WSTSProtocol.StartStream(msg, id);
this.events.set(id,[
(data:any) => {
callback(data);
},
() => { }
]);
}
public signal(event: string, callback: Function)
{
let T = this.signals.get(event);
if(!T)
{
this.signals.set(event, [callback]);
}else{
T.push(callback);
}
}
}