dev-classes
Version:
<h3 align="center">SocketApi</h3> ### !!! Пакет пепеехал в lib-socket-api !!!
159 lines (158 loc) • 5.3 kB
JavaScript
var v = Object.defineProperty;
var d = (r, t, e) => t in r ? v(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
var s = (r, t, e) => (d(r, typeof t != "symbol" ? t + "" : t, e), e);
class p {
constructor(t) {
s(this, "networkInfo");
s(this, "listUrls", []);
s(this, "state", {
isActiveEvents: !1,
isMonitoring: !1,
checkIntervalId: null
});
s(this, "getFetchOptions", (t) => ({
method: "HEAD",
cache: "no-store",
signal: t.signal,
headers: {
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
Expires: "0"
}
}));
s(this, "getIsNetwork", (t) => !["unknown", "none"].includes(t));
s(this, "getTypeNetwork", ({ effectiveType: t, type: e }, n) => (n || n === null) && (t || e) || "none");
/*-----------------------------------------------------------------------------*/
s(this, "controllersEvents", {
online: null,
offline: null,
change: null
});
s(this, "controllersFetching", {
online: null,
offline: null,
change: null
});
this.listUrls = t;
const e = typeof navigator < "u" ? navigator.onLine : !1;
this.networkInfo = {
isNetwork: e,
typeNetwork: this.getTypeNetwork(this.getConnection(), e)
};
}
setState(t) {
this.state = { ...this.state, ...t };
}
getState() {
return this.state;
}
getConnection() {
var t;
return (t = window == null ? void 0 : window.navigator) == null ? void 0 : t.connection;
}
updateState(t, e) {
const n = {
isNetwork: t,
typeNetwork: this.getTypeNetwork(this.getConnection(), t)
};
this.networkInfo = n, typeof e == "function" && e(n);
}
getControllersEvents() {
return this.controllersEvents;
}
setControllersEvents(t) {
this.controllersEvents = { ...this.controllersEvents, ...t };
}
startEvents(t) {
var l, c, a, h;
const { isActiveEvents: e } = this.getState();
if (e)
return;
this.setState({ isActiveEvents: !0 });
const n = {
online: new AbortController(),
offline: new AbortController(),
change: new AbortController()
};
this.setControllersEvents(n);
const o = this.getConnection(), i = this.getTypeNetwork(o, window.navigator.onLine), g = this.getIsNetwork(i);
this.updateState(g, t), window.addEventListener("online", () => {
this.updateState(!0, t);
}, { signal: (l = n.online) == null ? void 0 : l.signal }), window.addEventListener("offline", () => {
this.updateState(!1, t);
}, { signal: (c = n.offline) == null ? void 0 : c.signal }), (a = this.getConnection()) != null && a.addEventListener && this.getConnection().addEventListener("change", () => {
const u = this.getConnection(), w = this.getTypeNetwork(u, null), f = this.getIsNetwork(w);
this.updateState(f, t);
}, { signal: (h = n.change) == null ? void 0 : h.signal });
}
stopEvents() {
const { isActiveEvents: t } = this.getState();
if (t) {
this.setState({ isActiveEvents: !1 });
const e = this.getControllersEvents();
for (const n of Object.values(e))
n == null || n.abort();
}
}
/*-----------------------------------------------------------------------------*/
async requestByUrls(t, e) {
const n = await this.checkConnection(e);
this.updateState(n, t);
}
async checkConnection(t = {}) {
for (const e of this.listUrls)
try {
const n = new AbortController(), o = setTimeout(() => n.abort(), (t == null ? void 0 : t.timeout) || 5e3), i = await window.fetch(e);
if (clearTimeout(o), i.status >= 200 && i.status < 400)
return !0;
} catch {
continue;
}
return !1;
}
getControllersMonitoring() {
return this.controllersFetching;
}
setControllersMonitoring(t) {
this.controllersFetching = { ...this.controllersFetching, ...t };
}
startFetching(t, { interval: e = 5e3 }) {
const { isMonitoring: n } = this.getState();
if (n)
return;
this.setState({ isMonitoring: !0 });
const o = {
online: new AbortController(),
offline: new AbortController(),
change: new AbortController()
};
this.setControllersMonitoring(o), this.requestByUrls(t, { timeout: 2e3 });
const i = window.setInterval(() => this.requestByUrls(t), e);
this.setState({ checkIntervalId: i });
}
stopFetching() {
const { isMonitoring: t, checkIntervalId: e } = this.getState();
if (!t)
return;
const n = this.getControllersMonitoring();
for (const o of Object.values(n))
o == null || o.abort();
e && clearInterval(e), this.setState({ isMonitoring: !1, checkIntervalId: null });
}
/*--------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------*/
async checkStatus(t, e) {
return await this.requestByUrls(t), this.getCurrentState();
}
fetchingNetwork(t, e) {
return this.startFetching(t, e), {
stop: () => this.stopFetching()
};
}
getCurrentState() {
return this.networkInfo;
}
}
export {
p as NetworkStatusTracker
};