@salad-labs/loopz-typescript
Version:
The Official Loopz TypeScript SDK
113 lines • 4.04 kB
JavaScript
import { Auth } from ".";
import { Client } from "./core/client";
import { v4 as uuid } from "uuid";
export class Notification {
constructor() {
this._socket = null;
this._socketInitialized = false;
this._onOpenConnectionFunctions = [];
this._onCloseConnectionFunctions = [];
this._onMessageFunctions = [];
if (!Notification._config)
throw new Error("Notification must be configured before getting the instance");
Notification._client = new Client(Notification._config.devMode);
Notification._instance = this;
}
/** static methods */
static config(config) {
if (Notification._config)
throw new Error("Notification already configured");
Notification._config = config;
}
static getInstance() {
var _a;
return (_a = Notification._instance) !== null && _a !== void 0 ? _a : new Notification();
}
/** public instance methods */
init() {
if (!Auth.authToken || !Notification._client)
return;
try {
this._socket = new WebSocket(Notification._client.backendNotificationUrl(`?jwt=${Auth.authToken}&apiKey=${Auth.apiKey}&mode=loopz`));
this._socketInitialized = true;
}
catch (error) {
console.log(error);
this._socketInitialized = false;
}
}
close() {
var _a;
if (!this._socketInitialized)
return;
(_a = this._socket) === null || _a === void 0 ? void 0 : _a.close();
this._socketInitialized = false;
}
onOpenConnection(callback) {
if (!this._socketInitialized)
return null;
this._socket.addEventListener("open", callback);
this._onOpenConnectionFunctions.push({
key: uuid(),
fn: callback,
});
return this._onOpenConnectionFunctions[this._onOpenConnectionFunctions.length - 1].key;
}
onCloseConnection(callback) {
if (!this._socketInitialized)
return;
this._socket.addEventListener("close", callback);
this._onCloseConnectionFunctions.push({
key: uuid(),
fn: callback,
});
return this._onCloseConnectionFunctions[this._onCloseConnectionFunctions.length - 1].key;
}
onMessage(callback) {
if (!this._socketInitialized)
return;
this._socket.addEventListener("message", callback);
this._onMessageFunctions.push({
key: uuid(),
fn: callback,
});
return this._onMessageFunctions[this._onMessageFunctions.length - 1].key;
}
offOpenConnection(key) {
if (!this._socketInitialized)
return;
const index = this._onOpenConnectionFunctions.findIndex((element) => {
return element.key === key;
});
if (index > -1) {
this._socket.removeEventListener("open", this._onOpenConnectionFunctions[index].fn);
this._onOpenConnectionFunctions.splice(index, 1);
}
}
offCloseConnection(key) {
if (!this._socketInitialized)
return;
const index = this._onCloseConnectionFunctions.findIndex((element) => {
return element.key === key;
});
if (index > -1) {
this._socket.removeEventListener("close", this._onCloseConnectionFunctions[index].fn);
this._onCloseConnectionFunctions.splice(index, 1);
}
}
offMessage(key) {
if (!this._socketInitialized)
return;
const index = this._onMessageFunctions.findIndex((element) => {
return element.key === key;
});
if (index > -1) {
this._socket.removeEventListener("message", this._onMessageFunctions[index].fn);
this._onMessageFunctions.splice(index, 1);
}
}
}
Notification._config = null;
Notification._instance = null;
Notification._client = null;
//# sourceMappingURL=notification.js.map