@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.
@@ -735,35 +735,94 @@ var Users = class {
735
735
  */
736
736
  var Ws = class {
737
737
  constructor(options) {
738
- this.options = options;
739
- this.options.wsClient = this.options?.wsClient ?? WebSocket;
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
- const me = this;
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
- const connect = () => {
745
- let queryString = `?token=` + accessToken;
746
- if (this.options?.rel) queryString = queryString + `&rel=` + this.options.rel;
747
- const WSClass = this.options.wsClient;
748
- const ws = new WSClass(this.options.host + `/` + queryString);
749
- ws.onopen = function() {
750
- console.log("WebSocket connected!");
751
- };
752
- ws.onmessage = function(event) {
753
- me.options.callBack(event.data);
754
- };
755
- ws.onclose = function(event) {
756
- if (accessToken !== "") {
757
- console.log("WebSocket closed, reconnecting in 3 seconds...", event.reason);
758
- setTimeout(connect, 3e3);
759
- }
760
- };
761
- ws.onerror = function(err) {
762
- console.error("WebSocket error observed:", err);
763
- ws.close();
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