seepur/resources/scripts/applications/home/scripts/user.channel.service.ts
2020-04-12 10:25:42 -04:00

36 lines
867 B
TypeScript

let singleton: UserChannel = null;
export default class UserChannel {
private subscription;
private constructor(private ws) {
this.subscription = null;
}
async connect(): Promise<boolean> {
this.subscription = this.ws.subscribe(`user_channel`);
const subscription = this.subscription;
const self = this;
return new Promise((resolve, reject) => {
subscription.on('error', () => { resolve(false) });
subscription.on('ready', () => {
resolve(true)
});
subscription.on('close', self.close);
});
}
on(event: string, callback: (...args) => void) {
if (this.subscription) this.subscription.on(event, callback);
}
static async getInstance(ws) {
if (singleton) return singleton;
else return new UserChannel(ws);
}
close() {
this.subscription.close();
singleton = null;
}
}