dev-classes
Version:
<h3 align="center">SocketApi</h3>
117 lines (116 loc) • 3.75 kB
JavaScript
var c = Object.defineProperty;
var l = (a, t, s) => t in a ? c(a, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : a[t] = s;
var e = (a, t, s) => (l(a, typeof t != "symbol" ? t + "" : t, s), s);
import { EventSubscribers as S } from "../../../Utils/EventSubscribers/EventSubscribers.js";
class v {
constructor() {
e(this, "options", {
timeReConnect: 5e3,
numberOfRepit: 5,
url: ""
});
e(this, "state", {
statusConnect: "disconnect",
ws: null,
arrSaveReq: []
});
e(this, "initOptions", !1);
e(this, "events", new S(["status", "msg"]));
e(this, "stateDefault", this.copyState(this.state));
e(this, "eventListener", (t) => {
var i, r;
const s = [
["open", this.openHandler],
["close", this.closeHandler],
["message", this.msgHandler],
["error", this.errHandler]
];
for (let n = 0; n < s.length; n++) {
const [o, h] = s[n];
t === "add" ? (i = this.state.ws) == null || i.addEventListener(o, h) : (r = this.state.ws) == null || r.removeEventListener(o, h);
}
});
e(this, "openHandler", () => {
this.setStatus("ready");
});
e(this, "closeHandler", () => {
this.setStatus("close");
});
e(this, "msgHandler", (t) => {
const s = JSON.parse(t.data ? t.data : "{}");
try {
const { action: i } = s;
i && this.filterSaveItemsByResponse(s), this.events.publish("msg", s);
} catch {
this.events.publish("msg", JSON.parse("{}"));
}
});
e(this, "errHandler", (t) => {
this.setStatus("error");
});
e(this, "errorInitSocket", () => {
console.error("Вы не установили опции");
});
e(this, "setStatus", (t) => {
this.events.publish("status", t), this.setState({ statusConnect: t });
});
/*----------------------------------------------------------------------------------------------------------*/
e(this, "getSocket", () => this.state.ws);
e(this, "getStatusSocket", () => this.state.statusConnect);
e(this, "getRequestSave", () => this.state.arrSaveReq);
e(this, "getOptions", () => this.options);
e(this, "getRegisteredEvents", this.events.getListNameEvents);
e(this, "on", this.events.subscribe);
e(this, "off", this.events.unsubscribe);
e(this, "init", (t) => {
this.initOptions = !0, this.options = { ...this.options, ...t };
});
e(this, "getIsInitWS", () => {
const t = this.initOptions;
return t || this.errorInitSocket(), t;
});
}
resetState() {
this.state = this.copyState(this.stateDefault);
}
setState(t) {
this.state = { ...this.state, ...t };
}
copyState(t) {
return JSON.parse(JSON.stringify(t));
}
filterSaveItemsByResponse(t) {
const s = this.getRequestSave(), i = [];
for (let r = 0; r < s.length; r++) {
const n = s[r];
n.payload.action !== t.action ? i.push(n) : n.cb && n.cb(t);
}
this.state.arrSaveReq = i;
}
connect() {
this.initOptions && (this.close(), this.setState({
ws: new WebSocket(this.options.url)
}), this.setStatus("pending"), this.eventListener("add"));
}
close() {
var t;
(t = this.state.ws) == null || t.close(), this.eventListener("remove");
}
disconnect() {
this.close(), this.resetState();
}
send(t) {
var i;
const s = JSON.stringify(t);
(i = this.state.ws) == null || i.send(s);
}
setRequestSave(t) {
if ("action" in t.payload) {
const s = this.state.arrSaveReq.findIndex((i) => i.payload.action === t.payload.action);
~s ? this.state.arrSaveReq[s] = t : this.state.arrSaveReq.push(t);
}
}
}
export {
v as WsApi
};