Skip to content

refactor!: hoist isBackgroundProcess script detection to utils package [LW-12130] #1603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/util/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ export const isProductionEnvironment = (): boolean => process.env.NODE_ENV === E

export const getEnvironmentConfiguration = (): Environment =>
isProductionEnvironment() ? Environment.Production : Environment.Development;

/**
* Chrome extensions use a service worker that does not have a window object.
* Firefox addons use a generated background page to run the background script, so they have a window object
*
* @returns {boolean} True if the script is running in a background/service worker process, false otherwise.
*/
export const isBackgroundProcess = () =>
typeof window === 'undefined' || window.location.href.includes('_generated_background');
3 changes: 2 additions & 1 deletion packages/wallet/src/services/util/connectionStatusTracker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NEVER, Observable, distinctUntilChanged, fromEvent, map, merge, shareReplay, startWith } from 'rxjs';
import { isBackgroundProcess } from '@cardano-sdk/util';

export enum ConnectionStatus {
down = 0,
Expand All @@ -21,7 +22,7 @@ export interface ConnectionStatusTrackerInternals {
* @returns {ConnectionStatusTracker} ConnectionStatusTracker
*/
export const createSimpleConnectionStatusTracker = ({
isNodeEnv = typeof window === 'undefined',
isNodeEnv = isBackgroundProcess(),
online$ = isNodeEnv ? NEVER : fromEvent(window, 'online'),
offline$ = isNodeEnv ? NEVER : fromEvent(window, 'offline'),
initialStatus = isNodeEnv ? true : navigator.onLine
Expand Down
3 changes: 2 additions & 1 deletion packages/web-extension/src/messaging/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { BackgroundMessenger, createBackgroundMessenger, generalizeBackgroundMessenger } from './BackgroundMessenger';
import { ChannelName, ConsumeRemoteApiOptions, ExposeApiProps, MessengerDependencies } from './types';
import { FinalizationRegistryDestructor, isBackgroundProcess } from './util';
import { FinalizationRegistryDestructor } from './util';
import { consumeMessengerRemoteApi, exposeMessengerApi } from './remoteApi';
import { createNonBackgroundMessenger } from './NonBackgroundMessenger';
import { isBackgroundProcess } from '@cardano-sdk/util';

export * from './BackgroundMessenger';
export * from './NonBackgroundMessenger';
Expand Down
4 changes: 0 additions & 4 deletions packages/web-extension/src/messaging/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,3 @@ export class FinalizationRegistryDestructor implements Destructor {
this.#registry.register(obj, objectId);
}
}

// Firefox addons use a generated background page to run the background script, so they have a window object
export const isBackgroundProcess = () =>
typeof window === 'undefined' || window.location.href.includes('_generated_background');
Loading