Skip to content

Commit 25ae251

Browse files
committed
refactor!: hoist isBackgroundProcess script detection to utils package
1 parent a78683f commit 25ae251

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

Diff for: packages/util/src/environment.ts

+9
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ export const isProductionEnvironment = (): boolean => process.env.NODE_ENV === E
1616

1717
export const getEnvironmentConfiguration = (): Environment =>
1818
isProductionEnvironment() ? Environment.Production : Environment.Development;
19+
20+
/**
21+
* Chrome extensions use a service worker that does not have a window object.
22+
* Firefox addons use a generated background page to run the background script, so they have a window object
23+
*
24+
* @returns {boolean} True if the script is running in a background/service worker process, false otherwise.
25+
*/
26+
export const isBackgroundProcess = () =>
27+
typeof window === 'undefined' || window.location.href.includes('_generated_background');

Diff for: packages/wallet/src/services/util/connectionStatusTracker.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { NEVER, Observable, distinctUntilChanged, fromEvent, map, merge, shareReplay, startWith } from 'rxjs';
2+
import { isBackgroundProcess } from '@cardano-sdk/util';
23

34
export enum ConnectionStatus {
45
down = 0,
@@ -21,7 +22,7 @@ export interface ConnectionStatusTrackerInternals {
2122
* @returns {ConnectionStatusTracker} ConnectionStatusTracker
2223
*/
2324
export const createSimpleConnectionStatusTracker = ({
24-
isNodeEnv = typeof window === 'undefined',
25+
isNodeEnv = isBackgroundProcess(),
2526
online$ = isNodeEnv ? NEVER : fromEvent(window, 'online'),
2627
offline$ = isNodeEnv ? NEVER : fromEvent(window, 'offline'),
2728
initialStatus = isNodeEnv ? true : navigator.onLine

Diff for: packages/web-extension/src/messaging/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { BackgroundMessenger, createBackgroundMessenger, generalizeBackgroundMessenger } from './BackgroundMessenger';
22
import { ChannelName, ConsumeRemoteApiOptions, ExposeApiProps, MessengerDependencies } from './types';
3-
import { FinalizationRegistryDestructor, isBackgroundProcess } from './util';
3+
import { FinalizationRegistryDestructor } from './util';
44
import { consumeMessengerRemoteApi, exposeMessengerApi } from './remoteApi';
55
import { createNonBackgroundMessenger } from './NonBackgroundMessenger';
6+
import { isBackgroundProcess } from '@cardano-sdk/util';
67

78
export * from './BackgroundMessenger';
89
export * from './NonBackgroundMessenger';

Diff for: packages/web-extension/src/messaging/util.ts

-4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,3 @@ export class FinalizationRegistryDestructor implements Destructor {
7777
this.#registry.register(obj, objectId);
7878
}
7979
}
80-
81-
// Firefox addons use a generated background page to run the background script, so they have a window object
82-
export const isBackgroundProcess = () =>
83-
typeof window === 'undefined' || window.location.href.includes('_generated_background');

0 commit comments

Comments
 (0)