@stream-io/feeds-client 0.2.4 → 0.2.5

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.
@@ -5715,6 +5715,11 @@ function handleUserUpdated(event) {
5715
5715
  });
5716
5716
  }
5717
5717
 
5718
+ var UnhandledErrorType;
5719
+ (function (UnhandledErrorType) {
5720
+ UnhandledErrorType["ReconnectionReconciliation"] = "reconnection-reconciliation";
5721
+ })(UnhandledErrorType || (UnhandledErrorType = {}));
5722
+
5718
5723
  class FeedsClient extends FeedsApi {
5719
5724
  constructor(apiKey, options) {
5720
5725
  const tokenManager = new TokenManager();
@@ -5724,6 +5729,22 @@ class FeedsClient extends FeedsApi {
5724
5729
  this.eventDispatcher = new EventDispatcher();
5725
5730
  this.activeFeeds = {};
5726
5731
  this.healthyConnectionChangedEventCount = 0;
5732
+ this.recoverOnReconnect = async () => {
5733
+ this.healthyConnectionChangedEventCount++;
5734
+ // we skip the first event as we could potentially be querying twice
5735
+ if (this.healthyConnectionChangedEventCount > 1) {
5736
+ const entries = Object.entries(this.activeFeeds);
5737
+ const results = await Promise.allSettled(entries.map(([, feed]) => feed.synchronize()));
5738
+ const failures = results.flatMap((result, index) => result.status === 'rejected'
5739
+ ? [{ feed: entries[index][0], reason: result.reason }]
5740
+ : []);
5741
+ this.eventDispatcher.dispatch({
5742
+ type: 'errors.unhandled',
5743
+ error_type: UnhandledErrorType.ReconnectionReconciliation,
5744
+ failures,
5745
+ });
5746
+ }
5747
+ };
5727
5748
  this.pollFromState = (id) => this.polls_by_id.get(id);
5728
5749
  this.connectUser = async (user, tokenProvider) => {
5729
5750
  if (this.state.getLatestValue().connected_user !== undefined ||
@@ -5819,18 +5840,18 @@ class FeedsClient extends FeedsApi {
5819
5840
  };
5820
5841
  this.getOrCreateActiveFeed = (group, id, data, watch) => {
5821
5842
  const fid = `${group}:${id}`;
5822
- if (this.activeFeeds[fid]) {
5823
- const feed = this.activeFeeds[fid];
5824
- if (watch && !feed.currentState.watch) {
5825
- handleWatchStarted.bind(feed)();
5826
- }
5827
- return feed;
5843
+ if (!this.activeFeeds[fid]) {
5844
+ this.activeFeeds[fid] = new Feed(this, group, id, data, watch);
5828
5845
  }
5829
- else {
5830
- const feed = new Feed(this, group, id, data, watch);
5831
- this.activeFeeds[fid] = feed;
5832
- return feed;
5846
+ const feed = this.activeFeeds[fid];
5847
+ if (!feed.currentState.watch) {
5848
+ // feed isn't watched and may be stale, update it
5849
+ if (data)
5850
+ handleFeedUpdated.call(feed, { feed: data });
5851
+ if (watch)
5852
+ handleWatchStarted.call(feed);
5833
5853
  }
5854
+ return feed;
5834
5855
  };
5835
5856
  this.state = new StateStore({
5836
5857
  connected_user: undefined,
@@ -5848,13 +5869,7 @@ class FeedsClient extends FeedsApi {
5848
5869
  const { online } = event;
5849
5870
  this.state.partialNext({ is_ws_connection_healthy: online });
5850
5871
  if (online) {
5851
- this.healthyConnectionChangedEventCount++;
5852
- // we skip the first event as we could potentially be querying twice
5853
- if (this.healthyConnectionChangedEventCount > 1) {
5854
- for (const activeFeed of Object.values(this.activeFeeds)) {
5855
- activeFeed.synchronize();
5856
- }
5857
- }
5872
+ this.recoverOnReconnect();
5858
5873
  }
5859
5874
  else {
5860
5875
  for (const activeFeed of Object.values(this.activeFeeds)) {
@@ -5968,7 +5983,7 @@ class FeedsClient extends FeedsApi {
5968
5983
  }
5969
5984
  async queryFeeds(request) {
5970
5985
  const response = await this._queryFeeds(request);
5971
- const feeds = response.feeds.map((f) => this.getOrCreateActiveFeed(f.group_id, f.id, f, request?.watch));
5986
+ const feeds = response.feeds.map((feedResponse) => this.getOrCreateActiveFeed(feedResponse.group_id, feedResponse.id, feedResponse, request?.watch));
5972
5987
  return {
5973
5988
  feeds,
5974
5989
  next: response.next,