Skip to content
Open
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
3 changes: 3 additions & 0 deletions packages/connectivity-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The controller now initializes with a default state (online) and requires calling `init()` to fetch the actual status
- Add `setConnectivityStatus` method to manually set connectivity status ([#7676](https://github.com/MetaMask/core/pull/7676))
- The method is exposed as a messenger action `ConnectivityController:setConnectivityStatus`
- Add `connectivityControllerSelectors` with `selectConnectivityStatus` and `selectIsOffline` selectors ([#7701](https://github.com/MetaMask/core/pull/7701))
- `selectConnectivityStatus` returns the current connectivity status from the controller state
- `selectIsOffline` is a memoized selector that returns `true` when the device is offline

### Changed

Expand Down
3 changes: 2 additions & 1 deletion packages/connectivity-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
},
"dependencies": {
"@metamask/base-controller": "^9.0.0",
"@metamask/messenger": "^0.3.0"
"@metamask/messenger": "^0.3.0",
"reselect": "^5.1.1"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
Expand Down
1 change: 1 addition & 0 deletions packages/connectivity-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export {
ConnectivityController,
getDefaultConnectivityControllerState,
} from './ConnectivityController';
export { connectivityControllerSelectors } from './selectors';
39 changes: 39 additions & 0 deletions packages/connectivity-controller/src/selectors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { ConnectivityControllerState } from './ConnectivityController';
import { connectivityControllerSelectors } from './selectors';
import { CONNECTIVITY_STATUSES } from './types';

describe('connectivityControllerSelectors', () => {
describe('selectConnectivityStatus', () => {
it.each([[CONNECTIVITY_STATUSES.Online], [CONNECTIVITY_STATUSES.Offline]])(
'returns %s when connectivityStatus is %s',
(connectivityStatus) => {
const state: ConnectivityControllerState = {
connectivityStatus,
};

const result =
connectivityControllerSelectors.selectConnectivityStatus(state);

expect(result).toBe(connectivityStatus);
},
);
});

describe('selectIsOffline', () => {
it.each([
[CONNECTIVITY_STATUSES.Online, false],
[CONNECTIVITY_STATUSES.Offline, true],
])(
'when connectivityStatus=%s, returns %s',
(connectivityStatus, expected) => {
const state: ConnectivityControllerState = {
connectivityStatus,
};

const result = connectivityControllerSelectors.selectIsOffline(state);

expect(result).toBe(expected);
},
);
});
});
35 changes: 35 additions & 0 deletions packages/connectivity-controller/src/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createSelector } from 'reselect';

import type { ConnectivityControllerState } from './ConnectivityController';
import { CONNECTIVITY_STATUSES } from './types';
import type { ConnectivityStatus } from './types';

/**
* Selects the connectivity status from the controller state.
*
* @param state - The controller state
* @returns The connectivity status
*/
const selectConnectivityStatus = (
state: ConnectivityControllerState,
): ConnectivityStatus => state.connectivityStatus;

/**
* Selects whether the device is offline.
*
* @param state - The controller state
* @returns Whether the device is offline
*/
const selectIsOffline = createSelector(
[selectConnectivityStatus],
(connectivityStatus) => connectivityStatus === CONNECTIVITY_STATUSES.Offline,
);

/**
* Selectors for the ConnectivityController state.
* These can be used with Redux or directly with controller state.
*/
export const connectivityControllerSelectors = {
selectConnectivityStatus,
selectIsOffline,
};
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2995,6 +2995,7 @@ __metadata:
"@types/jest": "npm:^27.4.1"
deepmerge: "npm:^4.2.2"
jest: "npm:^27.5.1"
reselect: "npm:^5.1.1"
ts-jest: "npm:^27.1.4"
typedoc: "npm:^0.24.8"
typedoc-plugin-missing-exports: "npm:^2.0.0"
Expand Down
Loading