@metamask-previews/core-backend 8.1.0-preview-beea66146 → 8.1.0-preview-7b60eca

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/CHANGELOG.md CHANGED
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Fixed
11
+
12
+ - `OHLCVService` now flushes grace-period channels when subscribing to a different asset/interval, retries failed WebSocket unsubscribes with backoff before forcing reconnection, and only removes channel tracking after a successful unsubscribe ([#9678](https://github.com/MetaMask/core/pull/9678))
13
+
10
14
  ## [8.1.0]
11
15
 
12
16
  ### Changed
@@ -18,10 +18,11 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
18
18
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
19
19
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
20
20
  };
21
- var _OHLCVService_instances, _OHLCVService_messenger, _OHLCVService_trace, _OHLCVService_channels, _OHLCVService_mutex, _OHLCVService_chainsUp, _OHLCVService_subscribeInner, _OHLCVService_unsubscribeInner, _OHLCVService_performUnsubscribe, _OHLCVService_resubscribeActiveChannels, _OHLCVService_handleBarUpdate, _OHLCVService_handleSystemNotification, _OHLCVService_handleWebSocketStateChange, _OHLCVService_buildChannel;
21
+ var _OHLCVService_instances, _OHLCVService_messenger, _OHLCVService_trace, _OHLCVService_channels, _OHLCVService_mutex, _OHLCVService_chainsUp, _OHLCVService_subscribeInner, _OHLCVService_unsubscribeInner, _OHLCVService_flushOtherChannels, _OHLCVService_clearChannelTimers, _OHLCVService_unsubscribeChannelOnServer, _OHLCVService_scheduleUnsubscribeRetry, _OHLCVService_runUnsubRetryLoop, _OHLCVService_performUnsubscribe, _OHLCVService_resubscribeActiveChannels, _OHLCVService_handleBarUpdate, _OHLCVService_handleSystemNotification, _OHLCVService_handleWebSocketStateChange, _OHLCVService_buildChannel;
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.OHLCVService = exports.OHLCV_SERVICE_ALLOWED_EVENTS = exports.OHLCV_SERVICE_ALLOWED_ACTIONS = void 0;
24
24
  const async_mutex_1 = require("async-mutex");
25
+ const cockatiel_1 = require("cockatiel");
25
26
  const logger_js_1 = require("../../logger.cjs");
26
27
  const BackendWebSocketService_js_1 = require("../BackendWebSocketService.cjs");
27
28
  // =============================================================================
@@ -34,6 +35,14 @@ const SUBSCRIPTION_NAMESPACE = 'market-data.v1';
34
35
  const SYSTEM_NOTIFICATIONS_CHANNEL = `system-notifications.v1.${SUBSCRIPTION_NAMESPACE}`;
35
36
  /** Delay before actually unsubscribing from a channel after refCount reaches 0. */
36
37
  const GRACE_PERIOD_MS = 3000;
38
+ /** Backoff delays between failed WebSocket unsubscribe attempts. */
39
+ const UNSUB_RETRY_DELAYS_MS = [1000, 2000, 4000];
40
+ const unsubRetryPolicy = (0, cockatiel_1.retry)(cockatiel_1.handleAll, {
41
+ // Cockatiel stops retrying once the failure index reaches `maxAttempts`, so
42
+ // `length` here yields one initial attempt plus three delayed retries (4 total).
43
+ maxAttempts: UNSUB_RETRY_DELAYS_MS.length,
44
+ backoff: new cockatiel_1.IterableBackoff([...UNSUB_RETRY_DELAYS_MS]),
45
+ });
37
46
  exports.OHLCV_SERVICE_ALLOWED_ACTIONS = [
38
47
  'BackendWebSocketService:connect',
39
48
  'BackendWebSocketService:forceReconnection',
@@ -58,7 +67,9 @@ exports.OHLCV_SERVICE_ALLOWED_EVENTS = [
58
67
  *
59
68
  * Features:
60
69
  * - Reference counting: multiple UI consumers share one WebSocket subscription
61
- * - Grace-period unsubscribe: avoids rapid unsub/resub during navigation
70
+ * - Grace-period unsubscribe: reuses same-channel subs on rapid back navigation
71
+ * - Grace-period flush: immediately unsubscribes other channels on navigation
72
+ * - Unsubscribe retry: retries failed unsubs with backoff before force reconnect
62
73
  * - Idempotency: duplicate subscribe calls for the same channel are no-ops
63
74
  * - Reconnect resilience: resubscribes all active channels on reconnect
64
75
  * - Chain-status forwarding: listens to system-notifications for chain up/down
@@ -142,9 +153,7 @@ class OHLCVService {
142
153
  */
143
154
  destroy() {
144
155
  for (const entry of __classPrivateFieldGet(this, _OHLCVService_channels, "f").values()) {
145
- if (entry.gracePeriodTimer) {
146
- clearTimeout(entry.gracePeriodTimer);
147
- }
156
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_clearChannelTimers).call(this, entry);
148
157
  }
149
158
  __classPrivateFieldGet(this, _OHLCVService_channels, "f").clear();
150
159
  __classPrivateFieldGet(this, _OHLCVService_chainsUp, "f").clear();
@@ -154,13 +163,27 @@ class OHLCVService {
154
163
  exports.OHLCVService = OHLCVService;
155
164
  _OHLCVService_messenger = new WeakMap(), _OHLCVService_trace = new WeakMap(), _OHLCVService_channels = new WeakMap(), _OHLCVService_mutex = new WeakMap(), _OHLCVService_chainsUp = new WeakMap(), _OHLCVService_instances = new WeakSet(), _OHLCVService_subscribeInner = async function _OHLCVService_subscribeInner(channel) {
156
165
  const entry = __classPrivateFieldGet(this, _OHLCVService_channels, "f").get(channel);
157
- if (entry?.gracePeriodTimer) {
166
+ if (entry?.retryAbort) {
167
+ entry.retryAbort.abort();
168
+ entry.retryAbort = undefined;
169
+ entry.refCount = 1;
170
+ if (__classPrivateFieldGet(this, _OHLCVService_messenger, "f").call('BackendWebSocketService:channelHasSubscription', channel)) {
171
+ await __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_flushOtherChannels).call(this, channel);
172
+ log('OHLCV-WS: Cancelled unsubscribe retry — reusing WS subscription', {
173
+ channel,
174
+ });
175
+ return;
176
+ }
177
+ // WS subscription was lost — fall through to recreate it.
178
+ }
179
+ else if (entry?.gracePeriodTimer) {
158
180
  clearTimeout(entry.gracePeriodTimer);
159
181
  entry.gracePeriodTimer = undefined;
160
182
  log('OHLCV-WS: Cancelled grace-period unsubscribe', {
161
183
  channel,
162
184
  });
163
185
  if (__classPrivateFieldGet(this, _OHLCVService_messenger, "f").call('BackendWebSocketService:channelHasSubscription', channel)) {
186
+ await __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_flushOtherChannels).call(this, channel);
164
187
  entry.refCount += 1;
165
188
  log('OHLCV-WS: WS subscription still alive, bumped refCount', {
166
189
  channel,
@@ -175,6 +198,7 @@ _OHLCVService_messenger = new WeakMap(), _OHLCVService_trace = new WeakMap(), _O
175
198
  entry.refCount += 1;
176
199
  return;
177
200
  }
201
+ await __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_flushOtherChannels).call(this, channel);
178
202
  try {
179
203
  await __classPrivateFieldGet(this, _OHLCVService_messenger, "f").call('BackendWebSocketService:connect');
180
204
  if (__classPrivateFieldGet(this, _OHLCVService_messenger, "f").call('BackendWebSocketService:channelHasSubscription', channel)) {
@@ -217,14 +241,114 @@ _OHLCVService_messenger = new WeakMap(), _OHLCVService_trace = new WeakMap(), _O
217
241
  entry.gracePeriodTimer = setTimeout(() => {
218
242
  entry.gracePeriodTimer = undefined;
219
243
  __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_performUnsubscribe).call(this, channel).catch(() => {
220
- // no-op
244
+ // no-op: retry scheduling and force-reconnection are handled internally
221
245
  });
222
246
  }, GRACE_PERIOD_MS);
223
- }, _OHLCVService_performUnsubscribe =
247
+ }, _OHLCVService_flushOtherChannels =
224
248
  // =============================================================================
225
249
  // Private — WebSocket Subscription Helpers
226
250
  // =============================================================================
227
- async function _OHLCVService_performUnsubscribe(channel) {
251
+ /**
252
+ * Immediately unsubscribe other channels in grace or failed-cleanup state.
253
+ * Called while the subscribe mutex is held before opening a new channel.
254
+ *
255
+ * @param exceptChannel - Channel being subscribed; excluded from flush.
256
+ */
257
+ async function _OHLCVService_flushOtherChannels(exceptChannel) {
258
+ for (const [channel, channelEntry] of __classPrivateFieldGet(this, _OHLCVService_channels, "f").entries()) {
259
+ if (channel === exceptChannel || channelEntry.refCount > 0) {
260
+ continue;
261
+ }
262
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_clearChannelTimers).call(this, channelEntry);
263
+ log('OHLCV-WS: Flushing grace-period channel before new subscribe', {
264
+ flushedChannel: channel,
265
+ newChannel: exceptChannel,
266
+ });
267
+ const success = await __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_unsubscribeChannelOnServer).call(this, channel);
268
+ if (success) {
269
+ __classPrivateFieldGet(this, _OHLCVService_channels, "f").delete(channel);
270
+ }
271
+ else {
272
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_scheduleUnsubscribeRetry).call(this, channel);
273
+ }
274
+ }
275
+ }, _OHLCVService_clearChannelTimers = function _OHLCVService_clearChannelTimers(entry) {
276
+ if (entry.gracePeriodTimer) {
277
+ clearTimeout(entry.gracePeriodTimer);
278
+ entry.gracePeriodTimer = undefined;
279
+ }
280
+ entry.retryAbort?.abort();
281
+ entry.retryAbort = undefined;
282
+ }, _OHLCVService_unsubscribeChannelOnServer = async function _OHLCVService_unsubscribeChannelOnServer(channel) {
283
+ try {
284
+ const subscriptions = __classPrivateFieldGet(this, _OHLCVService_messenger, "f").call('BackendWebSocketService:getSubscriptionsByChannel', channel);
285
+ for (const sub of subscriptions) {
286
+ await sub.unsubscribe();
287
+ }
288
+ return true;
289
+ }
290
+ catch (error) {
291
+ log('OHLCV-WS: Unsubscription failed', { channel, error });
292
+ __classPrivateFieldGet(this, _OHLCVService_messenger, "f").publish('OHLCVService:subscriptionError', {
293
+ channel,
294
+ error: String(error),
295
+ operation: 'unsubscribe',
296
+ });
297
+ return false;
298
+ }
299
+ }, _OHLCVService_scheduleUnsubscribeRetry = function _OHLCVService_scheduleUnsubscribeRetry(channel) {
300
+ const entry = __classPrivateFieldGet(this, _OHLCVService_channels, "f").get(channel);
301
+ if (!entry) {
302
+ // The channel was removed (e.g. destroy() or reconnect cleanup) while the
303
+ // unsubscribe was in flight. Do not resurrect it or start a new retry loop
304
+ // with an AbortController that teardown can no longer cancel — that could
305
+ // force a reconnection on the shared WebSocket after teardown.
306
+ return;
307
+ }
308
+ entry.retryAbort?.abort();
309
+ entry.retryAbort = new AbortController();
310
+ const { signal } = entry.retryAbort;
311
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
312
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_runUnsubRetryLoop).call(this, channel, signal);
313
+ }, _OHLCVService_runUnsubRetryLoop = async function _OHLCVService_runUnsubRetryLoop(channel, signal) {
314
+ try {
315
+ await unsubRetryPolicy.execute(async () => {
316
+ const releaseLock = await __classPrivateFieldGet(this, _OHLCVService_mutex, "f").acquire();
317
+ try {
318
+ const current = __classPrivateFieldGet(this, _OHLCVService_channels, "f").get(channel);
319
+ if (current && current.refCount > 0) {
320
+ return;
321
+ }
322
+ const success = await __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_unsubscribeChannelOnServer).call(this, channel);
323
+ if (!success) {
324
+ throw new Error('unsubscribe failed');
325
+ }
326
+ __classPrivateFieldGet(this, _OHLCVService_channels, "f").delete(channel);
327
+ log('OHLCV-WS: WS unsubscribe completed', { channel });
328
+ }
329
+ finally {
330
+ releaseLock();
331
+ }
332
+ }, signal);
333
+ }
334
+ catch {
335
+ if (signal.aborted) {
336
+ return;
337
+ }
338
+ log('OHLCV-WS: Unsubscribe retries exhausted — forcing reconnection', {
339
+ channel,
340
+ });
341
+ __classPrivateFieldGet(this, _OHLCVService_channels, "f").delete(channel);
342
+ // Last resort: reconnects the shared BackendWebSocketService instance
343
+ // (AccountActivityService and other consumers share this connection).
344
+ // They resubscribe on CONNECTED; OHLCV only resubscribes refCount > 0.
345
+ await __classPrivateFieldGet(this, _OHLCVService_messenger, "f")
346
+ .call('BackendWebSocketService:forceReconnection')
347
+ .catch(() => {
348
+ // no-op
349
+ });
350
+ }
351
+ }, _OHLCVService_performUnsubscribe = async function _OHLCVService_performUnsubscribe(channel) {
228
352
  const releaseLock = await __classPrivateFieldGet(this, _OHLCVService_mutex, "f").acquire();
229
353
  try {
230
354
  const entry = __classPrivateFieldGet(this, _OHLCVService_channels, "f").get(channel);
@@ -235,26 +359,12 @@ async function _OHLCVService_performUnsubscribe(channel) {
235
359
  log('OHLCV-WS: Grace period expired — performing actual WS unsubscribe', {
236
360
  channel,
237
361
  });
238
- __classPrivateFieldGet(this, _OHLCVService_channels, "f").delete(channel);
239
- try {
240
- const subscriptions = __classPrivateFieldGet(this, _OHLCVService_messenger, "f").call('BackendWebSocketService:getSubscriptionsByChannel', channel);
241
- for (const sub of subscriptions) {
242
- await sub.unsubscribe();
243
- }
244
- log('OHLCV-WS: WS unsubscribe completed', { channel });
245
- }
246
- catch (error) {
247
- log('OHLCV-WS: Unsubscription failed', { channel, error });
248
- __classPrivateFieldGet(this, _OHLCVService_messenger, "f").publish('OHLCVService:subscriptionError', {
249
- channel,
250
- error: String(error),
251
- operation: 'unsubscribe',
252
- });
253
- }
362
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_clearChannelTimers).call(this, __classPrivateFieldGet(this, _OHLCVService_channels, "f").get(channel) ?? { refCount: 0 });
254
363
  }
255
364
  finally {
256
365
  releaseLock();
257
366
  }
367
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_scheduleUnsubscribeRetry).call(this, channel);
258
368
  }, _OHLCVService_resubscribeActiveChannels =
259
369
  /**
260
370
  * Resubscribe all channels that were active before a disconnect.
@@ -267,8 +377,10 @@ async function _OHLCVService_resubscribeActiveChannels() {
267
377
  log('OHLCV-WS: Resubscribing active channels after reconnect', {
268
378
  count: channelCount,
269
379
  });
270
- for (const [channel, entry] of __classPrivateFieldGet(this, _OHLCVService_channels, "f").entries()) {
380
+ for (const [channel, entry] of [...__classPrivateFieldGet(this, _OHLCVService_channels, "f").entries()]) {
271
381
  if (entry.refCount === 0) {
382
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_clearChannelTimers).call(this, entry);
383
+ __classPrivateFieldGet(this, _OHLCVService_channels, "f").delete(channel);
272
384
  continue;
273
385
  }
274
386
  try {
@@ -1 +1 @@
1
- {"version":3,"file":"OHLCVService.cjs","sourceRoot":"","sources":["../../../src/ws/ohlcv/OHLCVService.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;;;;;;;;;;;AAQH,6CAAoC;AAEpC,gDAAoE;AAOpE,+EAA+D;AAI/D,gFAAgF;AAChF,YAAY;AACZ,gFAAgF;AAEhF,MAAM,YAAY,GAAG,cAAc,CAAC;AAEpC,MAAM,GAAG,GAAG,IAAA,8BAAkB,EAAC,yBAAa,EAAE,YAAY,CAAC,CAAC;AAE5D,MAAM,yBAAyB,GAAG,CAAC,WAAW,EAAE,aAAa,CAAU,CAAC;AAExE,MAAM,sBAAsB,GAAG,gBAAgB,CAAC;AAEhD,MAAM,4BAA4B,GAAG,2BAA2B,sBAAsB,EAAE,CAAC;AAEzF,mFAAmF;AACnF,MAAM,eAAe,GAAG,IAAK,CAAC;AA0CjB,QAAA,6BAA6B,GAAG;IAC3C,iCAAiC;IACjC,2CAA2C;IAC3C,mCAAmC;IACnC,2CAA2C;IAC3C,gDAAgD;IAChD,mDAAmD;IACnD,0DAA0D;IAC1D,4CAA4C;IAC5C,+CAA+C;CACvC,CAAC;AAEE,QAAA,4BAA4B,GAAG;IAC1C,gDAAgD;CACxC,CAAC;AAkCX,gFAAgF;AAChF,qBAAqB;AACrB,gFAAgF;AAEhF;;;;;;;;;;;;GAYG;AACH,MAAa,YAAY;IAavB,gFAAgF;IAChF,cAAc;IACd,gFAAgF;IAEhF,YACE,OAAmE;;QAjB5D,SAAI,GAAG,YAAY,CAAC;QAEpB,0CAAkC;QAElC,sCAAsB;QAEtB,iCAAY,IAAI,GAAG,EAAwB,EAAC;QAE5C,8BAAS,IAAI,mBAAK,EAAE,EAAC;QAErB,iCAAY,IAAI,GAAG,EAAU,EAAC;Q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AAI,CAAC,uBAAA,IAAI,8BAAU,CAAC,CAAC;QAEpD,IAAI,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAChC,uBAAA,IAAI,+BAAW,CAAC,OAAO,CAAC,iCAAiC,EAAE;gBACzD,QAAQ,EAAE,gBAAgB;gBAC1B,MAAM,EAAE,MAAM;gBACd,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;aACtB,CAAC,CAAC;YAEH,GAAG,CACD,mEAAmE,EACnE;gBACE,KAAK,EAAE,gBAAgB,CAAC,MAAM;gBAC9B,MAAM,EAAE,gBAAgB;aACzB,CACF,CAAC;YAEF,uBAAA,IAAI,8BAAU,CAAC,KAAK,EAAE,CAAC;QACzB,CAAC;IACH,CAAC;AACH,CAAC,mEAMa,OAAiC;IAC7C,OAAO,GAAG,sBAAsB,IAAI,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;AAChG,CAAC","sourcesContent":["/**\n * OHLCV Service for real-time candlestick data streaming via WebSocket.\n *\n * Wraps {@link BackendWebSocketService} through the messenger pattern to\n * provide subscribe/unsubscribe semantics for OHLCV market-data channels.\n * Includes reference counting, grace-period unsubscribe, idempotency checks,\n * chain-status forwarding, and automatic resubscription on reconnect.\n */\n\nimport type {\n TraceCallback,\n TraceContext,\n TraceRequest,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport { Mutex } from 'async-mutex';\n\nimport { projectLogger, createModuleLogger } from '../../logger.js';\nimport type { BackendWebSocketServiceMethodActions } from '../BackendWebSocketService-method-action-types.js';\nimport type {\n WebSocketConnectionInfo,\n BackendWebSocketServiceConnectionStateChangedEvent,\n ServerNotificationMessage,\n} from '../BackendWebSocketService.js';\nimport { WebSocketState } from '../BackendWebSocketService.js';\nimport type { OHLCVServiceMethodActions } from './OHLCVService-method-action-types.js';\nimport type { OHLCVBar, OHLCVSubscriptionOptions } from './types.js';\n\n// =============================================================================\n// Constants\n// =============================================================================\n\nconst SERVICE_NAME = 'OHLCVService';\n\nconst log = createModuleLogger(projectLogger, SERVICE_NAME);\n\nconst MESSENGER_EXPOSED_METHODS = ['subscribe', 'unsubscribe'] as const;\n\nconst SUBSCRIPTION_NAMESPACE = 'market-data.v1';\n\nconst SYSTEM_NOTIFICATIONS_CHANNEL = `system-notifications.v1.${SUBSCRIPTION_NAMESPACE}`;\n\n/** Delay before actually unsubscribing from a channel after refCount reaches 0. */\nconst GRACE_PERIOD_MS = 3_000;\n\n// =============================================================================\n// Types — Channel Tracking\n// =============================================================================\n\ntype ChannelEntry = {\n refCount: number;\n gracePeriodTimer?: ReturnType<typeof setTimeout>;\n};\n\n// =============================================================================\n// Types — System Notifications\n// =============================================================================\n\n/**\n * System notification data for chain status updates on market-data channels.\n */\nexport type OHLCVSystemNotificationData = {\n chainIds: string[];\n status: 'down' | 'up';\n timestamp?: number;\n};\n\n// =============================================================================\n// Types — Service Options\n// =============================================================================\n\n/**\n * Configuration options for the OHLCV service.\n */\nexport type OHLCVServiceOptions = {\n /** Optional callback to trace performance of OHLCV operations (default: no-op) */\n traceFn?: TraceCallback;\n};\n\n// =============================================================================\n// Action and Event Types\n// =============================================================================\n\nexport type OHLCVServiceActions = OHLCVServiceMethodActions;\n\nexport const OHLCV_SERVICE_ALLOWED_ACTIONS = [\n 'BackendWebSocketService:connect',\n 'BackendWebSocketService:forceReconnection',\n 'BackendWebSocketService:subscribe',\n 'BackendWebSocketService:getConnectionInfo',\n 'BackendWebSocketService:channelHasSubscription',\n 'BackendWebSocketService:getSubscriptionsByChannel',\n 'BackendWebSocketService:findSubscriptionsByChannelPrefix',\n 'BackendWebSocketService:addChannelCallback',\n 'BackendWebSocketService:removeChannelCallback',\n] as const;\n\nexport const OHLCV_SERVICE_ALLOWED_EVENTS = [\n 'BackendWebSocketService:connectionStateChanged',\n] as const;\n\nexport type AllowedActions = BackendWebSocketServiceMethodActions;\n\n// Events published by OHLCVService\n\nexport type OHLCVServiceBarUpdatedEvent = {\n type: `OHLCVService:barUpdated`;\n payload: [{ channel: string; bar: OHLCVBar }];\n};\n\nexport type OHLCVServiceChainStatusChangedEvent = {\n type: `OHLCVService:chainStatusChanged`;\n payload: [{ chainIds: string[]; status: 'up' | 'down'; timestamp?: number }];\n};\n\nexport type OHLCVServiceSubscriptionErrorEvent = {\n type: `OHLCVService:subscriptionError`;\n payload: [{ channel: string; error: string; operation: string }];\n};\n\nexport type OHLCVServiceEvents =\n | OHLCVServiceBarUpdatedEvent\n | OHLCVServiceChainStatusChangedEvent\n | OHLCVServiceSubscriptionErrorEvent;\n\nexport type AllowedEvents = BackendWebSocketServiceConnectionStateChangedEvent;\n\nexport type OHLCVServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n OHLCVServiceActions | AllowedActions,\n OHLCVServiceEvents | AllowedEvents\n>;\n\n// =============================================================================\n// Main Service Class\n// =============================================================================\n\n/**\n * Service for real-time OHLCV candlestick streaming via the backend WebSocket\n * gateway. Communicates with {@link BackendWebSocketService} exclusively\n * through the messenger — no direct import of the class.\n *\n * Features:\n * - Reference counting: multiple UI consumers share one WebSocket subscription\n * - Grace-period unsubscribe: avoids rapid unsub/resub during navigation\n * - Idempotency: duplicate subscribe calls for the same channel are no-ops\n * - Reconnect resilience: resubscribes all active channels on reconnect\n * - Chain-status forwarding: listens to system-notifications for chain up/down\n *\n */\nexport class OHLCVService {\n readonly name = SERVICE_NAME;\n\n readonly #messenger: OHLCVServiceMessenger;\n\n readonly #trace: TraceCallback;\n\n readonly #channels = new Map<string, ChannelEntry>();\n\n readonly #mutex = new Mutex();\n\n readonly #chainsUp = new Set<string>();\n\n // =============================================================================\n // Constructor\n // =============================================================================\n\n constructor(\n options: OHLCVServiceOptions & { messenger: OHLCVServiceMessenger },\n ) {\n this.#messenger = options.messenger;\n\n this.#trace =\n options.traceFn ??\n ((<Result>(\n _request: TraceRequest,\n fn?: (context?: TraceContext) => Result,\n ) => fn?.()) as TraceCallback);\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.#messenger.subscribe(\n 'BackendWebSocketService:connectionStateChanged',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n (connectionInfo: WebSocketConnectionInfo) =>\n this.#handleWebSocketStateChange(connectionInfo),\n );\n }\n\n /**\n * Register the system-notifications channel callback.\n */\n init(): void {\n log('OHLCV-WS: Initializing — registering system-notifications callback');\n this.#messenger.call('BackendWebSocketService:addChannelCallback', {\n channelName: SYSTEM_NOTIFICATIONS_CHANNEL,\n callback: (notification: ServerNotificationMessage) =>\n this.#handleSystemNotification(notification),\n });\n }\n\n // =============================================================================\n // Public — Subscribe / Unsubscribe\n // =============================================================================\n\n /**\n * Subscribe to an OHLCV channel. If this is the first subscriber for the\n * given asset/interval/currency combination a WebSocket subscription is\n * created. Additional calls for the same combination only bump the reference\n * count.\n *\n * @param options - The subscription parameters.\n * @returns A promise that resolves once the subscription is established.\n */\n async subscribe(options: OHLCVSubscriptionOptions): Promise<void> {\n const channel = this.#buildChannel(options);\n const releaseLock = await this.#mutex.acquire();\n try {\n await this.#subscribeInner(channel);\n } finally {\n releaseLock();\n }\n }\n\n async #subscribeInner(channel: string): Promise<void> {\n const entry = this.#channels.get(channel);\n\n if (entry?.gracePeriodTimer) {\n clearTimeout(entry.gracePeriodTimer);\n entry.gracePeriodTimer = undefined;\n log('OHLCV-WS: Cancelled grace-period unsubscribe', {\n channel,\n });\n\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n entry.refCount += 1;\n log('OHLCV-WS: WS subscription still alive, bumped refCount', {\n channel,\n refCount: entry.refCount,\n });\n return;\n }\n // WS subscription was lost (e.g. after disconnect/reconnect) — fall\n // through to recreate it. refCount is bumped only after success below.\n } else if (entry && entry.refCount > 0) {\n entry.refCount += 1;\n return;\n }\n try {\n await this.#messenger.call('BackendWebSocketService:connect');\n\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n log(\n 'OHLCV-WS: Channel already has WS subscription (idempotency), skipping',\n {\n channel,\n },\n );\n this.#channels.set(channel, { refCount: 1 });\n return;\n }\n\n await this.#messenger.call('BackendWebSocketService:subscribe', {\n channels: [channel],\n channelType: SUBSCRIPTION_NAMESPACE,\n callback: (notification: ServerNotificationMessage) => {\n this.#handleBarUpdate(channel, notification);\n },\n });\n\n this.#channels.set(channel, { refCount: 1 });\n log('OHLCV-WS: Subscribe succeeded — new WS subscription created', {\n channel,\n });\n } catch (error) {\n log('OHLCV-WS: Subscription failed', { channel, error });\n this.#channels.delete(channel);\n this.#messenger.publish('OHLCVService:subscriptionError', {\n channel,\n error: String(error),\n operation: 'subscribe',\n });\n }\n }\n\n /**\n * Unsubscribe from an OHLCV channel. Decrements the reference count and,\n * when it reaches zero, starts a grace-period timer before actually\n * unsubscribing from the WebSocket to absorb rapid navigation patterns.\n *\n * @param options - The subscription parameters to unsubscribe from.\n * @returns A promise that resolves once the unsubscription is processed.\n */\n async unsubscribe(options: OHLCVSubscriptionOptions): Promise<void> {\n const channel = this.#buildChannel(options);\n const releaseLock = await this.#mutex.acquire();\n try {\n await this.#unsubscribeInner(channel);\n } finally {\n releaseLock();\n }\n }\n\n async #unsubscribeInner(channel: string): Promise<void> {\n const entry = this.#channels.get(channel);\n\n if (!entry || entry.refCount <= 0) {\n return;\n }\n\n entry.refCount -= 1;\n\n if (entry.refCount > 0) {\n return;\n }\n\n entry.gracePeriodTimer = setTimeout(() => {\n entry.gracePeriodTimer = undefined;\n this.#performUnsubscribe(channel).catch(() => {\n // no-op\n });\n }, GRACE_PERIOD_MS);\n }\n\n // =============================================================================\n // Private — WebSocket Subscription Helpers\n // =============================================================================\n\n async #performUnsubscribe(channel: string): Promise<void> {\n const releaseLock = await this.#mutex.acquire();\n try {\n const entry = this.#channels.get(channel);\n if (entry && entry.refCount > 0) {\n log(\n 'OHLCV-WS: Skipping unsubscribe — new subscriber arrived while queued',\n { channel, refCount: entry.refCount },\n );\n return;\n }\n\n log('OHLCV-WS: Grace period expired — performing actual WS unsubscribe', {\n channel,\n });\n this.#channels.delete(channel);\n\n try {\n const subscriptions = this.#messenger.call(\n 'BackendWebSocketService:getSubscriptionsByChannel',\n channel,\n );\n\n for (const sub of subscriptions) {\n await sub.unsubscribe();\n }\n log('OHLCV-WS: WS unsubscribe completed', { channel });\n } catch (error) {\n log('OHLCV-WS: Unsubscription failed', { channel, error });\n this.#messenger.publish('OHLCVService:subscriptionError', {\n channel,\n error: String(error),\n operation: 'unsubscribe',\n });\n }\n } finally {\n releaseLock();\n }\n }\n\n /**\n * Resubscribe all channels that were active before a disconnect.\n * Called when WebSocket transitions to CONNECTED.\n */\n async #resubscribeActiveChannels(): Promise<void> {\n const releaseLock = await this.#mutex.acquire();\n try {\n const channelCount = this.#channels.size;\n log('OHLCV-WS: Resubscribing active channels after reconnect', {\n count: channelCount,\n });\n\n for (const [channel, entry] of this.#channels.entries()) {\n if (entry.refCount === 0) {\n continue;\n }\n\n try {\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n log(\n 'OHLCV-WS: Channel already subscribed on server, skipping resubscribe',\n {\n channel,\n },\n );\n continue;\n }\n\n await this.#messenger.call('BackendWebSocketService:subscribe', {\n channels: [channel],\n channelType: SUBSCRIPTION_NAMESPACE,\n callback: (notification: ServerNotificationMessage) => {\n this.#handleBarUpdate(channel, notification);\n },\n });\n log('OHLCV-WS: Resubscription succeeded', { channel });\n } catch (error) {\n log('OHLCV-WS: Resubscription failed for channel', {\n channel,\n error,\n });\n }\n }\n } finally {\n releaseLock();\n }\n }\n\n // =============================================================================\n // Private — Message Handlers\n // =============================================================================\n\n #handleBarUpdate(\n channel: string,\n notification: ServerNotificationMessage,\n ): void {\n const bar = notification.data as OHLCVBar;\n\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#trace(\n {\n name: `${SERVICE_NAME} Bar Update`,\n data: { channel, timestamp: bar.timestamp },\n tags: { service: SERVICE_NAME },\n },\n () => {\n this.#messenger.publish('OHLCVService:barUpdated', { channel, bar });\n },\n );\n }\n\n #handleSystemNotification(notification: ServerNotificationMessage): void {\n const data = notification.data as OHLCVSystemNotificationData;\n const { timestamp } = notification;\n\n if (!data.chainIds || !Array.isArray(data.chainIds) || !data.status) {\n throw new Error(\n 'Invalid system notification data: missing chainIds or status',\n );\n }\n\n if (data.status === 'up') {\n for (const chainId of data.chainIds) {\n this.#chainsUp.add(chainId);\n }\n } else {\n for (const chainId of data.chainIds) {\n this.#chainsUp.delete(chainId);\n }\n }\n\n this.#messenger.publish('OHLCVService:chainStatusChanged', {\n chainIds: data.chainIds,\n status: data.status,\n timestamp,\n });\n\n log(`OHLCV-WS: Chain status change: ${data.status}`, {\n chains: data.chainIds,\n status: data.status,\n });\n }\n\n async #handleWebSocketStateChange(\n connectionInfo: WebSocketConnectionInfo,\n ): Promise<void> {\n const { state } = connectionInfo;\n\n if (state === WebSocketState.CONNECTED) {\n await this.#resubscribeActiveChannels();\n } else if (state === WebSocketState.DISCONNECTED) {\n const chainsToMarkDown = Array.from(this.#chainsUp);\n\n if (chainsToMarkDown.length > 0) {\n this.#messenger.publish('OHLCVService:chainStatusChanged', {\n chainIds: chainsToMarkDown,\n status: 'down',\n timestamp: Date.now(),\n });\n\n log(\n 'OHLCV-WS: WebSocket disconnection — marked tracked chains as down',\n {\n count: chainsToMarkDown.length,\n chains: chainsToMarkDown,\n },\n );\n\n this.#chainsUp.clear();\n }\n }\n }\n\n // =============================================================================\n // Private — Utility\n // =============================================================================\n\n #buildChannel(options: OHLCVSubscriptionOptions): string {\n return `${SUBSCRIPTION_NAMESPACE}.${options.assetId}.${options.interval}.${options.currency}`;\n }\n\n // =============================================================================\n // Public — Cleanup\n // =============================================================================\n\n /**\n * Destroy the service and clean up all resources.\n */\n destroy(): void {\n for (const entry of this.#channels.values()) {\n if (entry.gracePeriodTimer) {\n clearTimeout(entry.gracePeriodTimer);\n }\n }\n this.#channels.clear();\n this.#chainsUp.clear();\n\n this.#messenger.call(\n 'BackendWebSocketService:removeChannelCallback',\n SYSTEM_NOTIFICATIONS_CHANNEL,\n );\n }\n}\n"]}
1
+ {"version":3,"file":"OHLCVService.cjs","sourceRoot":"","sources":["../../../src/ws/ohlcv/OHLCVService.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;;;;;;;;;;;AAQH,6CAAoC;AACpC,yCAA8D;AAE9D,gDAAoE;AAOpE,+EAA+D;AAI/D,gFAAgF;AAChF,YAAY;AACZ,gFAAgF;AAEhF,MAAM,YAAY,GAAG,cAAc,CAAC;AAEpC,MAAM,GAAG,GAAG,IAAA,8BAAkB,EAAC,yBAAa,EAAE,YAAY,CAAC,CAAC;AAE5D,MAAM,yBAAyB,GAAG,CAAC,WAAW,EAAE,aAAa,CAAU,CAAC;AAExE,MAAM,sBAAsB,GAAG,gBAAgB,CAAC;AAEhD,MAAM,4BAA4B,GAAG,2BAA2B,sBAAsB,EAAE,CAAC;AAEzF,mFAAmF;AACnF,MAAM,eAAe,GAAG,IAAK,CAAC;AAE9B,oEAAoE;AACpE,MAAM,qBAAqB,GAAG,CAAC,IAAK,EAAE,IAAK,EAAE,IAAK,CAAU,CAAC;AAE7D,MAAM,gBAAgB,GAAG,IAAA,iBAAK,EAAC,qBAAS,EAAE;IACxC,4EAA4E;IAC5E,iFAAiF;IACjF,WAAW,EAAE,qBAAqB,CAAC,MAAM;IACzC,OAAO,EAAE,IAAI,2BAAe,CAAC,CAAC,GAAG,qBAAqB,CAAC,CAAC;CACzD,CAAC,CAAC;AA2CU,QAAA,6BAA6B,GAAG;IAC3C,iCAAiC;IACjC,2CAA2C;IAC3C,mCAAmC;IACnC,2CAA2C;IAC3C,gDAAgD;IAChD,mDAAmD;IACnD,0DAA0D;IAC1D,4CAA4C;IAC5C,+CAA+C;CACvC,CAAC;AAEE,QAAA,4BAA4B,GAAG;IAC1C,gDAAgD;CACxC,CAAC;AAkCX,gFAAg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C,SAAS,EAAE,CAAC;QACvC,MAAM,uBAAA,IAAI,wEAA2B,MAA/B,IAAI,CAA6B,CAAC;IAC1C,CAAC;SAAM,IAAI,KAAK,KAAK,2CAAc,CAAC,YAAY,EAAE,CAAC;QACjD,MAAM,gBAAgB,GAAG,KAAK,CAAC,IAAI,CAAC,uBAAA,IAAI,8BAAU,CAAC,CAAC;QAEpD,IAAI,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAChC,uBAAA,IAAI,+BAAW,CAAC,OAAO,CAAC,iCAAiC,EAAE;gBACzD,QAAQ,EAAE,gBAAgB;gBAC1B,MAAM,EAAE,MAAM;gBACd,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;aACtB,CAAC,CAAC;YAEH,GAAG,CACD,mEAAmE,EACnE;gBACE,KAAK,EAAE,gBAAgB,CAAC,MAAM;gBAC9B,MAAM,EAAE,gBAAgB;aACzB,CACF,CAAC;YAEF,uBAAA,IAAI,8BAAU,CAAC,KAAK,EAAE,CAAC;QACzB,CAAC;IACH,CAAC;AACH,CAAC,mEAMa,OAAiC;IAC7C,OAAO,GAAG,sBAAsB,IAAI,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;AAChG,CAAC","sourcesContent":["/**\n * OHLCV Service for real-time candlestick data streaming via WebSocket.\n *\n * Wraps {@link BackendWebSocketService} through the messenger pattern to\n * provide subscribe/unsubscribe semantics for OHLCV market-data channels.\n * Includes reference counting, grace-period unsubscribe, idempotency checks,\n * chain-status forwarding, and automatic resubscription on reconnect.\n */\n\nimport type {\n TraceCallback,\n TraceContext,\n TraceRequest,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport { Mutex } from 'async-mutex';\nimport { handleAll, IterableBackoff, retry } from 'cockatiel';\n\nimport { projectLogger, createModuleLogger } from '../../logger.js';\nimport type { BackendWebSocketServiceMethodActions } from '../BackendWebSocketService-method-action-types.js';\nimport type {\n WebSocketConnectionInfo,\n BackendWebSocketServiceConnectionStateChangedEvent,\n ServerNotificationMessage,\n} from '../BackendWebSocketService.js';\nimport { WebSocketState } from '../BackendWebSocketService.js';\nimport type { OHLCVServiceMethodActions } from './OHLCVService-method-action-types.js';\nimport type { OHLCVBar, OHLCVSubscriptionOptions } from './types.js';\n\n// =============================================================================\n// Constants\n// =============================================================================\n\nconst SERVICE_NAME = 'OHLCVService';\n\nconst log = createModuleLogger(projectLogger, SERVICE_NAME);\n\nconst MESSENGER_EXPOSED_METHODS = ['subscribe', 'unsubscribe'] as const;\n\nconst SUBSCRIPTION_NAMESPACE = 'market-data.v1';\n\nconst SYSTEM_NOTIFICATIONS_CHANNEL = `system-notifications.v1.${SUBSCRIPTION_NAMESPACE}`;\n\n/** Delay before actually unsubscribing from a channel after refCount reaches 0. */\nconst GRACE_PERIOD_MS = 3_000;\n\n/** Backoff delays between failed WebSocket unsubscribe attempts. */\nconst UNSUB_RETRY_DELAYS_MS = [1_000, 2_000, 4_000] as const;\n\nconst unsubRetryPolicy = retry(handleAll, {\n // Cockatiel stops retrying once the failure index reaches `maxAttempts`, so\n // `length` here yields one initial attempt plus three delayed retries (4 total).\n maxAttempts: UNSUB_RETRY_DELAYS_MS.length,\n backoff: new IterableBackoff([...UNSUB_RETRY_DELAYS_MS]),\n});\n\n// =============================================================================\n// Types — Channel Tracking\n// =============================================================================\n\ntype ChannelEntry = {\n refCount: number;\n gracePeriodTimer?: ReturnType<typeof setTimeout>;\n retryAbort?: AbortController;\n};\n\n// =============================================================================\n// Types — System Notifications\n// =============================================================================\n\n/**\n * System notification data for chain status updates on market-data channels.\n */\nexport type OHLCVSystemNotificationData = {\n chainIds: string[];\n status: 'down' | 'up';\n timestamp?: number;\n};\n\n// =============================================================================\n// Types — Service Options\n// =============================================================================\n\n/**\n * Configuration options for the OHLCV service.\n */\nexport type OHLCVServiceOptions = {\n /** Optional callback to trace performance of OHLCV operations (default: no-op) */\n traceFn?: TraceCallback;\n};\n\n// =============================================================================\n// Action and Event Types\n// =============================================================================\n\nexport type OHLCVServiceActions = OHLCVServiceMethodActions;\n\nexport const OHLCV_SERVICE_ALLOWED_ACTIONS = [\n 'BackendWebSocketService:connect',\n 'BackendWebSocketService:forceReconnection',\n 'BackendWebSocketService:subscribe',\n 'BackendWebSocketService:getConnectionInfo',\n 'BackendWebSocketService:channelHasSubscription',\n 'BackendWebSocketService:getSubscriptionsByChannel',\n 'BackendWebSocketService:findSubscriptionsByChannelPrefix',\n 'BackendWebSocketService:addChannelCallback',\n 'BackendWebSocketService:removeChannelCallback',\n] as const;\n\nexport const OHLCV_SERVICE_ALLOWED_EVENTS = [\n 'BackendWebSocketService:connectionStateChanged',\n] as const;\n\nexport type AllowedActions = BackendWebSocketServiceMethodActions;\n\n// Events published by OHLCVService\n\nexport type OHLCVServiceBarUpdatedEvent = {\n type: `OHLCVService:barUpdated`;\n payload: [{ channel: string; bar: OHLCVBar }];\n};\n\nexport type OHLCVServiceChainStatusChangedEvent = {\n type: `OHLCVService:chainStatusChanged`;\n payload: [{ chainIds: string[]; status: 'up' | 'down'; timestamp?: number }];\n};\n\nexport type OHLCVServiceSubscriptionErrorEvent = {\n type: `OHLCVService:subscriptionError`;\n payload: [{ channel: string; error: string; operation: string }];\n};\n\nexport type OHLCVServiceEvents =\n | OHLCVServiceBarUpdatedEvent\n | OHLCVServiceChainStatusChangedEvent\n | OHLCVServiceSubscriptionErrorEvent;\n\nexport type AllowedEvents = BackendWebSocketServiceConnectionStateChangedEvent;\n\nexport type OHLCVServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n OHLCVServiceActions | AllowedActions,\n OHLCVServiceEvents | AllowedEvents\n>;\n\n// =============================================================================\n// Main Service Class\n// =============================================================================\n\n/**\n * Service for real-time OHLCV candlestick streaming via the backend WebSocket\n * gateway. Communicates with {@link BackendWebSocketService} exclusively\n * through the messenger — no direct import of the class.\n *\n * Features:\n * - Reference counting: multiple UI consumers share one WebSocket subscription\n * - Grace-period unsubscribe: reuses same-channel subs on rapid back navigation\n * - Grace-period flush: immediately unsubscribes other channels on navigation\n * - Unsubscribe retry: retries failed unsubs with backoff before force reconnect\n * - Idempotency: duplicate subscribe calls for the same channel are no-ops\n * - Reconnect resilience: resubscribes all active channels on reconnect\n * - Chain-status forwarding: listens to system-notifications for chain up/down\n *\n */\nexport class OHLCVService {\n readonly name = SERVICE_NAME;\n\n readonly #messenger: OHLCVServiceMessenger;\n\n readonly #trace: TraceCallback;\n\n readonly #channels = new Map<string, ChannelEntry>();\n\n readonly #mutex = new Mutex();\n\n readonly #chainsUp = new Set<string>();\n\n // =============================================================================\n // Constructor\n // =============================================================================\n\n constructor(\n options: OHLCVServiceOptions & { messenger: OHLCVServiceMessenger },\n ) {\n this.#messenger = options.messenger;\n\n this.#trace =\n options.traceFn ??\n ((<Result>(\n _request: TraceRequest,\n fn?: (context?: TraceContext) => Result,\n ) => fn?.()) as TraceCallback);\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.#messenger.subscribe(\n 'BackendWebSocketService:connectionStateChanged',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n (connectionInfo: WebSocketConnectionInfo) =>\n this.#handleWebSocketStateChange(connectionInfo),\n );\n }\n\n /**\n * Register the system-notifications channel callback.\n */\n init(): void {\n log('OHLCV-WS: Initializing — registering system-notifications callback');\n this.#messenger.call('BackendWebSocketService:addChannelCallback', {\n channelName: SYSTEM_NOTIFICATIONS_CHANNEL,\n callback: (notification: ServerNotificationMessage) =>\n this.#handleSystemNotification(notification),\n });\n }\n\n // =============================================================================\n // Public — Subscribe / Unsubscribe\n // =============================================================================\n\n /**\n * Subscribe to an OHLCV channel. If this is the first subscriber for the\n * given asset/interval/currency combination a WebSocket subscription is\n * created. Additional calls for the same combination only bump the reference\n * count.\n *\n * @param options - The subscription parameters.\n * @returns A promise that resolves once the subscription is established.\n */\n async subscribe(options: OHLCVSubscriptionOptions): Promise<void> {\n const channel = this.#buildChannel(options);\n const releaseLock = await this.#mutex.acquire();\n try {\n await this.#subscribeInner(channel);\n } finally {\n releaseLock();\n }\n }\n\n async #subscribeInner(channel: string): Promise<void> {\n const entry = this.#channels.get(channel);\n\n if (entry?.retryAbort) {\n entry.retryAbort.abort();\n entry.retryAbort = undefined;\n entry.refCount = 1;\n\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n await this.#flushOtherChannels(channel);\n log('OHLCV-WS: Cancelled unsubscribe retry — reusing WS subscription', {\n channel,\n });\n return;\n }\n // WS subscription was lost — fall through to recreate it.\n } else if (entry?.gracePeriodTimer) {\n clearTimeout(entry.gracePeriodTimer);\n entry.gracePeriodTimer = undefined;\n log('OHLCV-WS: Cancelled grace-period unsubscribe', {\n channel,\n });\n\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n await this.#flushOtherChannels(channel);\n entry.refCount += 1;\n log('OHLCV-WS: WS subscription still alive, bumped refCount', {\n channel,\n refCount: entry.refCount,\n });\n return;\n }\n // WS subscription was lost (e.g. after disconnect/reconnect) — fall\n // through to recreate it. refCount is bumped only after success below.\n } else if (entry && entry.refCount > 0) {\n entry.refCount += 1;\n return;\n }\n\n await this.#flushOtherChannels(channel);\n\n try {\n await this.#messenger.call('BackendWebSocketService:connect');\n\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n log(\n 'OHLCV-WS: Channel already has WS subscription (idempotency), skipping',\n {\n channel,\n },\n );\n this.#channels.set(channel, { refCount: 1 });\n return;\n }\n\n await this.#messenger.call('BackendWebSocketService:subscribe', {\n channels: [channel],\n channelType: SUBSCRIPTION_NAMESPACE,\n callback: (notification: ServerNotificationMessage) => {\n this.#handleBarUpdate(channel, notification);\n },\n });\n\n this.#channels.set(channel, { refCount: 1 });\n log('OHLCV-WS: Subscribe succeeded — new WS subscription created', {\n channel,\n });\n } catch (error) {\n log('OHLCV-WS: Subscription failed', { channel, error });\n this.#channels.delete(channel);\n this.#messenger.publish('OHLCVService:subscriptionError', {\n channel,\n error: String(error),\n operation: 'subscribe',\n });\n }\n }\n\n /**\n * Unsubscribe from an OHLCV channel. Decrements the reference count and,\n * when it reaches zero, starts a grace-period timer before actually\n * unsubscribing from the WebSocket to absorb rapid navigation patterns.\n *\n * @param options - The subscription parameters to unsubscribe from.\n * @returns A promise that resolves once the unsubscription is processed.\n */\n async unsubscribe(options: OHLCVSubscriptionOptions): Promise<void> {\n const channel = this.#buildChannel(options);\n const releaseLock = await this.#mutex.acquire();\n try {\n await this.#unsubscribeInner(channel);\n } finally {\n releaseLock();\n }\n }\n\n async #unsubscribeInner(channel: string): Promise<void> {\n const entry = this.#channels.get(channel);\n\n if (!entry || entry.refCount <= 0) {\n return;\n }\n\n entry.refCount -= 1;\n\n if (entry.refCount > 0) {\n return;\n }\n\n entry.gracePeriodTimer = setTimeout(() => {\n entry.gracePeriodTimer = undefined;\n this.#performUnsubscribe(channel).catch(() => {\n // no-op: retry scheduling and force-reconnection are handled internally\n });\n }, GRACE_PERIOD_MS);\n }\n\n // =============================================================================\n // Private — WebSocket Subscription Helpers\n // =============================================================================\n\n /**\n * Immediately unsubscribe other channels in grace or failed-cleanup state.\n * Called while the subscribe mutex is held before opening a new channel.\n *\n * @param exceptChannel - Channel being subscribed; excluded from flush.\n */\n async #flushOtherChannels(exceptChannel: string): Promise<void> {\n for (const [channel, channelEntry] of this.#channels.entries()) {\n if (channel === exceptChannel || channelEntry.refCount > 0) {\n continue;\n }\n\n this.#clearChannelTimers(channelEntry);\n log('OHLCV-WS: Flushing grace-period channel before new subscribe', {\n flushedChannel: channel,\n newChannel: exceptChannel,\n });\n\n const success = await this.#unsubscribeChannelOnServer(channel);\n if (success) {\n this.#channels.delete(channel);\n } else {\n this.#scheduleUnsubscribeRetry(channel);\n }\n }\n }\n\n #clearChannelTimers(entry: ChannelEntry): void {\n if (entry.gracePeriodTimer) {\n clearTimeout(entry.gracePeriodTimer);\n entry.gracePeriodTimer = undefined;\n }\n entry.retryAbort?.abort();\n entry.retryAbort = undefined;\n }\n\n async #unsubscribeChannelOnServer(channel: string): Promise<boolean> {\n try {\n const subscriptions = this.#messenger.call(\n 'BackendWebSocketService:getSubscriptionsByChannel',\n channel,\n );\n\n for (const sub of subscriptions) {\n await sub.unsubscribe();\n }\n return true;\n } catch (error) {\n log('OHLCV-WS: Unsubscription failed', { channel, error });\n this.#messenger.publish('OHLCVService:subscriptionError', {\n channel,\n error: String(error),\n operation: 'unsubscribe',\n });\n return false;\n }\n }\n\n #scheduleUnsubscribeRetry(channel: string): void {\n const entry = this.#channels.get(channel);\n if (!entry) {\n // The channel was removed (e.g. destroy() or reconnect cleanup) while the\n // unsubscribe was in flight. Do not resurrect it or start a new retry loop\n // with an AbortController that teardown can no longer cancel — that could\n // force a reconnection on the shared WebSocket after teardown.\n return;\n }\n\n entry.retryAbort?.abort();\n entry.retryAbort = new AbortController();\n\n const { signal } = entry.retryAbort;\n\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#runUnsubRetryLoop(channel, signal);\n }\n\n async #runUnsubRetryLoop(\n channel: string,\n signal: AbortSignal,\n ): Promise<void> {\n try {\n await unsubRetryPolicy.execute(async () => {\n const releaseLock = await this.#mutex.acquire();\n try {\n const current = this.#channels.get(channel);\n if (current && current.refCount > 0) {\n return;\n }\n\n const success = await this.#unsubscribeChannelOnServer(channel);\n if (!success) {\n throw new Error('unsubscribe failed');\n }\n\n this.#channels.delete(channel);\n log('OHLCV-WS: WS unsubscribe completed', { channel });\n } finally {\n releaseLock();\n }\n }, signal);\n } catch {\n if (signal.aborted) {\n return;\n }\n\n log('OHLCV-WS: Unsubscribe retries exhausted — forcing reconnection', {\n channel,\n });\n this.#channels.delete(channel);\n // Last resort: reconnects the shared BackendWebSocketService instance\n // (AccountActivityService and other consumers share this connection).\n // They resubscribe on CONNECTED; OHLCV only resubscribes refCount > 0.\n await this.#messenger\n .call('BackendWebSocketService:forceReconnection')\n .catch(() => {\n // no-op\n });\n }\n }\n\n async #performUnsubscribe(channel: string): Promise<void> {\n const releaseLock = await this.#mutex.acquire();\n try {\n const entry = this.#channels.get(channel);\n if (entry && entry.refCount > 0) {\n log(\n 'OHLCV-WS: Skipping unsubscribe — new subscriber arrived while queued',\n { channel, refCount: entry.refCount },\n );\n return;\n }\n\n log('OHLCV-WS: Grace period expired — performing actual WS unsubscribe', {\n channel,\n });\n\n this.#clearChannelTimers(this.#channels.get(channel) ?? { refCount: 0 });\n } finally {\n releaseLock();\n }\n\n this.#scheduleUnsubscribeRetry(channel);\n }\n\n /**\n * Resubscribe all channels that were active before a disconnect.\n * Called when WebSocket transitions to CONNECTED.\n */\n async #resubscribeActiveChannels(): Promise<void> {\n const releaseLock = await this.#mutex.acquire();\n try {\n const channelCount = this.#channels.size;\n log('OHLCV-WS: Resubscribing active channels after reconnect', {\n count: channelCount,\n });\n\n for (const [channel, entry] of [...this.#channels.entries()]) {\n if (entry.refCount === 0) {\n this.#clearChannelTimers(entry);\n this.#channels.delete(channel);\n continue;\n }\n\n try {\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n log(\n 'OHLCV-WS: Channel already subscribed on server, skipping resubscribe',\n {\n channel,\n },\n );\n continue;\n }\n\n await this.#messenger.call('BackendWebSocketService:subscribe', {\n channels: [channel],\n channelType: SUBSCRIPTION_NAMESPACE,\n callback: (notification: ServerNotificationMessage) => {\n this.#handleBarUpdate(channel, notification);\n },\n });\n log('OHLCV-WS: Resubscription succeeded', { channel });\n } catch (error) {\n log('OHLCV-WS: Resubscription failed for channel', {\n channel,\n error,\n });\n }\n }\n } finally {\n releaseLock();\n }\n }\n\n // =============================================================================\n // Private — Message Handlers\n // =============================================================================\n\n #handleBarUpdate(\n channel: string,\n notification: ServerNotificationMessage,\n ): void {\n const bar = notification.data as OHLCVBar;\n\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#trace(\n {\n name: `${SERVICE_NAME} Bar Update`,\n data: { channel, timestamp: bar.timestamp },\n tags: { service: SERVICE_NAME },\n },\n () => {\n this.#messenger.publish('OHLCVService:barUpdated', { channel, bar });\n },\n );\n }\n\n #handleSystemNotification(notification: ServerNotificationMessage): void {\n const data = notification.data as OHLCVSystemNotificationData;\n const { timestamp } = notification;\n\n if (!data.chainIds || !Array.isArray(data.chainIds) || !data.status) {\n throw new Error(\n 'Invalid system notification data: missing chainIds or status',\n );\n }\n\n if (data.status === 'up') {\n for (const chainId of data.chainIds) {\n this.#chainsUp.add(chainId);\n }\n } else {\n for (const chainId of data.chainIds) {\n this.#chainsUp.delete(chainId);\n }\n }\n\n this.#messenger.publish('OHLCVService:chainStatusChanged', {\n chainIds: data.chainIds,\n status: data.status,\n timestamp,\n });\n\n log(`OHLCV-WS: Chain status change: ${data.status}`, {\n chains: data.chainIds,\n status: data.status,\n });\n }\n\n async #handleWebSocketStateChange(\n connectionInfo: WebSocketConnectionInfo,\n ): Promise<void> {\n const { state } = connectionInfo;\n\n if (state === WebSocketState.CONNECTED) {\n await this.#resubscribeActiveChannels();\n } else if (state === WebSocketState.DISCONNECTED) {\n const chainsToMarkDown = Array.from(this.#chainsUp);\n\n if (chainsToMarkDown.length > 0) {\n this.#messenger.publish('OHLCVService:chainStatusChanged', {\n chainIds: chainsToMarkDown,\n status: 'down',\n timestamp: Date.now(),\n });\n\n log(\n 'OHLCV-WS: WebSocket disconnection — marked tracked chains as down',\n {\n count: chainsToMarkDown.length,\n chains: chainsToMarkDown,\n },\n );\n\n this.#chainsUp.clear();\n }\n }\n }\n\n // =============================================================================\n // Private — Utility\n // =============================================================================\n\n #buildChannel(options: OHLCVSubscriptionOptions): string {\n return `${SUBSCRIPTION_NAMESPACE}.${options.assetId}.${options.interval}.${options.currency}`;\n }\n\n // =============================================================================\n // Public — Cleanup\n // =============================================================================\n\n /**\n * Destroy the service and clean up all resources.\n */\n destroy(): void {\n for (const entry of this.#channels.values()) {\n this.#clearChannelTimers(entry);\n }\n this.#channels.clear();\n this.#chainsUp.clear();\n\n this.#messenger.call(\n 'BackendWebSocketService:removeChannelCallback',\n SYSTEM_NOTIFICATIONS_CHANNEL,\n );\n }\n}\n"]}
@@ -65,7 +65,9 @@ export type OHLCVServiceMessenger = Messenger<typeof SERVICE_NAME, OHLCVServiceA
65
65
  *
66
66
  * Features:
67
67
  * - Reference counting: multiple UI consumers share one WebSocket subscription
68
- * - Grace-period unsubscribe: avoids rapid unsub/resub during navigation
68
+ * - Grace-period unsubscribe: reuses same-channel subs on rapid back navigation
69
+ * - Grace-period flush: immediately unsubscribes other channels on navigation
70
+ * - Unsubscribe retry: retries failed unsubs with backoff before force reconnect
69
71
  * - Idempotency: duplicate subscribe calls for the same channel are no-ops
70
72
  * - Reconnect resilience: resubscribes all active channels on reconnect
71
73
  * - Chain-status forwarding: listens to system-notifications for chain up/down
@@ -1 +1 @@
1
- {"version":3,"file":"OHLCVService.d.cts","sourceRoot":"","sources":["../../../src/ws/ohlcv/OHLCVService.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,aAAa,EAGd,mCAAmC;AACpC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAIrD,OAAO,KAAK,EAAE,oCAAoC,EAAE,2DAA0D;AAC9G,OAAO,KAAK,EAEV,kDAAkD,EAEnD,uCAAsC;AAEvC,OAAO,KAAK,EAAE,yBAAyB,EAAE,+CAA8C;AACvF,OAAO,KAAK,EAAE,QAAQ,EAAE,wBAAwB,EAAE,oBAAmB;AAMrE,QAAA,MAAM,YAAY,iBAAiB,CAAC;AA0BpC;;GAEG;AACH,MAAM,MAAM,2BAA2B,GAAG;IACxC,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAMF;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG;IA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OAAO,IAAI,IAAI;CAYhB"}
@@ -65,7 +65,9 @@ export type OHLCVServiceMessenger = Messenger<typeof SERVICE_NAME, OHLCVServiceA
65
65
  *
66
66
  * Features:
67
67
  * - Reference counting: multiple UI consumers share one WebSocket subscription
68
- * - Grace-period unsubscribe: avoids rapid unsub/resub during navigation
68
+ * - Grace-period unsubscribe: reuses same-channel subs on rapid back navigation
69
+ * - Grace-period flush: immediately unsubscribes other channels on navigation
70
+ * - Unsubscribe retry: retries failed unsubs with backoff before force reconnect
69
71
  * - Idempotency: duplicate subscribe calls for the same channel are no-ops
70
72
  * - Reconnect resilience: resubscribes all active channels on reconnect
71
73
  * - Chain-status forwarding: listens to system-notifications for chain up/down
@@ -1 +1 @@
1
- {"version":3,"file":"OHLCVService.d.mts","sourceRoot":"","sources":["../../../src/ws/ohlcv/OHLCVService.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,aAAa,EAGd,mCAAmC;AACpC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAIrD,OAAO,KAAK,EAAE,oCAAoC,EAAE,2DAA0D;AAC9G,OAAO,KAAK,EAEV,kDAAkD,EAEnD,uCAAsC;AAEvC,OAAO,KAAK,EAAE,yBAAyB,EAAE,+CAA8C;AACvF,OAAO,KAAK,EAAE,QAAQ,EAAE,wBAAwB,EAAE,oBAAmB;AAMrE,QAAA,MAAM,YAAY,iBAAiB,CAAC;AA0BpC;;GAEG;AACH,MAAM,MAAM,2BAA2B,GAAG;IACxC,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAMF;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,kFAAkF;IAClF,OAAO,CAAC,EAAE,aAAa,CAAC;CACzB,CAAC;AAMF,MAAM,MAAM,mBAAmB,GAAG,yBAAyB,CAAC;AAE5D,eA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!f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
18
18
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
19
19
  };
20
- var _OHLCVService_instances, _OHLCVService_messenger, _OHLCVService_trace, _OHLCVService_channels, _OHLCVService_mutex, _OHLCVService_chainsUp, _OHLCVService_subscribeInner, _OHLCVService_unsubscribeInner, _OHLCVService_performUnsubscribe, _OHLCVService_resubscribeActiveChannels, _OHLCVService_handleBarUpdate, _OHLCVService_handleSystemNotification, _OHLCVService_handleWebSocketStateChange, _OHLCVService_buildChannel;
20
+ var _OHLCVService_instances, _OHLCVService_messenger, _OHLCVService_trace, _OHLCVService_channels, _OHLCVService_mutex, _OHLCVService_chainsUp, _OHLCVService_subscribeInner, _OHLCVService_unsubscribeInner, _OHLCVService_flushOtherChannels, _OHLCVService_clearChannelTimers, _OHLCVService_unsubscribeChannelOnServer, _OHLCVService_scheduleUnsubscribeRetry, _OHLCVService_runUnsubRetryLoop, _OHLCVService_performUnsubscribe, _OHLCVService_resubscribeActiveChannels, _OHLCVService_handleBarUpdate, _OHLCVService_handleSystemNotification, _OHLCVService_handleWebSocketStateChange, _OHLCVService_buildChannel;
21
21
  import { Mutex } from "async-mutex";
22
+ import { handleAll, IterableBackoff, retry } from "cockatiel";
22
23
  import { projectLogger, createModuleLogger } from "../../logger.mjs";
23
24
  import { WebSocketState } from "../BackendWebSocketService.mjs";
24
25
  // =============================================================================
@@ -31,6 +32,14 @@ const SUBSCRIPTION_NAMESPACE = 'market-data.v1';
31
32
  const SYSTEM_NOTIFICATIONS_CHANNEL = `system-notifications.v1.${SUBSCRIPTION_NAMESPACE}`;
32
33
  /** Delay before actually unsubscribing from a channel after refCount reaches 0. */
33
34
  const GRACE_PERIOD_MS = 3000;
35
+ /** Backoff delays between failed WebSocket unsubscribe attempts. */
36
+ const UNSUB_RETRY_DELAYS_MS = [1000, 2000, 4000];
37
+ const unsubRetryPolicy = retry(handleAll, {
38
+ // Cockatiel stops retrying once the failure index reaches `maxAttempts`, so
39
+ // `length` here yields one initial attempt plus three delayed retries (4 total).
40
+ maxAttempts: UNSUB_RETRY_DELAYS_MS.length,
41
+ backoff: new IterableBackoff([...UNSUB_RETRY_DELAYS_MS]),
42
+ });
34
43
  export const OHLCV_SERVICE_ALLOWED_ACTIONS = [
35
44
  'BackendWebSocketService:connect',
36
45
  'BackendWebSocketService:forceReconnection',
@@ -55,7 +64,9 @@ export const OHLCV_SERVICE_ALLOWED_EVENTS = [
55
64
  *
56
65
  * Features:
57
66
  * - Reference counting: multiple UI consumers share one WebSocket subscription
58
- * - Grace-period unsubscribe: avoids rapid unsub/resub during navigation
67
+ * - Grace-period unsubscribe: reuses same-channel subs on rapid back navigation
68
+ * - Grace-period flush: immediately unsubscribes other channels on navigation
69
+ * - Unsubscribe retry: retries failed unsubs with backoff before force reconnect
59
70
  * - Idempotency: duplicate subscribe calls for the same channel are no-ops
60
71
  * - Reconnect resilience: resubscribes all active channels on reconnect
61
72
  * - Chain-status forwarding: listens to system-notifications for chain up/down
@@ -139,9 +150,7 @@ export class OHLCVService {
139
150
  */
140
151
  destroy() {
141
152
  for (const entry of __classPrivateFieldGet(this, _OHLCVService_channels, "f").values()) {
142
- if (entry.gracePeriodTimer) {
143
- clearTimeout(entry.gracePeriodTimer);
144
- }
153
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_clearChannelTimers).call(this, entry);
145
154
  }
146
155
  __classPrivateFieldGet(this, _OHLCVService_channels, "f").clear();
147
156
  __classPrivateFieldGet(this, _OHLCVService_chainsUp, "f").clear();
@@ -150,13 +159,27 @@ export class OHLCVService {
150
159
  }
151
160
  _OHLCVService_messenger = new WeakMap(), _OHLCVService_trace = new WeakMap(), _OHLCVService_channels = new WeakMap(), _OHLCVService_mutex = new WeakMap(), _OHLCVService_chainsUp = new WeakMap(), _OHLCVService_instances = new WeakSet(), _OHLCVService_subscribeInner = async function _OHLCVService_subscribeInner(channel) {
152
161
  const entry = __classPrivateFieldGet(this, _OHLCVService_channels, "f").get(channel);
153
- if (entry?.gracePeriodTimer) {
162
+ if (entry?.retryAbort) {
163
+ entry.retryAbort.abort();
164
+ entry.retryAbort = undefined;
165
+ entry.refCount = 1;
166
+ if (__classPrivateFieldGet(this, _OHLCVService_messenger, "f").call('BackendWebSocketService:channelHasSubscription', channel)) {
167
+ await __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_flushOtherChannels).call(this, channel);
168
+ log('OHLCV-WS: Cancelled unsubscribe retry — reusing WS subscription', {
169
+ channel,
170
+ });
171
+ return;
172
+ }
173
+ // WS subscription was lost — fall through to recreate it.
174
+ }
175
+ else if (entry?.gracePeriodTimer) {
154
176
  clearTimeout(entry.gracePeriodTimer);
155
177
  entry.gracePeriodTimer = undefined;
156
178
  log('OHLCV-WS: Cancelled grace-period unsubscribe', {
157
179
  channel,
158
180
  });
159
181
  if (__classPrivateFieldGet(this, _OHLCVService_messenger, "f").call('BackendWebSocketService:channelHasSubscription', channel)) {
182
+ await __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_flushOtherChannels).call(this, channel);
160
183
  entry.refCount += 1;
161
184
  log('OHLCV-WS: WS subscription still alive, bumped refCount', {
162
185
  channel,
@@ -171,6 +194,7 @@ _OHLCVService_messenger = new WeakMap(), _OHLCVService_trace = new WeakMap(), _O
171
194
  entry.refCount += 1;
172
195
  return;
173
196
  }
197
+ await __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_flushOtherChannels).call(this, channel);
174
198
  try {
175
199
  await __classPrivateFieldGet(this, _OHLCVService_messenger, "f").call('BackendWebSocketService:connect');
176
200
  if (__classPrivateFieldGet(this, _OHLCVService_messenger, "f").call('BackendWebSocketService:channelHasSubscription', channel)) {
@@ -213,14 +237,114 @@ _OHLCVService_messenger = new WeakMap(), _OHLCVService_trace = new WeakMap(), _O
213
237
  entry.gracePeriodTimer = setTimeout(() => {
214
238
  entry.gracePeriodTimer = undefined;
215
239
  __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_performUnsubscribe).call(this, channel).catch(() => {
216
- // no-op
240
+ // no-op: retry scheduling and force-reconnection are handled internally
217
241
  });
218
242
  }, GRACE_PERIOD_MS);
219
- }, _OHLCVService_performUnsubscribe =
243
+ }, _OHLCVService_flushOtherChannels =
220
244
  // =============================================================================
221
245
  // Private — WebSocket Subscription Helpers
222
246
  // =============================================================================
223
- async function _OHLCVService_performUnsubscribe(channel) {
247
+ /**
248
+ * Immediately unsubscribe other channels in grace or failed-cleanup state.
249
+ * Called while the subscribe mutex is held before opening a new channel.
250
+ *
251
+ * @param exceptChannel - Channel being subscribed; excluded from flush.
252
+ */
253
+ async function _OHLCVService_flushOtherChannels(exceptChannel) {
254
+ for (const [channel, channelEntry] of __classPrivateFieldGet(this, _OHLCVService_channels, "f").entries()) {
255
+ if (channel === exceptChannel || channelEntry.refCount > 0) {
256
+ continue;
257
+ }
258
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_clearChannelTimers).call(this, channelEntry);
259
+ log('OHLCV-WS: Flushing grace-period channel before new subscribe', {
260
+ flushedChannel: channel,
261
+ newChannel: exceptChannel,
262
+ });
263
+ const success = await __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_unsubscribeChannelOnServer).call(this, channel);
264
+ if (success) {
265
+ __classPrivateFieldGet(this, _OHLCVService_channels, "f").delete(channel);
266
+ }
267
+ else {
268
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_scheduleUnsubscribeRetry).call(this, channel);
269
+ }
270
+ }
271
+ }, _OHLCVService_clearChannelTimers = function _OHLCVService_clearChannelTimers(entry) {
272
+ if (entry.gracePeriodTimer) {
273
+ clearTimeout(entry.gracePeriodTimer);
274
+ entry.gracePeriodTimer = undefined;
275
+ }
276
+ entry.retryAbort?.abort();
277
+ entry.retryAbort = undefined;
278
+ }, _OHLCVService_unsubscribeChannelOnServer = async function _OHLCVService_unsubscribeChannelOnServer(channel) {
279
+ try {
280
+ const subscriptions = __classPrivateFieldGet(this, _OHLCVService_messenger, "f").call('BackendWebSocketService:getSubscriptionsByChannel', channel);
281
+ for (const sub of subscriptions) {
282
+ await sub.unsubscribe();
283
+ }
284
+ return true;
285
+ }
286
+ catch (error) {
287
+ log('OHLCV-WS: Unsubscription failed', { channel, error });
288
+ __classPrivateFieldGet(this, _OHLCVService_messenger, "f").publish('OHLCVService:subscriptionError', {
289
+ channel,
290
+ error: String(error),
291
+ operation: 'unsubscribe',
292
+ });
293
+ return false;
294
+ }
295
+ }, _OHLCVService_scheduleUnsubscribeRetry = function _OHLCVService_scheduleUnsubscribeRetry(channel) {
296
+ const entry = __classPrivateFieldGet(this, _OHLCVService_channels, "f").get(channel);
297
+ if (!entry) {
298
+ // The channel was removed (e.g. destroy() or reconnect cleanup) while the
299
+ // unsubscribe was in flight. Do not resurrect it or start a new retry loop
300
+ // with an AbortController that teardown can no longer cancel — that could
301
+ // force a reconnection on the shared WebSocket after teardown.
302
+ return;
303
+ }
304
+ entry.retryAbort?.abort();
305
+ entry.retryAbort = new AbortController();
306
+ const { signal } = entry.retryAbort;
307
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
308
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_runUnsubRetryLoop).call(this, channel, signal);
309
+ }, _OHLCVService_runUnsubRetryLoop = async function _OHLCVService_runUnsubRetryLoop(channel, signal) {
310
+ try {
311
+ await unsubRetryPolicy.execute(async () => {
312
+ const releaseLock = await __classPrivateFieldGet(this, _OHLCVService_mutex, "f").acquire();
313
+ try {
314
+ const current = __classPrivateFieldGet(this, _OHLCVService_channels, "f").get(channel);
315
+ if (current && current.refCount > 0) {
316
+ return;
317
+ }
318
+ const success = await __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_unsubscribeChannelOnServer).call(this, channel);
319
+ if (!success) {
320
+ throw new Error('unsubscribe failed');
321
+ }
322
+ __classPrivateFieldGet(this, _OHLCVService_channels, "f").delete(channel);
323
+ log('OHLCV-WS: WS unsubscribe completed', { channel });
324
+ }
325
+ finally {
326
+ releaseLock();
327
+ }
328
+ }, signal);
329
+ }
330
+ catch {
331
+ if (signal.aborted) {
332
+ return;
333
+ }
334
+ log('OHLCV-WS: Unsubscribe retries exhausted — forcing reconnection', {
335
+ channel,
336
+ });
337
+ __classPrivateFieldGet(this, _OHLCVService_channels, "f").delete(channel);
338
+ // Last resort: reconnects the shared BackendWebSocketService instance
339
+ // (AccountActivityService and other consumers share this connection).
340
+ // They resubscribe on CONNECTED; OHLCV only resubscribes refCount > 0.
341
+ await __classPrivateFieldGet(this, _OHLCVService_messenger, "f")
342
+ .call('BackendWebSocketService:forceReconnection')
343
+ .catch(() => {
344
+ // no-op
345
+ });
346
+ }
347
+ }, _OHLCVService_performUnsubscribe = async function _OHLCVService_performUnsubscribe(channel) {
224
348
  const releaseLock = await __classPrivateFieldGet(this, _OHLCVService_mutex, "f").acquire();
225
349
  try {
226
350
  const entry = __classPrivateFieldGet(this, _OHLCVService_channels, "f").get(channel);
@@ -231,26 +355,12 @@ async function _OHLCVService_performUnsubscribe(channel) {
231
355
  log('OHLCV-WS: Grace period expired — performing actual WS unsubscribe', {
232
356
  channel,
233
357
  });
234
- __classPrivateFieldGet(this, _OHLCVService_channels, "f").delete(channel);
235
- try {
236
- const subscriptions = __classPrivateFieldGet(this, _OHLCVService_messenger, "f").call('BackendWebSocketService:getSubscriptionsByChannel', channel);
237
- for (const sub of subscriptions) {
238
- await sub.unsubscribe();
239
- }
240
- log('OHLCV-WS: WS unsubscribe completed', { channel });
241
- }
242
- catch (error) {
243
- log('OHLCV-WS: Unsubscription failed', { channel, error });
244
- __classPrivateFieldGet(this, _OHLCVService_messenger, "f").publish('OHLCVService:subscriptionError', {
245
- channel,
246
- error: String(error),
247
- operation: 'unsubscribe',
248
- });
249
- }
358
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_clearChannelTimers).call(this, __classPrivateFieldGet(this, _OHLCVService_channels, "f").get(channel) ?? { refCount: 0 });
250
359
  }
251
360
  finally {
252
361
  releaseLock();
253
362
  }
363
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_scheduleUnsubscribeRetry).call(this, channel);
254
364
  }, _OHLCVService_resubscribeActiveChannels =
255
365
  /**
256
366
  * Resubscribe all channels that were active before a disconnect.
@@ -263,8 +373,10 @@ async function _OHLCVService_resubscribeActiveChannels() {
263
373
  log('OHLCV-WS: Resubscribing active channels after reconnect', {
264
374
  count: channelCount,
265
375
  });
266
- for (const [channel, entry] of __classPrivateFieldGet(this, _OHLCVService_channels, "f").entries()) {
376
+ for (const [channel, entry] of [...__classPrivateFieldGet(this, _OHLCVService_channels, "f").entries()]) {
267
377
  if (entry.refCount === 0) {
378
+ __classPrivateFieldGet(this, _OHLCVService_instances, "m", _OHLCVService_clearChannelTimers).call(this, entry);
379
+ __classPrivateFieldGet(this, _OHLCVService_channels, "f").delete(channel);
268
380
  continue;
269
381
  }
270
382
  try {
@@ -1 +1 @@
1
- {"version":3,"file":"OHLCVService.mjs","sourceRoot":"","sources":["../../../src/ws/ohlcv/OHLCVService.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;;;;;;;;;;;;;AAQH,OAAO,EAAE,KAAK,EAAE,oBAAoB;AAEpC,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,yBAAwB;AAOpE,OAAO,EAAE,cAAc,EAAE,uCAAsC;AAI/D,gFAAgF;AAChF,YAAY;AACZ,gFAAgF;AAEhF,MAAM,YAAY,GAAG,cAAc,CAAC;AAEpC,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,YAAY,CAAC,CAAC;AAE5D,MAAM,yBAAyB,GAAG,CAAC,WAAW,EAAE,aAAa,CAAU,CAAC;AAExE,MAAM,sBAAsB,GAAG,gBAAgB,CAAC;AAEhD,MAAM,4BAA4B,GAAG,2BAA2B,sBAAsB,EAAE,CAAC;AAEzF,mF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esContent":["/**\n * OHLCV Service for real-time candlestick data streaming via WebSocket.\n *\n * Wraps {@link BackendWebSocketService} through the messenger pattern to\n * provide subscribe/unsubscribe semantics for OHLCV market-data channels.\n * Includes reference counting, grace-period unsubscribe, idempotency checks,\n * chain-status forwarding, and automatic resubscription on reconnect.\n */\n\nimport type {\n TraceCallback,\n TraceContext,\n TraceRequest,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport { Mutex } from 'async-mutex';\n\nimport { projectLogger, createModuleLogger } from '../../logger.js';\nimport type { BackendWebSocketServiceMethodActions } from '../BackendWebSocketService-method-action-types.js';\nimport type {\n WebSocketConnectionInfo,\n BackendWebSocketServiceConnectionStateChangedEvent,\n ServerNotificationMessage,\n} from '../BackendWebSocketService.js';\nimport { WebSocketState } from '../BackendWebSocketService.js';\nimport type { OHLCVServiceMethodActions } from './OHLCVService-method-action-types.js';\nimport type { OHLCVBar, OHLCVSubscriptionOptions } from './types.js';\n\n// =============================================================================\n// Constants\n// =============================================================================\n\nconst SERVICE_NAME = 'OHLCVService';\n\nconst log = createModuleLogger(projectLogger, SERVICE_NAME);\n\nconst MESSENGER_EXPOSED_METHODS = ['subscribe', 'unsubscribe'] as const;\n\nconst SUBSCRIPTION_NAMESPACE = 'market-data.v1';\n\nconst SYSTEM_NOTIFICATIONS_CHANNEL = `system-notifications.v1.${SUBSCRIPTION_NAMESPACE}`;\n\n/** Delay before actually unsubscribing from a channel after refCount reaches 0. */\nconst GRACE_PERIOD_MS = 3_000;\n\n// =============================================================================\n// Types — Channel Tracking\n// =============================================================================\n\ntype ChannelEntry = {\n refCount: number;\n gracePeriodTimer?: ReturnType<typeof setTimeout>;\n};\n\n// =============================================================================\n// Types — System Notifications\n// =============================================================================\n\n/**\n * System notification data for chain status updates on market-data channels.\n */\nexport type OHLCVSystemNotificationData = {\n chainIds: string[];\n status: 'down' | 'up';\n timestamp?: number;\n};\n\n// =============================================================================\n// Types — Service Options\n// =============================================================================\n\n/**\n * Configuration options for the OHLCV service.\n */\nexport type OHLCVServiceOptions = {\n /** Optional callback to trace performance of OHLCV operations (default: no-op) */\n traceFn?: TraceCallback;\n};\n\n// =============================================================================\n// Action and Event Types\n// =============================================================================\n\nexport type OHLCVServiceActions = OHLCVServiceMethodActions;\n\nexport const OHLCV_SERVICE_ALLOWED_ACTIONS = [\n 'BackendWebSocketService:connect',\n 'BackendWebSocketService:forceReconnection',\n 'BackendWebSocketService:subscribe',\n 'BackendWebSocketService:getConnectionInfo',\n 'BackendWebSocketService:channelHasSubscription',\n 'BackendWebSocketService:getSubscriptionsByChannel',\n 'BackendWebSocketService:findSubscriptionsByChannelPrefix',\n 'BackendWebSocketService:addChannelCallback',\n 'BackendWebSocketService:removeChannelCallback',\n] as const;\n\nexport const OHLCV_SERVICE_ALLOWED_EVENTS = [\n 'BackendWebSocketService:connectionStateChanged',\n] as const;\n\nexport type AllowedActions = BackendWebSocketServiceMethodActions;\n\n// Events published by OHLCVService\n\nexport type OHLCVServiceBarUpdatedEvent = {\n type: `OHLCVService:barUpdated`;\n payload: [{ channel: string; bar: OHLCVBar }];\n};\n\nexport type OHLCVServiceChainStatusChangedEvent = {\n type: `OHLCVService:chainStatusChanged`;\n payload: [{ chainIds: string[]; status: 'up' | 'down'; timestamp?: number }];\n};\n\nexport type OHLCVServiceSubscriptionErrorEvent = {\n type: `OHLCVService:subscriptionError`;\n payload: [{ channel: string; error: string; operation: string }];\n};\n\nexport type OHLCVServiceEvents =\n | OHLCVServiceBarUpdatedEvent\n | OHLCVServiceChainStatusChangedEvent\n | OHLCVServiceSubscriptionErrorEvent;\n\nexport type AllowedEvents = BackendWebSocketServiceConnectionStateChangedEvent;\n\nexport type OHLCVServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n OHLCVServiceActions | AllowedActions,\n OHLCVServiceEvents | AllowedEvents\n>;\n\n// =============================================================================\n// Main Service Class\n// =============================================================================\n\n/**\n * Service for real-time OHLCV candlestick streaming via the backend WebSocket\n * gateway. Communicates with {@link BackendWebSocketService} exclusively\n * through the messenger — no direct import of the class.\n *\n * Features:\n * - Reference counting: multiple UI consumers share one WebSocket subscription\n * - Grace-period unsubscribe: avoids rapid unsub/resub during navigation\n * - Idempotency: duplicate subscribe calls for the same channel are no-ops\n * - Reconnect resilience: resubscribes all active channels on reconnect\n * - Chain-status forwarding: listens to system-notifications for chain up/down\n *\n */\nexport class OHLCVService {\n readonly name = SERVICE_NAME;\n\n readonly #messenger: OHLCVServiceMessenger;\n\n readonly #trace: TraceCallback;\n\n readonly #channels = new Map<string, ChannelEntry>();\n\n readonly #mutex = new Mutex();\n\n readonly #chainsUp = new Set<string>();\n\n // =============================================================================\n // Constructor\n // =============================================================================\n\n constructor(\n options: OHLCVServiceOptions & { messenger: OHLCVServiceMessenger },\n ) {\n this.#messenger = options.messenger;\n\n this.#trace =\n options.traceFn ??\n ((<Result>(\n _request: TraceRequest,\n fn?: (context?: TraceContext) => Result,\n ) => fn?.()) as TraceCallback);\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.#messenger.subscribe(\n 'BackendWebSocketService:connectionStateChanged',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n (connectionInfo: WebSocketConnectionInfo) =>\n this.#handleWebSocketStateChange(connectionInfo),\n );\n }\n\n /**\n * Register the system-notifications channel callback.\n */\n init(): void {\n log('OHLCV-WS: Initializing — registering system-notifications callback');\n this.#messenger.call('BackendWebSocketService:addChannelCallback', {\n channelName: SYSTEM_NOTIFICATIONS_CHANNEL,\n callback: (notification: ServerNotificationMessage) =>\n this.#handleSystemNotification(notification),\n });\n }\n\n // =============================================================================\n // Public — Subscribe / Unsubscribe\n // =============================================================================\n\n /**\n * Subscribe to an OHLCV channel. If this is the first subscriber for the\n * given asset/interval/currency combination a WebSocket subscription is\n * created. Additional calls for the same combination only bump the reference\n * count.\n *\n * @param options - The subscription parameters.\n * @returns A promise that resolves once the subscription is established.\n */\n async subscribe(options: OHLCVSubscriptionOptions): Promise<void> {\n const channel = this.#buildChannel(options);\n const releaseLock = await this.#mutex.acquire();\n try {\n await this.#subscribeInner(channel);\n } finally {\n releaseLock();\n }\n }\n\n async #subscribeInner(channel: string): Promise<void> {\n const entry = this.#channels.get(channel);\n\n if (entry?.gracePeriodTimer) {\n clearTimeout(entry.gracePeriodTimer);\n entry.gracePeriodTimer = undefined;\n log('OHLCV-WS: Cancelled grace-period unsubscribe', {\n channel,\n });\n\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n entry.refCount += 1;\n log('OHLCV-WS: WS subscription still alive, bumped refCount', {\n channel,\n refCount: entry.refCount,\n });\n return;\n }\n // WS subscription was lost (e.g. after disconnect/reconnect) — fall\n // through to recreate it. refCount is bumped only after success below.\n } else if (entry && entry.refCount > 0) {\n entry.refCount += 1;\n return;\n }\n try {\n await this.#messenger.call('BackendWebSocketService:connect');\n\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n log(\n 'OHLCV-WS: Channel already has WS subscription (idempotency), skipping',\n {\n channel,\n },\n );\n this.#channels.set(channel, { refCount: 1 });\n return;\n }\n\n await this.#messenger.call('BackendWebSocketService:subscribe', {\n channels: [channel],\n channelType: SUBSCRIPTION_NAMESPACE,\n callback: (notification: ServerNotificationMessage) => {\n this.#handleBarUpdate(channel, notification);\n },\n });\n\n this.#channels.set(channel, { refCount: 1 });\n log('OHLCV-WS: Subscribe succeeded — new WS subscription created', {\n channel,\n });\n } catch (error) {\n log('OHLCV-WS: Subscription failed', { channel, error });\n this.#channels.delete(channel);\n this.#messenger.publish('OHLCVService:subscriptionError', {\n channel,\n error: String(error),\n operation: 'subscribe',\n });\n }\n }\n\n /**\n * Unsubscribe from an OHLCV channel. Decrements the reference count and,\n * when it reaches zero, starts a grace-period timer before actually\n * unsubscribing from the WebSocket to absorb rapid navigation patterns.\n *\n * @param options - The subscription parameters to unsubscribe from.\n * @returns A promise that resolves once the unsubscription is processed.\n */\n async unsubscribe(options: OHLCVSubscriptionOptions): Promise<void> {\n const channel = this.#buildChannel(options);\n const releaseLock = await this.#mutex.acquire();\n try {\n await this.#unsubscribeInner(channel);\n } finally {\n releaseLock();\n }\n }\n\n async #unsubscribeInner(channel: string): Promise<void> {\n const entry = this.#channels.get(channel);\n\n if (!entry || entry.refCount <= 0) {\n return;\n }\n\n entry.refCount -= 1;\n\n if (entry.refCount > 0) {\n return;\n }\n\n entry.gracePeriodTimer = setTimeout(() => {\n entry.gracePeriodTimer = undefined;\n this.#performUnsubscribe(channel).catch(() => {\n // no-op\n });\n }, GRACE_PERIOD_MS);\n }\n\n // =============================================================================\n // Private — WebSocket Subscription Helpers\n // =============================================================================\n\n async #performUnsubscribe(channel: string): Promise<void> {\n const releaseLock = await this.#mutex.acquire();\n try {\n const entry = this.#channels.get(channel);\n if (entry && entry.refCount > 0) {\n log(\n 'OHLCV-WS: Skipping unsubscribe — new subscriber arrived while queued',\n { channel, refCount: entry.refCount },\n );\n return;\n }\n\n log('OHLCV-WS: Grace period expired — performing actual WS unsubscribe', {\n channel,\n });\n this.#channels.delete(channel);\n\n try {\n const subscriptions = this.#messenger.call(\n 'BackendWebSocketService:getSubscriptionsByChannel',\n channel,\n );\n\n for (const sub of subscriptions) {\n await sub.unsubscribe();\n }\n log('OHLCV-WS: WS unsubscribe completed', { channel });\n } catch (error) {\n log('OHLCV-WS: Unsubscription failed', { channel, error });\n this.#messenger.publish('OHLCVService:subscriptionError', {\n channel,\n error: String(error),\n operation: 'unsubscribe',\n });\n }\n } finally {\n releaseLock();\n }\n }\n\n /**\n * Resubscribe all channels that were active before a disconnect.\n * Called when WebSocket transitions to CONNECTED.\n */\n async #resubscribeActiveChannels(): Promise<void> {\n const releaseLock = await this.#mutex.acquire();\n try {\n const channelCount = this.#channels.size;\n log('OHLCV-WS: Resubscribing active channels after reconnect', {\n count: channelCount,\n });\n\n for (const [channel, entry] of this.#channels.entries()) {\n if (entry.refCount === 0) {\n continue;\n }\n\n try {\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n log(\n 'OHLCV-WS: Channel already subscribed on server, skipping resubscribe',\n {\n channel,\n },\n );\n continue;\n }\n\n await this.#messenger.call('BackendWebSocketService:subscribe', {\n channels: [channel],\n channelType: SUBSCRIPTION_NAMESPACE,\n callback: (notification: ServerNotificationMessage) => {\n this.#handleBarUpdate(channel, notification);\n },\n });\n log('OHLCV-WS: Resubscription succeeded', { channel });\n } catch (error) {\n log('OHLCV-WS: Resubscription failed for channel', {\n channel,\n error,\n });\n }\n }\n } finally {\n releaseLock();\n }\n }\n\n // =============================================================================\n // Private — Message Handlers\n // =============================================================================\n\n #handleBarUpdate(\n channel: string,\n notification: ServerNotificationMessage,\n ): void {\n const bar = notification.data as OHLCVBar;\n\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#trace(\n {\n name: `${SERVICE_NAME} Bar Update`,\n data: { channel, timestamp: bar.timestamp },\n tags: { service: SERVICE_NAME },\n },\n () => {\n this.#messenger.publish('OHLCVService:barUpdated', { channel, bar });\n },\n );\n }\n\n #handleSystemNotification(notification: ServerNotificationMessage): void {\n const data = notification.data as OHLCVSystemNotificationData;\n const { timestamp } = notification;\n\n if (!data.chainIds || !Array.isArray(data.chainIds) || !data.status) {\n throw new Error(\n 'Invalid system notification data: missing chainIds or status',\n );\n }\n\n if (data.status === 'up') {\n for (const chainId of data.chainIds) {\n this.#chainsUp.add(chainId);\n }\n } else {\n for (const chainId of data.chainIds) {\n this.#chainsUp.delete(chainId);\n }\n }\n\n this.#messenger.publish('OHLCVService:chainStatusChanged', {\n chainIds: data.chainIds,\n status: data.status,\n timestamp,\n });\n\n log(`OHLCV-WS: Chain status change: ${data.status}`, {\n chains: data.chainIds,\n status: data.status,\n });\n }\n\n async #handleWebSocketStateChange(\n connectionInfo: WebSocketConnectionInfo,\n ): Promise<void> {\n const { state } = connectionInfo;\n\n if (state === WebSocketState.CONNECTED) {\n await this.#resubscribeActiveChannels();\n } else if (state === WebSocketState.DISCONNECTED) {\n const chainsToMarkDown = Array.from(this.#chainsUp);\n\n if (chainsToMarkDown.length > 0) {\n this.#messenger.publish('OHLCVService:chainStatusChanged', {\n chainIds: chainsToMarkDown,\n status: 'down',\n timestamp: Date.now(),\n });\n\n log(\n 'OHLCV-WS: WebSocket disconnection — marked tracked chains as down',\n {\n count: chainsToMarkDown.length,\n chains: chainsToMarkDown,\n },\n );\n\n this.#chainsUp.clear();\n }\n }\n }\n\n // =============================================================================\n // Private — Utility\n // =============================================================================\n\n #buildChannel(options: OHLCVSubscriptionOptions): string {\n return `${SUBSCRIPTION_NAMESPACE}.${options.assetId}.${options.interval}.${options.currency}`;\n }\n\n // =============================================================================\n // Public — Cleanup\n // =============================================================================\n\n /**\n * Destroy the service and clean up all resources.\n */\n destroy(): void {\n for (const entry of this.#channels.values()) {\n if (entry.gracePeriodTimer) {\n clearTimeout(entry.gracePeriodTimer);\n }\n }\n this.#channels.clear();\n this.#chainsUp.clear();\n\n this.#messenger.call(\n 'BackendWebSocketService:removeChannelCallback',\n SYSTEM_NOTIFICATIONS_CHANNEL,\n );\n }\n}\n"]}
1
+ {"version":3,"file":"OHLCVService.mjs","sourceRoot":"","sources":["../../../src/ws/ohlcv/OHLCVService.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;;;;;;;;;;;;;AAQH,OAAO,EAAE,KAAK,EAAE,oBAAoB;AACpC,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,KAAK,EAAE,kBAAkB;AAE9D,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,yBAAwB;AAOpE,OAAO,EAAE,cAAc,EAAE,uCAAsC;AAI/D,gFAAgF;AAChF,YAAY;AACZ,gFAAgF;AAEhF,MAAM,YAAY,GAAG,cAAc,CAAC;AAEpC,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,YAAY,CAAC,CAAC;AAE5D,MAAM,yBAAyB,GAA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gBACE,KAAK,EAAE,gBAAgB,CAAC,MAAM;gBAC9B,MAAM,EAAE,gBAAgB;aACzB,CACF,CAAC;YAEF,uBAAA,IAAI,8BAAU,CAAC,KAAK,EAAE,CAAC;QACzB,CAAC;IACH,CAAC;AACH,CAAC,mEAMa,OAAiC;IAC7C,OAAO,GAAG,sBAAsB,IAAI,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;AAChG,CAAC","sourcesContent":["/**\n * OHLCV Service for real-time candlestick data streaming via WebSocket.\n *\n * Wraps {@link BackendWebSocketService} through the messenger pattern to\n * provide subscribe/unsubscribe semantics for OHLCV market-data channels.\n * Includes reference counting, grace-period unsubscribe, idempotency checks,\n * chain-status forwarding, and automatic resubscription on reconnect.\n */\n\nimport type {\n TraceCallback,\n TraceContext,\n TraceRequest,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport { Mutex } from 'async-mutex';\nimport { handleAll, IterableBackoff, retry } from 'cockatiel';\n\nimport { projectLogger, createModuleLogger } from '../../logger.js';\nimport type { BackendWebSocketServiceMethodActions } from '../BackendWebSocketService-method-action-types.js';\nimport type {\n WebSocketConnectionInfo,\n BackendWebSocketServiceConnectionStateChangedEvent,\n ServerNotificationMessage,\n} from '../BackendWebSocketService.js';\nimport { WebSocketState } from '../BackendWebSocketService.js';\nimport type { OHLCVServiceMethodActions } from './OHLCVService-method-action-types.js';\nimport type { OHLCVBar, OHLCVSubscriptionOptions } from './types.js';\n\n// =============================================================================\n// Constants\n// =============================================================================\n\nconst SERVICE_NAME = 'OHLCVService';\n\nconst log = createModuleLogger(projectLogger, SERVICE_NAME);\n\nconst MESSENGER_EXPOSED_METHODS = ['subscribe', 'unsubscribe'] as const;\n\nconst SUBSCRIPTION_NAMESPACE = 'market-data.v1';\n\nconst SYSTEM_NOTIFICATIONS_CHANNEL = `system-notifications.v1.${SUBSCRIPTION_NAMESPACE}`;\n\n/** Delay before actually unsubscribing from a channel after refCount reaches 0. */\nconst GRACE_PERIOD_MS = 3_000;\n\n/** Backoff delays between failed WebSocket unsubscribe attempts. */\nconst UNSUB_RETRY_DELAYS_MS = [1_000, 2_000, 4_000] as const;\n\nconst unsubRetryPolicy = retry(handleAll, {\n // Cockatiel stops retrying once the failure index reaches `maxAttempts`, so\n // `length` here yields one initial attempt plus three delayed retries (4 total).\n maxAttempts: UNSUB_RETRY_DELAYS_MS.length,\n backoff: new IterableBackoff([...UNSUB_RETRY_DELAYS_MS]),\n});\n\n// =============================================================================\n// Types — Channel Tracking\n// =============================================================================\n\ntype ChannelEntry = {\n refCount: number;\n gracePeriodTimer?: ReturnType<typeof setTimeout>;\n retryAbort?: AbortController;\n};\n\n// =============================================================================\n// Types — System Notifications\n// =============================================================================\n\n/**\n * System notification data for chain status updates on market-data channels.\n */\nexport type OHLCVSystemNotificationData = {\n chainIds: string[];\n status: 'down' | 'up';\n timestamp?: number;\n};\n\n// =============================================================================\n// Types — Service Options\n// =============================================================================\n\n/**\n * Configuration options for the OHLCV service.\n */\nexport type OHLCVServiceOptions = {\n /** Optional callback to trace performance of OHLCV operations (default: no-op) */\n traceFn?: TraceCallback;\n};\n\n// =============================================================================\n// Action and Event Types\n// =============================================================================\n\nexport type OHLCVServiceActions = OHLCVServiceMethodActions;\n\nexport const OHLCV_SERVICE_ALLOWED_ACTIONS = [\n 'BackendWebSocketService:connect',\n 'BackendWebSocketService:forceReconnection',\n 'BackendWebSocketService:subscribe',\n 'BackendWebSocketService:getConnectionInfo',\n 'BackendWebSocketService:channelHasSubscription',\n 'BackendWebSocketService:getSubscriptionsByChannel',\n 'BackendWebSocketService:findSubscriptionsByChannelPrefix',\n 'BackendWebSocketService:addChannelCallback',\n 'BackendWebSocketService:removeChannelCallback',\n] as const;\n\nexport const OHLCV_SERVICE_ALLOWED_EVENTS = [\n 'BackendWebSocketService:connectionStateChanged',\n] as const;\n\nexport type AllowedActions = BackendWebSocketServiceMethodActions;\n\n// Events published by OHLCVService\n\nexport type OHLCVServiceBarUpdatedEvent = {\n type: `OHLCVService:barUpdated`;\n payload: [{ channel: string; bar: OHLCVBar }];\n};\n\nexport type OHLCVServiceChainStatusChangedEvent = {\n type: `OHLCVService:chainStatusChanged`;\n payload: [{ chainIds: string[]; status: 'up' | 'down'; timestamp?: number }];\n};\n\nexport type OHLCVServiceSubscriptionErrorEvent = {\n type: `OHLCVService:subscriptionError`;\n payload: [{ channel: string; error: string; operation: string }];\n};\n\nexport type OHLCVServiceEvents =\n | OHLCVServiceBarUpdatedEvent\n | OHLCVServiceChainStatusChangedEvent\n | OHLCVServiceSubscriptionErrorEvent;\n\nexport type AllowedEvents = BackendWebSocketServiceConnectionStateChangedEvent;\n\nexport type OHLCVServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n OHLCVServiceActions | AllowedActions,\n OHLCVServiceEvents | AllowedEvents\n>;\n\n// =============================================================================\n// Main Service Class\n// =============================================================================\n\n/**\n * Service for real-time OHLCV candlestick streaming via the backend WebSocket\n * gateway. Communicates with {@link BackendWebSocketService} exclusively\n * through the messenger — no direct import of the class.\n *\n * Features:\n * - Reference counting: multiple UI consumers share one WebSocket subscription\n * - Grace-period unsubscribe: reuses same-channel subs on rapid back navigation\n * - Grace-period flush: immediately unsubscribes other channels on navigation\n * - Unsubscribe retry: retries failed unsubs with backoff before force reconnect\n * - Idempotency: duplicate subscribe calls for the same channel are no-ops\n * - Reconnect resilience: resubscribes all active channels on reconnect\n * - Chain-status forwarding: listens to system-notifications for chain up/down\n *\n */\nexport class OHLCVService {\n readonly name = SERVICE_NAME;\n\n readonly #messenger: OHLCVServiceMessenger;\n\n readonly #trace: TraceCallback;\n\n readonly #channels = new Map<string, ChannelEntry>();\n\n readonly #mutex = new Mutex();\n\n readonly #chainsUp = new Set<string>();\n\n // =============================================================================\n // Constructor\n // =============================================================================\n\n constructor(\n options: OHLCVServiceOptions & { messenger: OHLCVServiceMessenger },\n ) {\n this.#messenger = options.messenger;\n\n this.#trace =\n options.traceFn ??\n ((<Result>(\n _request: TraceRequest,\n fn?: (context?: TraceContext) => Result,\n ) => fn?.()) as TraceCallback);\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.#messenger.subscribe(\n 'BackendWebSocketService:connectionStateChanged',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n (connectionInfo: WebSocketConnectionInfo) =>\n this.#handleWebSocketStateChange(connectionInfo),\n );\n }\n\n /**\n * Register the system-notifications channel callback.\n */\n init(): void {\n log('OHLCV-WS: Initializing — registering system-notifications callback');\n this.#messenger.call('BackendWebSocketService:addChannelCallback', {\n channelName: SYSTEM_NOTIFICATIONS_CHANNEL,\n callback: (notification: ServerNotificationMessage) =>\n this.#handleSystemNotification(notification),\n });\n }\n\n // =============================================================================\n // Public — Subscribe / Unsubscribe\n // =============================================================================\n\n /**\n * Subscribe to an OHLCV channel. If this is the first subscriber for the\n * given asset/interval/currency combination a WebSocket subscription is\n * created. Additional calls for the same combination only bump the reference\n * count.\n *\n * @param options - The subscription parameters.\n * @returns A promise that resolves once the subscription is established.\n */\n async subscribe(options: OHLCVSubscriptionOptions): Promise<void> {\n const channel = this.#buildChannel(options);\n const releaseLock = await this.#mutex.acquire();\n try {\n await this.#subscribeInner(channel);\n } finally {\n releaseLock();\n }\n }\n\n async #subscribeInner(channel: string): Promise<void> {\n const entry = this.#channels.get(channel);\n\n if (entry?.retryAbort) {\n entry.retryAbort.abort();\n entry.retryAbort = undefined;\n entry.refCount = 1;\n\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n await this.#flushOtherChannels(channel);\n log('OHLCV-WS: Cancelled unsubscribe retry — reusing WS subscription', {\n channel,\n });\n return;\n }\n // WS subscription was lost — fall through to recreate it.\n } else if (entry?.gracePeriodTimer) {\n clearTimeout(entry.gracePeriodTimer);\n entry.gracePeriodTimer = undefined;\n log('OHLCV-WS: Cancelled grace-period unsubscribe', {\n channel,\n });\n\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n await this.#flushOtherChannels(channel);\n entry.refCount += 1;\n log('OHLCV-WS: WS subscription still alive, bumped refCount', {\n channel,\n refCount: entry.refCount,\n });\n return;\n }\n // WS subscription was lost (e.g. after disconnect/reconnect) — fall\n // through to recreate it. refCount is bumped only after success below.\n } else if (entry && entry.refCount > 0) {\n entry.refCount += 1;\n return;\n }\n\n await this.#flushOtherChannels(channel);\n\n try {\n await this.#messenger.call('BackendWebSocketService:connect');\n\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n log(\n 'OHLCV-WS: Channel already has WS subscription (idempotency), skipping',\n {\n channel,\n },\n );\n this.#channels.set(channel, { refCount: 1 });\n return;\n }\n\n await this.#messenger.call('BackendWebSocketService:subscribe', {\n channels: [channel],\n channelType: SUBSCRIPTION_NAMESPACE,\n callback: (notification: ServerNotificationMessage) => {\n this.#handleBarUpdate(channel, notification);\n },\n });\n\n this.#channels.set(channel, { refCount: 1 });\n log('OHLCV-WS: Subscribe succeeded — new WS subscription created', {\n channel,\n });\n } catch (error) {\n log('OHLCV-WS: Subscription failed', { channel, error });\n this.#channels.delete(channel);\n this.#messenger.publish('OHLCVService:subscriptionError', {\n channel,\n error: String(error),\n operation: 'subscribe',\n });\n }\n }\n\n /**\n * Unsubscribe from an OHLCV channel. Decrements the reference count and,\n * when it reaches zero, starts a grace-period timer before actually\n * unsubscribing from the WebSocket to absorb rapid navigation patterns.\n *\n * @param options - The subscription parameters to unsubscribe from.\n * @returns A promise that resolves once the unsubscription is processed.\n */\n async unsubscribe(options: OHLCVSubscriptionOptions): Promise<void> {\n const channel = this.#buildChannel(options);\n const releaseLock = await this.#mutex.acquire();\n try {\n await this.#unsubscribeInner(channel);\n } finally {\n releaseLock();\n }\n }\n\n async #unsubscribeInner(channel: string): Promise<void> {\n const entry = this.#channels.get(channel);\n\n if (!entry || entry.refCount <= 0) {\n return;\n }\n\n entry.refCount -= 1;\n\n if (entry.refCount > 0) {\n return;\n }\n\n entry.gracePeriodTimer = setTimeout(() => {\n entry.gracePeriodTimer = undefined;\n this.#performUnsubscribe(channel).catch(() => {\n // no-op: retry scheduling and force-reconnection are handled internally\n });\n }, GRACE_PERIOD_MS);\n }\n\n // =============================================================================\n // Private — WebSocket Subscription Helpers\n // =============================================================================\n\n /**\n * Immediately unsubscribe other channels in grace or failed-cleanup state.\n * Called while the subscribe mutex is held before opening a new channel.\n *\n * @param exceptChannel - Channel being subscribed; excluded from flush.\n */\n async #flushOtherChannels(exceptChannel: string): Promise<void> {\n for (const [channel, channelEntry] of this.#channels.entries()) {\n if (channel === exceptChannel || channelEntry.refCount > 0) {\n continue;\n }\n\n this.#clearChannelTimers(channelEntry);\n log('OHLCV-WS: Flushing grace-period channel before new subscribe', {\n flushedChannel: channel,\n newChannel: exceptChannel,\n });\n\n const success = await this.#unsubscribeChannelOnServer(channel);\n if (success) {\n this.#channels.delete(channel);\n } else {\n this.#scheduleUnsubscribeRetry(channel);\n }\n }\n }\n\n #clearChannelTimers(entry: ChannelEntry): void {\n if (entry.gracePeriodTimer) {\n clearTimeout(entry.gracePeriodTimer);\n entry.gracePeriodTimer = undefined;\n }\n entry.retryAbort?.abort();\n entry.retryAbort = undefined;\n }\n\n async #unsubscribeChannelOnServer(channel: string): Promise<boolean> {\n try {\n const subscriptions = this.#messenger.call(\n 'BackendWebSocketService:getSubscriptionsByChannel',\n channel,\n );\n\n for (const sub of subscriptions) {\n await sub.unsubscribe();\n }\n return true;\n } catch (error) {\n log('OHLCV-WS: Unsubscription failed', { channel, error });\n this.#messenger.publish('OHLCVService:subscriptionError', {\n channel,\n error: String(error),\n operation: 'unsubscribe',\n });\n return false;\n }\n }\n\n #scheduleUnsubscribeRetry(channel: string): void {\n const entry = this.#channels.get(channel);\n if (!entry) {\n // The channel was removed (e.g. destroy() or reconnect cleanup) while the\n // unsubscribe was in flight. Do not resurrect it or start a new retry loop\n // with an AbortController that teardown can no longer cancel — that could\n // force a reconnection on the shared WebSocket after teardown.\n return;\n }\n\n entry.retryAbort?.abort();\n entry.retryAbort = new AbortController();\n\n const { signal } = entry.retryAbort;\n\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#runUnsubRetryLoop(channel, signal);\n }\n\n async #runUnsubRetryLoop(\n channel: string,\n signal: AbortSignal,\n ): Promise<void> {\n try {\n await unsubRetryPolicy.execute(async () => {\n const releaseLock = await this.#mutex.acquire();\n try {\n const current = this.#channels.get(channel);\n if (current && current.refCount > 0) {\n return;\n }\n\n const success = await this.#unsubscribeChannelOnServer(channel);\n if (!success) {\n throw new Error('unsubscribe failed');\n }\n\n this.#channels.delete(channel);\n log('OHLCV-WS: WS unsubscribe completed', { channel });\n } finally {\n releaseLock();\n }\n }, signal);\n } catch {\n if (signal.aborted) {\n return;\n }\n\n log('OHLCV-WS: Unsubscribe retries exhausted — forcing reconnection', {\n channel,\n });\n this.#channels.delete(channel);\n // Last resort: reconnects the shared BackendWebSocketService instance\n // (AccountActivityService and other consumers share this connection).\n // They resubscribe on CONNECTED; OHLCV only resubscribes refCount > 0.\n await this.#messenger\n .call('BackendWebSocketService:forceReconnection')\n .catch(() => {\n // no-op\n });\n }\n }\n\n async #performUnsubscribe(channel: string): Promise<void> {\n const releaseLock = await this.#mutex.acquire();\n try {\n const entry = this.#channels.get(channel);\n if (entry && entry.refCount > 0) {\n log(\n 'OHLCV-WS: Skipping unsubscribe — new subscriber arrived while queued',\n { channel, refCount: entry.refCount },\n );\n return;\n }\n\n log('OHLCV-WS: Grace period expired — performing actual WS unsubscribe', {\n channel,\n });\n\n this.#clearChannelTimers(this.#channels.get(channel) ?? { refCount: 0 });\n } finally {\n releaseLock();\n }\n\n this.#scheduleUnsubscribeRetry(channel);\n }\n\n /**\n * Resubscribe all channels that were active before a disconnect.\n * Called when WebSocket transitions to CONNECTED.\n */\n async #resubscribeActiveChannels(): Promise<void> {\n const releaseLock = await this.#mutex.acquire();\n try {\n const channelCount = this.#channels.size;\n log('OHLCV-WS: Resubscribing active channels after reconnect', {\n count: channelCount,\n });\n\n for (const [channel, entry] of [...this.#channels.entries()]) {\n if (entry.refCount === 0) {\n this.#clearChannelTimers(entry);\n this.#channels.delete(channel);\n continue;\n }\n\n try {\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n log(\n 'OHLCV-WS: Channel already subscribed on server, skipping resubscribe',\n {\n channel,\n },\n );\n continue;\n }\n\n await this.#messenger.call('BackendWebSocketService:subscribe', {\n channels: [channel],\n channelType: SUBSCRIPTION_NAMESPACE,\n callback: (notification: ServerNotificationMessage) => {\n this.#handleBarUpdate(channel, notification);\n },\n });\n log('OHLCV-WS: Resubscription succeeded', { channel });\n } catch (error) {\n log('OHLCV-WS: Resubscription failed for channel', {\n channel,\n error,\n });\n }\n }\n } finally {\n releaseLock();\n }\n }\n\n // =============================================================================\n // Private — Message Handlers\n // =============================================================================\n\n #handleBarUpdate(\n channel: string,\n notification: ServerNotificationMessage,\n ): void {\n const bar = notification.data as OHLCVBar;\n\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#trace(\n {\n name: `${SERVICE_NAME} Bar Update`,\n data: { channel, timestamp: bar.timestamp },\n tags: { service: SERVICE_NAME },\n },\n () => {\n this.#messenger.publish('OHLCVService:barUpdated', { channel, bar });\n },\n );\n }\n\n #handleSystemNotification(notification: ServerNotificationMessage): void {\n const data = notification.data as OHLCVSystemNotificationData;\n const { timestamp } = notification;\n\n if (!data.chainIds || !Array.isArray(data.chainIds) || !data.status) {\n throw new Error(\n 'Invalid system notification data: missing chainIds or status',\n );\n }\n\n if (data.status === 'up') {\n for (const chainId of data.chainIds) {\n this.#chainsUp.add(chainId);\n }\n } else {\n for (const chainId of data.chainIds) {\n this.#chainsUp.delete(chainId);\n }\n }\n\n this.#messenger.publish('OHLCVService:chainStatusChanged', {\n chainIds: data.chainIds,\n status: data.status,\n timestamp,\n });\n\n log(`OHLCV-WS: Chain status change: ${data.status}`, {\n chains: data.chainIds,\n status: data.status,\n });\n }\n\n async #handleWebSocketStateChange(\n connectionInfo: WebSocketConnectionInfo,\n ): Promise<void> {\n const { state } = connectionInfo;\n\n if (state === WebSocketState.CONNECTED) {\n await this.#resubscribeActiveChannels();\n } else if (state === WebSocketState.DISCONNECTED) {\n const chainsToMarkDown = Array.from(this.#chainsUp);\n\n if (chainsToMarkDown.length > 0) {\n this.#messenger.publish('OHLCVService:chainStatusChanged', {\n chainIds: chainsToMarkDown,\n status: 'down',\n timestamp: Date.now(),\n });\n\n log(\n 'OHLCV-WS: WebSocket disconnection — marked tracked chains as down',\n {\n count: chainsToMarkDown.length,\n chains: chainsToMarkDown,\n },\n );\n\n this.#chainsUp.clear();\n }\n }\n }\n\n // =============================================================================\n // Private — Utility\n // =============================================================================\n\n #buildChannel(options: OHLCVSubscriptionOptions): string {\n return `${SUBSCRIPTION_NAMESPACE}.${options.assetId}.${options.interval}.${options.currency}`;\n }\n\n // =============================================================================\n // Public — Cleanup\n // =============================================================================\n\n /**\n * Destroy the service and clean up all resources.\n */\n destroy(): void {\n for (const entry of this.#channels.values()) {\n this.#clearChannelTimers(entry);\n }\n this.#channels.clear();\n this.#chainsUp.clear();\n\n this.#messenger.call(\n 'BackendWebSocketService:removeChannelCallback',\n SYSTEM_NOTIFICATIONS_CHANNEL,\n );\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/core-backend",
3
- "version": "8.1.0-preview-beea66146",
3
+ "version": "8.1.0-preview-7b60eca",
4
4
  "description": "Core backend services for MetaMask",
5
5
  "keywords": [
6
6
  "Ethereum",
@@ -64,6 +64,7 @@
64
64
  "@metamask/utils": "^11.11.0",
65
65
  "@tanstack/query-core": "^5.62.16",
66
66
  "async-mutex": "^0.5.0",
67
+ "cockatiel": "^3.1.2",
67
68
  "uuid": "^8.3.2"
68
69
  },
69
70
  "devDependencies": {