Skip to content

Commit 5308071

Browse files
fix(extension): fetch prices only while session is active (#2003)
1 parent 177d837 commit 5308071

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

apps/browser-extension-wallet/src/lib/scripts/background/cip30.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { cip30 as walletCip30 } from '@cardano-sdk/wallet';
22
import { ensureUiIsOpenAndLoaded } from './util';
33
import { userPromptService } from './services/dappService';
44
import { authenticator } from './authenticator';
5-
import { dAppConnectorActivity$, wallet$ } from './wallet';
5+
import { wallet$ } from './wallet';
6+
import { dAppConnectorActivity$ } from './session/poll-controller';
67
import { runtime, Tabs, tabs } from 'webextension-polyfill';
78
import { cip30, exposeApi, RemoteApiPropertyType } from '@cardano-sdk/web-extension';
89
import { DAPP_CHANNELS } from '../../../utils/constants';

apps/browser-extension-wallet/src/lib/scripts/background/services/utilityServices.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,18 @@ import {
1616
UnhandledError,
1717
WalletMode
1818
} from '../../types';
19-
import { Subject, of, BehaviorSubject, merge, map, fromEvent, Observable } from 'rxjs';
19+
import {
20+
Subject,
21+
of,
22+
BehaviorSubject,
23+
merge,
24+
map,
25+
fromEvent,
26+
Observable,
27+
filter,
28+
withLatestFrom,
29+
interval
30+
} from 'rxjs';
2031
import { walletRoutePaths } from '@routes/wallet-paths';
2132
import { backgroundServiceProperties } from '../config';
2233
import { ActiveWallet, exposeApi } from '@cardano-sdk/web-extension';
@@ -30,6 +41,7 @@ import { logger } from '@lace/common';
3041
import { POPUP_WINDOW_NAMI_TITLE } from '@utils/constants';
3142
import { catchAndBrandExtensionApiError } from '@utils/catch-and-brand-extension-api-error';
3243
import { initCardanoTokenPrices } from './cardanoTokenPrices';
44+
import { pollController$ } from '../session/poll-controller';
3345

3446
export const requestMessage$ = new Subject<Message>();
3547
export const backendFailures$ = new BehaviorSubject(0);
@@ -320,8 +332,15 @@ export const exposeBackgroundService = (wallet$: Observable<ActiveWallet>): void
320332
fetchAdaPrice(coinPrices);
321333
fetchBitcoinPrice(coinPrices);
322334
};
335+
// Fetch the prices initially, regardless of the session status
323336
updatePrices();
324-
setInterval(updatePrices, ADA_PRICE_CHECK_INTERVAL);
337+
// Fetch the prices periodically, only if the session is active
338+
interval(ADA_PRICE_CHECK_INTERVAL)
339+
.pipe(
340+
withLatestFrom(pollController$),
341+
filter(([, isActive]) => isActive)
342+
)
343+
.subscribe(updatePrices);
325344

326345
exposeApi<BackgroundService>(
327346
{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Subject, tap } from 'rxjs';
2+
import { TrackerSubject } from '@cardano-sdk/util-rxjs';
3+
import { logger } from '@lace/common';
4+
import { createUserSessionTracker } from './user-session-tracker';
5+
import { isLacePopupOpen$ } from './is-lace-popup-open';
6+
import { isLaceTabActive$ } from './is-lace-tab-active';
7+
import { SESSION_TIMEOUT } from '../config';
8+
9+
export const dAppConnectorActivity$ = new Subject<void>();
10+
11+
export const pollController$ = new TrackerSubject(
12+
createUserSessionTracker(isLacePopupOpen$, isLaceTabActive$, dAppConnectorActivity$, SESSION_TIMEOUT).pipe(
13+
tap((isActive) => logger.debug('Session active:', isActive))
14+
)
15+
);

apps/browser-extension-wallet/src/lib/scripts/background/wallet.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
/* eslint-disable unicorn/no-null, no-magic-numbers, promise/catch-or-return, promise/no-nesting */
22
import { runtime, storage as webStorage } from 'webextension-polyfill';
3-
import {
4-
BehaviorSubject,
5-
combineLatest,
6-
defaultIfEmpty,
7-
EMPTY,
8-
firstValueFrom,
9-
from,
10-
map,
11-
Observable,
12-
of,
13-
Subject,
14-
tap
15-
} from 'rxjs';
16-
import { getProviders, SESSION_TIMEOUT } from './config';
3+
import { BehaviorSubject, combineLatest, defaultIfEmpty, EMPTY, firstValueFrom, from, map, Observable, of } from 'rxjs';
4+
import { getProviders } from './config';
175
import { createPersonalWallet, createSharedWallet, DEFAULT_POLLING_CONFIG, storage } from '@cardano-sdk/wallet';
186
import { Cardano } from '@cardano-sdk/core';
197
import {
@@ -47,8 +35,7 @@ import { ExtensionDocumentStore } from './storage/extension-document-store';
4735
import { ExtensionBlobKeyValueStore } from './storage/extension-blob-key-value-store';
4836
import { ExtensionBlobCollectionStore } from './storage/extension-blob-collection-store';
4937
import { migrateCollectionStore, migrateWalletStores, shouldAttemptWalletStoresMigration } from './storage/migrations';
50-
import { createUserSessionTracker, isLacePopupOpen$, isLaceTabActive$ } from './session';
51-
import { TrackerSubject } from '@cardano-sdk/util-rxjs';
38+
import { pollController$ } from './session/poll-controller';
5239
import { ExperimentName, FeatureFlags } from '../types/feature-flags';
5340
import { TX_HISTORY_LIMIT_SIZE } from '@utils/constants';
5441
import { Bitcoin } from '@lace/bitcoin';
@@ -65,13 +52,6 @@ if (!isBackgroundProcess()) {
6552
throw new TypeError('This module should only be imported in service worker');
6653
}
6754

68-
export const dAppConnectorActivity$ = new Subject<void>();
69-
const pollController$ = new TrackerSubject(
70-
createUserSessionTracker(isLacePopupOpen$, isLaceTabActive$, dAppConnectorActivity$, SESSION_TIMEOUT).pipe(
71-
tap((isActive) => logger.debug('Session active:', isActive))
72-
)
73-
);
74-
7555
const networkMagicToChainName = (networkMagic: Cardano.NetworkMagic): Wallet.ChainName => {
7656
switch (networkMagic) {
7757
case Wallet.Cardano.ChainIds.Mainnet.networkMagic:

0 commit comments

Comments
 (0)