@centia-io/sdk 0.0.53 → 0.0.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/centia-io-sdk.cjs +84 -25
- package/dist/centia-io-sdk.d.cts +65 -7
- package/dist/centia-io-sdk.d.cts.map +1 -1
- package/dist/centia-io-sdk.d.ts +65 -7
- package/dist/centia-io-sdk.d.ts.map +1 -1
- package/dist/centia-io-sdk.js +84 -25
- package/dist/centia-io-sdk.js.map +1 -1
- package/dist/centia-io-sdk.umd.js +84 -25
- package/package.json +1 -1
package/dist/centia-io-sdk.js
CHANGED
|
@@ -735,35 +735,94 @@ var Users = class {
|
|
|
735
735
|
*/
|
|
736
736
|
var Ws = class {
|
|
737
737
|
constructor(options) {
|
|
738
|
-
this.
|
|
739
|
-
this.
|
|
738
|
+
this.ws = null;
|
|
739
|
+
this.listeners = {};
|
|
740
|
+
this.closed = false;
|
|
741
|
+
this.options = {
|
|
742
|
+
reconnect: true,
|
|
743
|
+
reconnectInterval: 3e3,
|
|
744
|
+
...options
|
|
745
|
+
};
|
|
746
|
+
this.options.wsClient = this.options.wsClient ?? WebSocket;
|
|
740
747
|
}
|
|
741
748
|
connect() {
|
|
742
|
-
|
|
749
|
+
this.closed = false;
|
|
750
|
+
this.doConnect();
|
|
751
|
+
}
|
|
752
|
+
disconnect() {
|
|
753
|
+
this.closed = true;
|
|
754
|
+
this.ws?.close();
|
|
755
|
+
this.ws = null;
|
|
756
|
+
}
|
|
757
|
+
subscribe(sub) {
|
|
758
|
+
this.send({
|
|
759
|
+
type: "subscription",
|
|
760
|
+
...sub
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
send(data) {
|
|
764
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) throw new Error("WebSocket is not connected");
|
|
765
|
+
this.ws.send(typeof data === "string" ? data : JSON.stringify(data));
|
|
766
|
+
}
|
|
767
|
+
on(event, listener) {
|
|
768
|
+
if (!this.listeners[event]) this.listeners[event] = [];
|
|
769
|
+
this.listeners[event].push(listener);
|
|
770
|
+
return () => this.off(event, listener);
|
|
771
|
+
}
|
|
772
|
+
off(event, listener) {
|
|
773
|
+
const arr = this.listeners[event];
|
|
774
|
+
if (!arr) return;
|
|
775
|
+
const idx = arr.indexOf(listener);
|
|
776
|
+
if (idx !== -1) arr.splice(idx, 1);
|
|
777
|
+
}
|
|
778
|
+
get connected() {
|
|
779
|
+
return this.ws?.readyState === WebSocket.OPEN;
|
|
780
|
+
}
|
|
781
|
+
emit(event, data) {
|
|
782
|
+
const arr = this.listeners[event];
|
|
783
|
+
if (!arr) return;
|
|
784
|
+
for (const fn of arr) fn(data);
|
|
785
|
+
}
|
|
786
|
+
doConnect() {
|
|
743
787
|
const { accessToken } = getTokens();
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
788
|
+
if (!accessToken) return;
|
|
789
|
+
let url = this.options.host + "/?token=" + encodeURIComponent(accessToken);
|
|
790
|
+
if (this.options.rels) url += "&rels=" + encodeURIComponent(this.options.rels);
|
|
791
|
+
const WSClass = this.options.wsClient;
|
|
792
|
+
const ws = new WSClass(url);
|
|
793
|
+
this.ws = ws;
|
|
794
|
+
ws.onopen = () => {
|
|
795
|
+
this.emit("open", void 0);
|
|
796
|
+
};
|
|
797
|
+
ws.onmessage = (event) => {
|
|
798
|
+
let msg;
|
|
799
|
+
try {
|
|
800
|
+
msg = JSON.parse(typeof event.data === "string" ? event.data : event.data.toString());
|
|
801
|
+
} catch {
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
804
|
+
switch (msg.type) {
|
|
805
|
+
case "batch":
|
|
806
|
+
this.emit("batch", msg);
|
|
807
|
+
break;
|
|
808
|
+
case "subscription_ack":
|
|
809
|
+
this.emit("subscription_ack", msg);
|
|
810
|
+
break;
|
|
811
|
+
case "error":
|
|
812
|
+
this.emit("error", msg);
|
|
813
|
+
break;
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
ws.onclose = (event) => {
|
|
817
|
+
this.emit("close", {
|
|
818
|
+
code: event.code,
|
|
819
|
+
reason: event.reason
|
|
820
|
+
});
|
|
821
|
+
if (!this.closed && this.options.reconnect) setTimeout(() => this.doConnect(), this.options.reconnectInterval);
|
|
822
|
+
};
|
|
823
|
+
ws.onerror = () => {
|
|
824
|
+
ws.close();
|
|
765
825
|
};
|
|
766
|
-
if (accessToken !== "") connect();
|
|
767
826
|
}
|
|
768
827
|
};
|
|
769
828
|
|