slapflow 1.2.0 → 1.2.1

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/index.cjs CHANGED
@@ -2675,115 +2675,148 @@ var createWS = (options) => {
2675
2675
  return { start, stop, reconnect, status: () => currentStatus };
2676
2676
  };
2677
2677
 
2678
+ // src/helpers/parsePayload.ts
2679
+ var parsePayload = (data) => {
2680
+ if (typeof data === "string") {
2681
+ try {
2682
+ return JSON.parse(data);
2683
+ } catch {
2684
+ return data;
2685
+ }
2686
+ }
2687
+ return data;
2688
+ };
2689
+
2678
2690
  // src/createWebSocket.ts
2679
- var createWebSocket = (options) => {
2680
- let socket;
2681
- let retryTimer;
2682
- let retryAttempt = 0;
2683
- let started = false;
2684
- let currentStatus = "idle";
2685
- const emitSocketEvent = (topic, payload) => {
2686
- options.bus.dispatch({
2687
- id: createId(),
2688
- topic,
2689
- occurredAt: Date.now(),
2690
- ...options.origin ? { origin: options.origin } : {},
2691
- parsed: payload,
2692
- serialized: JSON.stringify(payload)
2693
- });
2694
- };
2695
- const scheduleRetry = () => {
2696
- const delay = getRetryDelay(retryAttempt, options.reconnect ?? {});
2697
- const attempt = retryAttempt + 1;
2698
- currentStatus = "reconnecting";
2699
- retryAttempt = attempt;
2700
- retryTimer = setTimeout(() => {
2701
- retryTimer = void 0;
2702
- connect();
2703
- }, delay);
2704
- };
2705
- const connect = () => {
2706
- if (!started || socket) {
2691
+ var WebSocketConnection = class {
2692
+ constructor(options) {
2693
+ this.options = options;
2694
+ }
2695
+ options;
2696
+ _socket;
2697
+ _retryTimer;
2698
+ _retryAttempt = 0;
2699
+ _queue = [];
2700
+ _started = false;
2701
+ _status = "idle";
2702
+ start() {
2703
+ if (!this._started) {
2704
+ this._started = true;
2705
+ this.connect();
2706
+ }
2707
+ }
2708
+ stop() {
2709
+ this._started = false;
2710
+ this.clearTimer();
2711
+ this.clearQueue();
2712
+ const current = this._socket;
2713
+ this._socket = void 0;
2714
+ current?.close();
2715
+ this.setStatus("stopped");
2716
+ }
2717
+ reconnect() {
2718
+ if (this._started) {
2719
+ this.clearTimer();
2720
+ const current = this._socket;
2721
+ this._socket = void 0;
2722
+ current?.close();
2723
+ this.connect();
2724
+ }
2725
+ }
2726
+ status() {
2727
+ return this._status;
2728
+ }
2729
+ send(data) {
2730
+ if (this._socket?.readyState === WebSocket.OPEN) {
2731
+ this._socket.send(data);
2732
+ } else {
2733
+ this._queue.push(data);
2734
+ }
2735
+ }
2736
+ connect() {
2737
+ if (!this._started || this._socket) {
2707
2738
  return;
2708
2739
  }
2709
- currentStatus = "connecting";
2710
- try {
2711
- const current = new WebSocket(options.url, options.protocols ?? []);
2712
- socket = current;
2713
- current.addEventListener("open", () => {
2714
- if (socket === current) {
2715
- retryAttempt = 0;
2716
- currentStatus = "connected";
2717
- emitSocketEvent("open", { url: options.url });
2718
- }
2719
- });
2720
- current.addEventListener("message", (event) => {
2721
- if (socket !== current) {
2722
- return;
2723
- }
2724
- let parsed = event.data;
2725
- if (typeof parsed === "string") {
2726
- try {
2727
- parsed = JSON.parse(parsed);
2728
- } catch {
2729
- parsed = event.data;
2730
- }
2731
- }
2732
- emitSocketEvent("message", parsed);
2733
- });
2734
- current.addEventListener("close", (event) => {
2735
- if (socket !== current) {
2736
- return;
2737
- }
2738
- socket = void 0;
2739
- emitSocketEvent("close", { code: event.code, reason: event.reason });
2740
- if (started) {
2741
- scheduleRetry();
2742
- }
2743
- });
2744
- current.addEventListener("error", (event) => {
2745
- emitSocketEvent("error", { error: event });
2746
- });
2747
- } catch (error) {
2748
- socket = void 0;
2749
- emitSocketEvent("error", { error });
2750
- if (started) {
2751
- scheduleRetry();
2740
+ const { url, protocols } = this.options;
2741
+ const current = new WebSocket(url, protocols ?? []);
2742
+ this._socket = current;
2743
+ this.setStatus("connecting");
2744
+ current.addEventListener("open", () => this.onOpen(current));
2745
+ current.addEventListener("message", (event) => this.onMessage(current, event));
2746
+ current.addEventListener("close", (event) => this.onClose(current, event));
2747
+ current.addEventListener("error", (event) => this.onError(current, event));
2748
+ }
2749
+ onOpen(current) {
2750
+ if (this._socket === current) {
2751
+ this._retryAttempt = 0;
2752
+ this.setStatus("connected");
2753
+ this.flush();
2754
+ this.dispatch("open", { url: this.options.url });
2755
+ }
2756
+ }
2757
+ onMessage(current, event) {
2758
+ if (this._socket === current) {
2759
+ this.dispatch("message", parsePayload(event.data));
2760
+ }
2761
+ }
2762
+ onClose(current, event) {
2763
+ if (this._socket === current) {
2764
+ this._socket = void 0;
2765
+ this.dispatch("close", event);
2766
+ if (this._started) {
2767
+ this.retry();
2752
2768
  }
2753
2769
  }
2754
- };
2755
- const start = () => {
2756
- if (!started) {
2757
- started = true;
2758
- connect();
2759
- }
2760
- };
2761
- const stop = () => {
2762
- started = false;
2763
- if (retryTimer) {
2764
- clearTimeout(retryTimer);
2770
+ }
2771
+ onError(current, event) {
2772
+ if (this._socket === current) {
2773
+ this.dispatch("error", serializeError(event.error));
2765
2774
  }
2766
- retryTimer = void 0;
2767
- const current = socket;
2768
- socket = void 0;
2769
- current?.close();
2770
- currentStatus = "stopped";
2771
- };
2772
- const reconnect = () => {
2773
- if (!started) {
2774
- return;
2775
+ }
2776
+ dispatch(topic, parsed) {
2777
+ const { bus, origin } = this.options;
2778
+ bus.dispatch({
2779
+ id: createId(),
2780
+ topic,
2781
+ occurredAt: Date.now(),
2782
+ ...origin ? { origin } : {},
2783
+ parsed,
2784
+ serialized: JSON.stringify(parsed)
2785
+ });
2786
+ }
2787
+ flush() {
2788
+ if (this._socket && this._socket.readyState === WebSocket.OPEN) {
2789
+ for (const message of this._queue) {
2790
+ this._socket.send(message);
2791
+ }
2792
+ this.clearQueue();
2775
2793
  }
2776
- if (retryTimer) {
2777
- clearTimeout(retryTimer);
2778
- retryTimer = void 0;
2794
+ }
2795
+ retry() {
2796
+ this.setStatus("reconnecting");
2797
+ this._retryTimer = setTimeout(
2798
+ () => {
2799
+ this._retryTimer = void 0;
2800
+ this.connect();
2801
+ },
2802
+ getRetryDelay(this._retryAttempt, this.options.reconnect ?? {})
2803
+ );
2804
+ this._retryAttempt = this._retryAttempt + 1;
2805
+ }
2806
+ setStatus(status) {
2807
+ this._status = status || "idle";
2808
+ }
2809
+ clearQueue() {
2810
+ this._queue = [];
2811
+ }
2812
+ clearTimer() {
2813
+ if (this._retryTimer) {
2814
+ clearTimeout(this._retryTimer);
2779
2815
  }
2780
- const current = socket;
2781
- socket = void 0;
2782
- current?.close();
2783
- connect();
2784
- };
2785
- return { start, stop, reconnect, status: () => currentStatus };
2816
+ this._retryTimer = void 0;
2817
+ }
2786
2818
  };
2819
+ var createWebSocket = (options) => new WebSocketConnection(options);
2787
2820
 
2788
2821
  // src/helpers/catchError.ts
2789
2822
  var catchError = (callback) => new Promise((resolve, reject) => {
package/dist/index.d.cts CHANGED
@@ -162,6 +162,7 @@ type WSClient = {
162
162
  stop(): void;
163
163
  reconnect(): void;
164
164
  status(): WebSocketStatus;
165
+ send(data: string): void;
165
166
  };
166
167
  type BindingEventMap = Record<string, Input>;
167
168
  type BusBindingKey<TEvents extends object> = {
package/dist/index.d.ts CHANGED
@@ -162,6 +162,7 @@ type WSClient = {
162
162
  stop(): void;
163
163
  reconnect(): void;
164
164
  status(): WebSocketStatus;
165
+ send(data: string): void;
165
166
  };
166
167
  type BindingEventMap = Record<string, Input>;
167
168
  type BusBindingKey<TEvents extends object> = {
package/dist/index.js CHANGED
@@ -2637,115 +2637,148 @@ var createWS = (options) => {
2637
2637
  return { start, stop, reconnect, status: () => currentStatus };
2638
2638
  };
2639
2639
 
2640
+ // src/helpers/parsePayload.ts
2641
+ var parsePayload = (data) => {
2642
+ if (typeof data === "string") {
2643
+ try {
2644
+ return JSON.parse(data);
2645
+ } catch {
2646
+ return data;
2647
+ }
2648
+ }
2649
+ return data;
2650
+ };
2651
+
2640
2652
  // src/createWebSocket.ts
2641
- var createWebSocket = (options) => {
2642
- let socket;
2643
- let retryTimer;
2644
- let retryAttempt = 0;
2645
- let started = false;
2646
- let currentStatus = "idle";
2647
- const emitSocketEvent = (topic, payload) => {
2648
- options.bus.dispatch({
2649
- id: createId(),
2650
- topic,
2651
- occurredAt: Date.now(),
2652
- ...options.origin ? { origin: options.origin } : {},
2653
- parsed: payload,
2654
- serialized: JSON.stringify(payload)
2655
- });
2656
- };
2657
- const scheduleRetry = () => {
2658
- const delay = getRetryDelay(retryAttempt, options.reconnect ?? {});
2659
- const attempt = retryAttempt + 1;
2660
- currentStatus = "reconnecting";
2661
- retryAttempt = attempt;
2662
- retryTimer = setTimeout(() => {
2663
- retryTimer = void 0;
2664
- connect();
2665
- }, delay);
2666
- };
2667
- const connect = () => {
2668
- if (!started || socket) {
2653
+ var WebSocketConnection = class {
2654
+ constructor(options) {
2655
+ this.options = options;
2656
+ }
2657
+ options;
2658
+ _socket;
2659
+ _retryTimer;
2660
+ _retryAttempt = 0;
2661
+ _queue = [];
2662
+ _started = false;
2663
+ _status = "idle";
2664
+ start() {
2665
+ if (!this._started) {
2666
+ this._started = true;
2667
+ this.connect();
2668
+ }
2669
+ }
2670
+ stop() {
2671
+ this._started = false;
2672
+ this.clearTimer();
2673
+ this.clearQueue();
2674
+ const current = this._socket;
2675
+ this._socket = void 0;
2676
+ current?.close();
2677
+ this.setStatus("stopped");
2678
+ }
2679
+ reconnect() {
2680
+ if (this._started) {
2681
+ this.clearTimer();
2682
+ const current = this._socket;
2683
+ this._socket = void 0;
2684
+ current?.close();
2685
+ this.connect();
2686
+ }
2687
+ }
2688
+ status() {
2689
+ return this._status;
2690
+ }
2691
+ send(data) {
2692
+ if (this._socket?.readyState === WebSocket.OPEN) {
2693
+ this._socket.send(data);
2694
+ } else {
2695
+ this._queue.push(data);
2696
+ }
2697
+ }
2698
+ connect() {
2699
+ if (!this._started || this._socket) {
2669
2700
  return;
2670
2701
  }
2671
- currentStatus = "connecting";
2672
- try {
2673
- const current = new WebSocket(options.url, options.protocols ?? []);
2674
- socket = current;
2675
- current.addEventListener("open", () => {
2676
- if (socket === current) {
2677
- retryAttempt = 0;
2678
- currentStatus = "connected";
2679
- emitSocketEvent("open", { url: options.url });
2680
- }
2681
- });
2682
- current.addEventListener("message", (event) => {
2683
- if (socket !== current) {
2684
- return;
2685
- }
2686
- let parsed = event.data;
2687
- if (typeof parsed === "string") {
2688
- try {
2689
- parsed = JSON.parse(parsed);
2690
- } catch {
2691
- parsed = event.data;
2692
- }
2693
- }
2694
- emitSocketEvent("message", parsed);
2695
- });
2696
- current.addEventListener("close", (event) => {
2697
- if (socket !== current) {
2698
- return;
2699
- }
2700
- socket = void 0;
2701
- emitSocketEvent("close", { code: event.code, reason: event.reason });
2702
- if (started) {
2703
- scheduleRetry();
2704
- }
2705
- });
2706
- current.addEventListener("error", (event) => {
2707
- emitSocketEvent("error", { error: event });
2708
- });
2709
- } catch (error) {
2710
- socket = void 0;
2711
- emitSocketEvent("error", { error });
2712
- if (started) {
2713
- scheduleRetry();
2702
+ const { url, protocols } = this.options;
2703
+ const current = new WebSocket(url, protocols ?? []);
2704
+ this._socket = current;
2705
+ this.setStatus("connecting");
2706
+ current.addEventListener("open", () => this.onOpen(current));
2707
+ current.addEventListener("message", (event) => this.onMessage(current, event));
2708
+ current.addEventListener("close", (event) => this.onClose(current, event));
2709
+ current.addEventListener("error", (event) => this.onError(current, event));
2710
+ }
2711
+ onOpen(current) {
2712
+ if (this._socket === current) {
2713
+ this._retryAttempt = 0;
2714
+ this.setStatus("connected");
2715
+ this.flush();
2716
+ this.dispatch("open", { url: this.options.url });
2717
+ }
2718
+ }
2719
+ onMessage(current, event) {
2720
+ if (this._socket === current) {
2721
+ this.dispatch("message", parsePayload(event.data));
2722
+ }
2723
+ }
2724
+ onClose(current, event) {
2725
+ if (this._socket === current) {
2726
+ this._socket = void 0;
2727
+ this.dispatch("close", event);
2728
+ if (this._started) {
2729
+ this.retry();
2714
2730
  }
2715
2731
  }
2716
- };
2717
- const start = () => {
2718
- if (!started) {
2719
- started = true;
2720
- connect();
2721
- }
2722
- };
2723
- const stop = () => {
2724
- started = false;
2725
- if (retryTimer) {
2726
- clearTimeout(retryTimer);
2732
+ }
2733
+ onError(current, event) {
2734
+ if (this._socket === current) {
2735
+ this.dispatch("error", serializeError(event.error));
2727
2736
  }
2728
- retryTimer = void 0;
2729
- const current = socket;
2730
- socket = void 0;
2731
- current?.close();
2732
- currentStatus = "stopped";
2733
- };
2734
- const reconnect = () => {
2735
- if (!started) {
2736
- return;
2737
+ }
2738
+ dispatch(topic, parsed) {
2739
+ const { bus, origin } = this.options;
2740
+ bus.dispatch({
2741
+ id: createId(),
2742
+ topic,
2743
+ occurredAt: Date.now(),
2744
+ ...origin ? { origin } : {},
2745
+ parsed,
2746
+ serialized: JSON.stringify(parsed)
2747
+ });
2748
+ }
2749
+ flush() {
2750
+ if (this._socket && this._socket.readyState === WebSocket.OPEN) {
2751
+ for (const message of this._queue) {
2752
+ this._socket.send(message);
2753
+ }
2754
+ this.clearQueue();
2737
2755
  }
2738
- if (retryTimer) {
2739
- clearTimeout(retryTimer);
2740
- retryTimer = void 0;
2756
+ }
2757
+ retry() {
2758
+ this.setStatus("reconnecting");
2759
+ this._retryTimer = setTimeout(
2760
+ () => {
2761
+ this._retryTimer = void 0;
2762
+ this.connect();
2763
+ },
2764
+ getRetryDelay(this._retryAttempt, this.options.reconnect ?? {})
2765
+ );
2766
+ this._retryAttempt = this._retryAttempt + 1;
2767
+ }
2768
+ setStatus(status) {
2769
+ this._status = status || "idle";
2770
+ }
2771
+ clearQueue() {
2772
+ this._queue = [];
2773
+ }
2774
+ clearTimer() {
2775
+ if (this._retryTimer) {
2776
+ clearTimeout(this._retryTimer);
2741
2777
  }
2742
- const current = socket;
2743
- socket = void 0;
2744
- current?.close();
2745
- connect();
2746
- };
2747
- return { start, stop, reconnect, status: () => currentStatus };
2778
+ this._retryTimer = void 0;
2779
+ }
2748
2780
  };
2781
+ var createWebSocket = (options) => new WebSocketConnection(options);
2749
2782
 
2750
2783
  // src/helpers/catchError.ts
2751
2784
  var catchError = (callback) => new Promise((resolve, reject) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slapflow",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Chain actions behavior runtime",
5
5
  "license": "ISC",
6
6
  "author": "Sergey Khalilov",