MWSE/frontend/EventPool.ts

49 lines
1.2 KiB
TypeScript

import WSTS from "./index";
import { Message } from "./WSTSProtocol";
export default class EventPool
{
public wsts : WSTS;
public events : Map<number, [Function,Function]> = new Map();
public signals : Map<string, Function[]> = new Map();
public count = 0;
constructor(wsts:WSTS){
this.wsts = wsts;
}
public request(msg: Message) : Promise<any>
{
return new Promise((ok,rej) => {
let id = ++this.count;
this.wsts.WSTSProtocol.SendRequest(msg, id);
this.events.set(id,[
(data:any) => {
ok(data);
},
(data:any) => {
rej(data);
}
]);
})
}
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);
}
}
}