tardis-dev 18.11.1 → 18.11.3

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.
@@ -1,6 +1,5 @@
1
1
  import { Writable } from 'stream'
2
- import { setTimeout as sleep } from 'node:timers/promises'
3
- import { batch, getJSON } from '../handy.ts'
2
+ import { getJSON } from '../handy.ts'
4
3
  import { Filter } from '../types.ts'
5
4
  import { MultiConnectionRealTimeFeedBase, PoolingClientBase, RealTimeFeedBase, Sequence } from './realtimefeed.ts'
6
5
 
@@ -29,24 +28,8 @@ export class WhiteBitRealTimeFeed extends MultiConnectionRealTimeFeedBase {
29
28
  throw new Error('WhiteBitRealTimeFeed requires explicitly specified symbols when subscribing to live feed')
30
29
  }
31
30
  }
32
- const webSocketSymbols = [...new Set(webSocketFilters.flatMap((filter) => filter.symbols!))]
33
- const spotSymbols = webSocketSymbols.filter((symbol) => !symbol.endsWith('_PERP'))
34
- const perpetualSymbols = webSocketSymbols.filter((symbol) => symbol.endsWith('_PERP'))
35
- let connectionIndex = 0
36
-
37
- for (const marketSymbols of [spotSymbols, perpetualSymbols]) {
38
- for (const group of batch(marketSymbols, 50)) {
39
- yield new WhiteBitWebSocketRealTimeFeed(
40
- exchange,
41
- webSocketFilters
42
- .map((filter) => ({ ...filter, symbols: filter.symbols!.filter((symbol) => group.includes(symbol)) }))
43
- .filter((filter) => filter.symbols.length > 0),
44
- timeoutIntervalMS,
45
- onError,
46
- validateSequence,
47
- connectionIndex++ * 50
48
- )
49
- }
31
+ if (webSocketFilters.length > 0) {
32
+ yield new WhiteBitWebSocketRealTimeFeed(exchange, webSocketFilters, timeoutIntervalMS, onError, validateSequence)
50
33
  }
51
34
 
52
35
  const futuresFilters = filters.filter((filter) => filter.channel === 'futures_update')
@@ -65,22 +48,6 @@ export class WhiteBitRealTimeFeed extends MultiConnectionRealTimeFeedBase {
65
48
  export class WhiteBitWebSocketRealTimeFeed extends RealTimeFeedBase {
66
49
  protected readonly wssURL = 'wss://wss.whitebit.com/ws'
67
50
 
68
- constructor(
69
- exchange: string,
70
- filters: Filter<string>[],
71
- timeoutIntervalMS: number | undefined,
72
- onError?: (error: Error) => void,
73
- validateSequence?: boolean,
74
- private readonly _startDelayMS = 0
75
- ) {
76
- super(exchange, filters, timeoutIntervalMS, onError, validateSequence)
77
- }
78
-
79
- protected async getWebSocketUrl() {
80
- await sleep(this._startDelayMS)
81
- return super.getWebSocketUrl()
82
- }
83
-
84
51
  protected mapToSubscribeMessages(filters: Filter<string>[]) {
85
52
  let id = 0
86
53
  return filters.flatMap(({ channel, symbols }) => {
package/src/stream.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { debug } from './debug.ts'
2
- import { createNormalizedSymbolFilter, getFilters, normalizeMessages } from './handy.ts'
2
+ import { abortableWait, createNormalizedSymbolFilter, getFilters, getRetryDelay, normalizeMessages } from './handy.ts'
3
3
  import { MapperFactory } from './mappers/index.ts'
4
4
  import { createManagedRealTimeIterator, type ManagedRealTimeIterator } from './realtimeiterator.ts'
5
5
  import { createRealTimeFeed, getRealTimeFeedFactory } from './realtimefeeds/index.ts'
@@ -15,7 +15,7 @@ type StreamState = {
15
15
  }
16
16
 
17
17
  type NormalizedStreamState = {
18
- closed: boolean
18
+ closeController: AbortController
19
19
  activeIterator?: ManagedRealTimeIterator<any>
20
20
  }
21
21
 
@@ -98,8 +98,9 @@ async function* _streamNormalized<T extends Exchange, U extends MapperFactory<T,
98
98
  ...normalizers: U
99
99
  ): AsyncIterableIterator<StreamNormalizedMessage<U, Z>> {
100
100
  getRealTimeFeedFactory(exchange)
101
+ let retries = 0
101
102
 
102
- while (state.closed === false) {
103
+ while (state.closeController.signal.aborted === false) {
103
104
  try {
104
105
  const createMappers = (localTimestamp: Date) => normalizers.map((m) => m(exchange, localTimestamp))
105
106
  const mappers = createMappers(new Date())
@@ -132,17 +133,20 @@ async function* _streamNormalized<T extends Exchange, U extends MapperFactory<T,
132
133
  )
133
134
 
134
135
  for await (const message of normalizedMessages) {
136
+ retries = 0
135
137
  yield message
136
138
  }
137
139
  } catch (error: any) {
138
- if (state.closed) {
140
+ if (state.closeController.signal.aborted) {
139
141
  return
140
142
  }
141
143
 
142
144
  if (onError !== undefined) {
143
145
  onError(error)
144
146
  }
145
- debug('%s normalize messages error: %o, retrying with new connection...', exchange, error)
147
+ retries++
148
+ const retryDelay = getRetryDelay(error instanceof Error ? error : new Error(String(error)), retries)
149
+ debug('%s normalize messages error: %o, retrying with new connection in %dms...', exchange, error, retryDelay)
146
150
  if (withDisconnectMessages) {
147
151
  // yield it as disconnect as well if flag is set
148
152
  const disconnect: Disconnect = {
@@ -154,6 +158,8 @@ async function* _streamNormalized<T extends Exchange, U extends MapperFactory<T,
154
158
 
155
159
  yield disconnect as any
156
160
  }
161
+
162
+ await abortableWait(retryDelay, state.closeController.signal)
157
163
  }
158
164
  }
159
165
  }
@@ -201,7 +207,7 @@ export function streamNormalized<T extends Exchange, U extends MapperFactory<T,
201
207
  }: StreamNormalizedOptions<T, Z>,
202
208
  ...normalizers: U
203
209
  ): AsyncIterableIterator<StreamNormalizedMessage<U, Z>> {
204
- const state: NormalizedStreamState = { closed: false }
210
+ const state: NormalizedStreamState = { closeController: new AbortController() }
205
211
  const iterator = _streamNormalized(
206
212
  { exchange, symbols, timeoutIntervalMS, validateSequence, withDisconnectMessages, onError },
207
213
  state,
@@ -209,7 +215,7 @@ export function streamNormalized<T extends Exchange, U extends MapperFactory<T,
209
215
  )
210
216
 
211
217
  return createManagedRealTimeIterator(iterator, () => {
212
- state.closed = true
218
+ state.closeController.abort()
213
219
  const activeIterator = state.activeIterator
214
220
  state.activeIterator = undefined
215
221
  return activeIterator?.return?.()