@arca-network/sdk 2.14.0 → 2.16.0

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/streams.js CHANGED
@@ -711,6 +711,15 @@ exports.AggregationWatchStream = AggregationWatchStream;
711
711
  * The stream's mids interest is exactly the markets its rows hold
712
712
  * positions in, recomputed whenever the rows change, so re-marking a
713
713
  * 200-row board costs the board's markets, not every market on the venue.
714
+ *
715
+ * With a `window`, rows carry `pnl` and `volume` figures, each with its
716
+ * own `from` and `status`. `equity`, `unrealizedValue` and `pnl` re-mark
717
+ * together in exact decimals; `volume` never moves with mids.
718
+ *
719
+ * Recovery: a delivery gap or a server `stream.resync` re-reads the whole
720
+ * scope under the same watch; a reconnect re-creates the watch, retrying
721
+ * with backoff until it succeeds. While either is under way {@link stale}
722
+ * is true and the rows are the last ones delivered, never zeros.
714
723
  */
715
724
  class ProjectionWatchStream extends WatchStream {
716
725
  ws;
@@ -719,6 +728,7 @@ class ProjectionWatchStream extends WatchStream {
719
728
  refreshWatch;
720
729
  exchange;
721
730
  updateScope;
731
+ window;
722
732
  /** Latest re-marked valuations, keyed by object path. */
723
733
  valuations = {};
724
734
  _watchId;
@@ -728,15 +738,27 @@ class ProjectionWatchStream extends WatchStream {
728
738
  structural = {};
729
739
  destroyWatch;
730
740
  refreshing = false;
741
+ refreshAttempt = 0;
742
+ refreshTimer;
743
+ resyncing = false;
744
+ resyncAgain = false;
731
745
  _scope;
732
746
  /** Markets currently acquired on the mids subscription for re-marking. */
733
747
  midsMarkets = [];
734
- /** Serializes setPaths/setPrefix so a later call always wins. */
748
+ /** Serializes setPaths/setPrefix and resyncs so a later call always wins. */
735
749
  scopeUpdate = Promise.resolve();
736
750
  get watchId() { return this._watchId; }
737
751
  /** The interest set this watch currently covers. */
738
752
  get scope() { return { ...this._scope, ...(this._scope.paths ? { paths: [...this._scope.paths] } : {}) }; }
739
- constructor(ws, projection, fields, watchId, initialValuations, destroyWatch, refreshWatch, exchange = 'sim', scope = {}, updateScope) {
753
+ /**
754
+ * True while the rows may be behind the server: the connection is down,
755
+ * the watch is being re-created, or a gap is being re-read. The rows are
756
+ * the last ones delivered; show them as stale, not as zero.
757
+ */
758
+ get stale() { return this.state !== 'connected' || this.resyncing; }
759
+ constructor(ws, projection, fields, watchId, initialValuations, destroyWatch, refreshWatch, exchange = 'sim', scope = {}, updateScope,
760
+ /** The rolling window the rows' `pnl` / `volume` figures cover, if any. */
761
+ window) {
740
762
  super();
741
763
  this.ws = ws;
742
764
  this.projection = projection;
@@ -744,6 +766,7 @@ class ProjectionWatchStream extends WatchStream {
744
766
  this.refreshWatch = refreshWatch;
745
767
  this.exchange = exchange;
746
768
  this.updateScope = updateScope;
769
+ this.window = window;
747
770
  this._watchId = watchId;
748
771
  this._scope = scope;
749
772
  for (const v of initialValuations)
@@ -760,15 +783,20 @@ class ProjectionWatchStream extends WatchStream {
760
783
  }),
761
784
  // A rotation swaps the socket without an outage; this watch lives on
762
785
  // the retiring connection's server side, so recreate it on the new one.
763
- this.ws.onRotated(() => this.handleReconnect()), this.ws.onProjectionValuations((eventWatchId, vals, _event, removed) => {
786
+ this.ws.onRotated(() => this.handleReconnect()),
787
+ // object.valuation frames are critical: a lost one leaves a row
788
+ // wrong until that account changes again.
789
+ this.ws.onGap(() => this.resync()), this.ws.onProjectionValuations((eventWatchId, vals, _event, removed) => {
764
790
  if (eventWatchId !== this._watchId)
765
791
  return;
766
- for (const [path, v] of Object.entries(vals))
767
- this.structural[path] = v;
792
+ // Removals first: an account replaced at the same path arrives as
793
+ // its new row, which a removal must not delete.
768
794
  if (removed) {
769
795
  for (const path of removed)
770
796
  delete this.structural[path];
771
797
  }
798
+ for (const [path, v] of Object.entries(vals))
799
+ this.structural[path] = v;
772
800
  this.syncMidsInterest();
773
801
  this.remarkAndEmit();
774
802
  }), this.ws.onMidsUpdated((mids) => {
@@ -800,15 +828,55 @@ class ProjectionWatchStream extends WatchStream {
800
828
  const valuations = await this.updateScope(this._watchId, scope);
801
829
  if (this.isClosed || this._scope !== scope)
802
830
  return; // superseded
803
- this.structural = {};
804
- for (const v of valuations)
805
- this.structural[v.path] = v;
806
- this.syncMidsInterest();
807
- this.remarkAndEmit();
831
+ this.replaceRows(valuations);
808
832
  };
809
833
  this.scopeUpdate = this.scopeUpdate.then(run, run);
810
834
  return this.scopeUpdate;
811
835
  }
836
+ /**
837
+ * Re-reads the whole scope under the same watch after a delivery gap or
838
+ * a server resync. A reconnect already re-reads, so it is skipped then.
839
+ * If the re-read fails the watch is re-created.
840
+ */
841
+ resync() {
842
+ if (this.isClosed || this.state !== 'connected' || this.refreshing)
843
+ return;
844
+ if (this.resyncing) {
845
+ this.resyncAgain = true;
846
+ return;
847
+ }
848
+ if (!this.updateScope) {
849
+ this.handleReconnect();
850
+ return;
851
+ }
852
+ this.resyncing = true;
853
+ const scope = this._scope;
854
+ const update = this.updateScope;
855
+ const run = async () => {
856
+ if (this.isClosed || this._scope !== scope)
857
+ return; // a scope change re-read already
858
+ const valuations = await update(this._watchId, scope);
859
+ if (this.isClosed || this._scope !== scope)
860
+ return;
861
+ this.replaceRows(valuations);
862
+ };
863
+ this.scopeUpdate = this.scopeUpdate.then(run, run)
864
+ .catch(() => { this.handleReconnect(); })
865
+ .finally(() => {
866
+ this.resyncing = false;
867
+ if (this.resyncAgain) {
868
+ this.resyncAgain = false;
869
+ this.resync();
870
+ }
871
+ });
872
+ }
873
+ replaceRows(valuations) {
874
+ this.structural = {};
875
+ for (const v of valuations)
876
+ this.structural[v.path] = v;
877
+ this.syncMidsInterest();
878
+ this.remarkAndEmit();
879
+ }
812
880
  /**
813
881
  * Declares the markets the rows hold positions in as this stream's mids
814
882
  * interest. The manager unions interests across consumers and re-sends
@@ -844,9 +912,19 @@ class ProjectionWatchStream extends WatchStream {
844
912
  this.valuations = out;
845
913
  this.emit(out);
846
914
  }
915
+ /**
916
+ * Re-creates the watch with the current scope (and window). A failure
917
+ * leaves the stream `reconnecting` with its last rows and retries with
918
+ * backoff (1s doubling to 30s) until a re-create succeeds or the stream
919
+ * closes.
920
+ */
847
921
  handleReconnect() {
848
- if (this.refreshing)
922
+ if (this.refreshing || this.isClosed)
849
923
  return;
924
+ if (this.refreshTimer) {
925
+ clearTimeout(this.refreshTimer);
926
+ this.refreshTimer = undefined;
927
+ }
850
928
  this.refreshing = true;
851
929
  this.refreshWatch(this._scope)
852
930
  .then(({ watchId: newId, valuations, destroyWatch: newDestroy }) => {
@@ -857,19 +935,27 @@ class ProjectionWatchStream extends WatchStream {
857
935
  }
858
936
  this._watchId = newId;
859
937
  this.destroyWatch = newDestroy;
860
- this.structural = {};
861
- for (const v of valuations)
862
- this.structural[v.path] = v;
863
- this.syncMidsInterest();
864
- this.remarkAndEmit();
938
+ this.refreshAttempt = 0;
939
+ this.replaceRows(valuations);
865
940
  this.setState('connected');
866
941
  })
867
942
  .catch(() => {
868
- this.setState('connected');
943
+ if (this.isClosed)
944
+ return;
945
+ this.setState('reconnecting');
946
+ const delay = Math.min(30_000, 1_000 * 2 ** this.refreshAttempt);
947
+ this.refreshAttempt++;
948
+ this.refreshTimer = setTimeout(() => {
949
+ this.refreshTimer = undefined;
950
+ this.handleReconnect();
951
+ }, delay);
869
952
  })
870
953
  .finally(() => { this.refreshing = false; });
871
954
  }
872
955
  doClose() {
956
+ if (this.refreshTimer)
957
+ clearTimeout(this.refreshTimer);
958
+ this.refreshTimer = undefined;
873
959
  this.unsubs.forEach(u => u());
874
960
  if (this.midsMarkets.length > 0)
875
961
  this.ws.releaseMids(this.midsMarkets);