@experteam-mx/ngx-services 20.8.9 → 20.8.10

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.
@@ -5671,28 +5671,40 @@ class WebSocketsService {
5671
5671
  return channel.trigger(`client-${eventName}`, data);
5672
5672
  }
5673
5673
  /**
5674
- * Subscribes to a specified channel, retrying multiple times upon failure.
5674
+ * Subscribes to a specified channel and resolves when Pusher reports success.
5675
5675
  *
5676
5676
  * @param {string} channelName - The name of the channel to be subscribed to.
5677
5677
  * @return {Promise<Channel>} A promise that resolves to the subscribed channel object if successful.
5678
- * @throws {Error} If the subscription fails after the maximum number of retry attempts.
5678
+ * @throws {Error} If the subscription fails.
5679
5679
  */
5680
5680
  async channelSub(channelName) {
5681
5681
  const pusher = await this.ensureConnected();
5682
5682
  this.refreshChannelAuthHeader();
5683
- const attempts = 3;
5684
- for (let attempt = 0; attempt <= attempts; attempt++) {
5685
- const channel = pusher.channel(channelName);
5686
- if (channel?.subscribed) {
5687
- return channel;
5688
- }
5689
- this.refreshChannelAuthHeader();
5690
- pusher.subscribe(channelName);
5691
- if (attempt < attempts) {
5692
- await this.wait();
5693
- }
5683
+ const existing = pusher.channel(channelName);
5684
+ if (existing?.subscribed) {
5685
+ return existing;
5694
5686
  }
5695
- throw new Error(`Failed to subscribe to channel "${channelName}" after ${attempts} attempts.`);
5687
+ const channel = pusher.subscribe(channelName);
5688
+ if (channel.subscribed) {
5689
+ return channel;
5690
+ }
5691
+ await new Promise((resolve, reject) => {
5692
+ const onSuccess = () => {
5693
+ cleanup();
5694
+ resolve();
5695
+ };
5696
+ const onError = () => {
5697
+ cleanup();
5698
+ reject(new Error(`Failed to subscribe to channel "${channelName}".`));
5699
+ };
5700
+ const cleanup = () => {
5701
+ channel.unbind('pusher:subscription_succeeded', onSuccess);
5702
+ channel.unbind('pusher:subscription_error', onError);
5703
+ };
5704
+ channel.bind('pusher:subscription_succeeded', onSuccess);
5705
+ channel.bind('pusher:subscription_error', onError);
5706
+ });
5707
+ return channel;
5696
5708
  }
5697
5709
  /**
5698
5710
  * Binds an event listener to a specified event on a given channel using Pusher or a Channel object,
@@ -5704,17 +5716,16 @@ class WebSocketsService {
5704
5716
  */
5705
5717
  eventBind(channel, event) {
5706
5718
  return new Observable((subscriber) => {
5719
+ if (channel?.name) {
5720
+ channel.bind(event, (data) => subscriber.next(data));
5721
+ return;
5722
+ }
5707
5723
  void (async () => {
5708
5724
  try {
5709
5725
  const pusher = await this.ensureConnected();
5710
5726
  this.refreshChannelAuthHeader();
5711
- if (channel?.name) {
5712
- channel.bind(event, (data) => subscriber.next(data));
5713
- }
5714
- else {
5715
- pusher.subscribe(channel)
5716
- .bind(event, (data) => subscriber.next(data));
5717
- }
5727
+ pusher.subscribe(channel)
5728
+ .bind(event, (data) => subscriber.next(data));
5718
5729
  }
5719
5730
  catch (error) {
5720
5731
  subscriber.error(error);
@@ -5733,6 +5744,14 @@ class WebSocketsService {
5733
5744
  eventUnbind(channel, event) {
5734
5745
  return new Observable((subscriber) => channel.unbind(event, (data) => subscriber.next(data)));
5735
5746
  }
5747
+ /**
5748
+ * Unsubscribes from a channel by name.
5749
+ *
5750
+ * @param {string} channelName - The channel name previously passed to subscribe.
5751
+ */
5752
+ unsubscribe(channelName) {
5753
+ this.pusher?.unsubscribe(channelName);
5754
+ }
5736
5755
  async ensureConnected() {
5737
5756
  if (this.pusher)
5738
5757
  return this.pusher;
@@ -5761,10 +5780,6 @@ class WebSocketsService {
5761
5780
  delete this.channelAuthHeaders['Authorization'];
5762
5781
  }
5763
5782
  }
5764
- async wait() {
5765
- const waitTime = 3 * 1000;
5766
- return new Promise((resolve) => setTimeout(resolve, waitTime));
5767
- }
5768
5783
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: WebSocketsService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
5769
5784
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: WebSocketsService, providedIn: 'root' });
5770
5785
  }