@metamask-previews/connectivity-controller 0.1.0-preview-3685bfb → 0.1.0-preview-25d2d4502
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 +0 -1
- package/dist/ConnectivityController-method-action-types.cjs.map +1 -1
- package/dist/ConnectivityController-method-action-types.d.cts +1 -8
- package/dist/ConnectivityController-method-action-types.d.cts.map +1 -1
- package/dist/ConnectivityController-method-action-types.d.mts +1 -8
- package/dist/ConnectivityController-method-action-types.d.mts.map +1 -1
- package/dist/ConnectivityController-method-action-types.mjs.map +1 -1
- package/dist/ConnectivityController.cjs +1 -1
- package/dist/ConnectivityController.cjs.map +1 -1
- package/dist/ConnectivityController.mjs +1 -1
- package/dist/ConnectivityController.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -11,7 +11,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
11
11
|
|
|
12
12
|
- Add `init` method to asynchronously fetch and set the initial connectivity status from the adapter ([#7679](https://github.com/MetaMask/core/pull/7679))
|
|
13
13
|
- The controller now initializes with a default state (online) and requires calling `init()` to fetch the actual status
|
|
14
|
-
- This method can be called through the messenger action `ConnectivityController:init`
|
|
15
14
|
- Add `setConnectivityStatus` method to manually set connectivity status ([#7676](https://github.com/MetaMask/core/pull/7676))
|
|
16
15
|
- The method is exposed as a messenger action `ConnectivityController:setConnectivityStatus`
|
|
17
16
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConnectivityController-method-action-types.cjs","sourceRoot":"","sources":["../src/ConnectivityController-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { ConnectivityController } from './ConnectivityController';\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"ConnectivityController-method-action-types.cjs","sourceRoot":"","sources":["../src/ConnectivityController-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { ConnectivityController } from './ConnectivityController';\n\n/**\n * Sets the connectivity status.\n *\n * @param status - The connectivity status to set.\n */\nexport type ConnectivityControllerSetConnectivityStatusAction = {\n type: `ConnectivityController:setConnectivityStatus`;\n handler: ConnectivityController['setConnectivityStatus'];\n};\n\n/**\n * Union of all ConnectivityController action types.\n */\nexport type ConnectivityControllerMethodActions =\n ConnectivityControllerSetConnectivityStatusAction;\n"]}
|
|
@@ -3,13 +3,6 @@
|
|
|
3
3
|
* Do not edit manually.
|
|
4
4
|
*/
|
|
5
5
|
import type { ConnectivityController } from "./ConnectivityController.cjs";
|
|
6
|
-
/**
|
|
7
|
-
* Initializes the controller by fetching the initial connectivity status.
|
|
8
|
-
*/
|
|
9
|
-
export type ConnectivityControllerInitAction = {
|
|
10
|
-
type: `ConnectivityController:init`;
|
|
11
|
-
handler: ConnectivityController['init'];
|
|
12
|
-
};
|
|
13
6
|
/**
|
|
14
7
|
* Sets the connectivity status.
|
|
15
8
|
*
|
|
@@ -22,5 +15,5 @@ export type ConnectivityControllerSetConnectivityStatusAction = {
|
|
|
22
15
|
/**
|
|
23
16
|
* Union of all ConnectivityController action types.
|
|
24
17
|
*/
|
|
25
|
-
export type ConnectivityControllerMethodActions =
|
|
18
|
+
export type ConnectivityControllerMethodActions = ConnectivityControllerSetConnectivityStatusAction;
|
|
26
19
|
//# sourceMappingURL=ConnectivityController-method-action-types.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConnectivityController-method-action-types.d.cts","sourceRoot":"","sources":["../src/ConnectivityController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,qCAAiC;AAEvE
|
|
1
|
+
{"version":3,"file":"ConnectivityController-method-action-types.d.cts","sourceRoot":"","sources":["../src/ConnectivityController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,qCAAiC;AAEvE;;;;GAIG;AACH,MAAM,MAAM,iDAAiD,GAAG;IAC9D,IAAI,EAAE,8CAA8C,CAAC;IACrD,OAAO,EAAE,sBAAsB,CAAC,uBAAuB,CAAC,CAAC;CAC1D,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAC7C,iDAAiD,CAAC"}
|
|
@@ -3,13 +3,6 @@
|
|
|
3
3
|
* Do not edit manually.
|
|
4
4
|
*/
|
|
5
5
|
import type { ConnectivityController } from "./ConnectivityController.mjs";
|
|
6
|
-
/**
|
|
7
|
-
* Initializes the controller by fetching the initial connectivity status.
|
|
8
|
-
*/
|
|
9
|
-
export type ConnectivityControllerInitAction = {
|
|
10
|
-
type: `ConnectivityController:init`;
|
|
11
|
-
handler: ConnectivityController['init'];
|
|
12
|
-
};
|
|
13
6
|
/**
|
|
14
7
|
* Sets the connectivity status.
|
|
15
8
|
*
|
|
@@ -22,5 +15,5 @@ export type ConnectivityControllerSetConnectivityStatusAction = {
|
|
|
22
15
|
/**
|
|
23
16
|
* Union of all ConnectivityController action types.
|
|
24
17
|
*/
|
|
25
|
-
export type ConnectivityControllerMethodActions =
|
|
18
|
+
export type ConnectivityControllerMethodActions = ConnectivityControllerSetConnectivityStatusAction;
|
|
26
19
|
//# sourceMappingURL=ConnectivityController-method-action-types.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConnectivityController-method-action-types.d.mts","sourceRoot":"","sources":["../src/ConnectivityController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,qCAAiC;AAEvE
|
|
1
|
+
{"version":3,"file":"ConnectivityController-method-action-types.d.mts","sourceRoot":"","sources":["../src/ConnectivityController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,qCAAiC;AAEvE;;;;GAIG;AACH,MAAM,MAAM,iDAAiD,GAAG;IAC9D,IAAI,EAAE,8CAA8C,CAAC;IACrD,OAAO,EAAE,sBAAsB,CAAC,uBAAuB,CAAC,CAAC;CAC1D,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAC7C,iDAAiD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConnectivityController-method-action-types.mjs","sourceRoot":"","sources":["../src/ConnectivityController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { ConnectivityController } from './ConnectivityController';\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"ConnectivityController-method-action-types.mjs","sourceRoot":"","sources":["../src/ConnectivityController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { ConnectivityController } from './ConnectivityController';\n\n/**\n * Sets the connectivity status.\n *\n * @param status - The connectivity status to set.\n */\nexport type ConnectivityControllerSetConnectivityStatusAction = {\n type: `ConnectivityController:setConnectivityStatus`;\n handler: ConnectivityController['setConnectivityStatus'];\n};\n\n/**\n * Union of all ConnectivityController action types.\n */\nexport type ConnectivityControllerMethodActions =\n ConnectivityControllerSetConnectivityStatusAction;\n"]}
|
|
@@ -46,7 +46,7 @@ function getDefaultConnectivityControllerState() {
|
|
|
46
46
|
};
|
|
47
47
|
}
|
|
48
48
|
exports.getDefaultConnectivityControllerState = getDefaultConnectivityControllerState;
|
|
49
|
-
const MESSENGER_EXPOSED_METHODS = ['
|
|
49
|
+
const MESSENGER_EXPOSED_METHODS = ['setConnectivityStatus'];
|
|
50
50
|
/**
|
|
51
51
|
* ConnectivityController stores the device's internet connectivity status.
|
|
52
52
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConnectivityController.cjs","sourceRoot":"","sources":["../src/ConnectivityController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,+DAA2D;AAI3D,uCAAgD;AAGhD;;;;GAIG;AACU,QAAA,cAAc,GAAG,wBAAwB,CAAC;AAavD;;GAEG;AACH,MAAM,8BAA8B,GAAG;IACrC,kBAAkB,EAAE;QAClB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;CACmD,CAAC;AAEvD;;;;;;;GAOG;AACH,SAAgB,qCAAqC;IACnD,OAAO;QACL,kBAAkB,EAAE,6BAAqB,CAAC,MAAM;KACjD,CAAC;AACJ,CAAC;AAJD,sFAIC;AAED,MAAM,yBAAyB,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"ConnectivityController.cjs","sourceRoot":"","sources":["../src/ConnectivityController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,+DAA2D;AAI3D,uCAAgD;AAGhD;;;;GAIG;AACU,QAAA,cAAc,GAAG,wBAAwB,CAAC;AAavD;;GAEG;AACH,MAAM,8BAA8B,GAAG;IACrC,kBAAkB,EAAE;QAClB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;CACmD,CAAC;AAEvD;;;;;;;GAOG;AACH,SAAgB,qCAAqC;IACnD,OAAO;QACL,kBAAkB,EAAE,6BAAqB,CAAC,MAAM;KACjD,CAAC;AACJ,CAAC;AAJD,sFAIC;AAED,MAAM,yBAAyB,GAAG,CAAC,uBAAuB,CAAU,CAAC;AAmErE;;;;;;;;;;;;;GAaG;AACH,MAAa,sBAAuB,SAAQ,gCAI3C;IAGC;;;;;;OAMG;IACH,YAAY,EACV,SAAS,EACT,mBAAmB,GACW;QAC9B,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,8BAA8B;YACxC,IAAI,EAAE,sBAAc;YACpB,KAAK,EAAE,qCAAqC,EAAE;SAC/C,CAAC,CAAC;QAlBI,8DAA0C;QAoBjD,uBAAA,IAAI,+CAAwB,mBAAmB,MAAA,CAAC;QAEhD,uBAAA,IAAI,mDAAqB,CAAC,oBAAoB,CAC5C,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,CACtC,CAAC;QAEF,IAAI,CAAC,SAAS,CAAC,4BAA4B,CACzC,IAAI,EACJ,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAI;QACR,MAAM,aAAa,GAAG,MAAM,uBAAA,IAAI,mDAAqB,CAAC,SAAS,EAAE,CAAC;QAClE,IAAI,CAAC,qBAAqB,CAAC,aAAa,CAAC,CAAC;IAC5C,CAAC;IAED;;;;OAIG;IACH,qBAAqB,CAAC,MAA0B;QAC9C,IAAI,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE;YACzB,UAAU,CAAC,kBAAkB,GAAG,MAAM,CAAC;QACzC,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAvDD,wDAuDC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\n\nimport { ConnectivityControllerMethodActions } from './ConnectivityController-method-action-types';\nimport { CONNECTIVITY_STATUSES } from './types';\nimport type { ConnectivityAdapter, ConnectivityStatus } from './types';\n\n/**\n * The name of the {@link ConnectivityController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'ConnectivityController';\n\n/**\n * State for the {@link ConnectivityController}.\n */\nexport type ConnectivityControllerState = {\n /**\n * The current device connectivity status.\n * Named with 'connectivity' prefix to avoid conflicts when state is flattened in Redux.\n */\n connectivityStatus: ConnectivityStatus;\n};\n\n/**\n * The metadata for each property in {@link ConnectivityControllerState}.\n */\nconst connectivityControllerMetadata = {\n connectivityStatus: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n} satisfies StateMetadata<ConnectivityControllerState>;\n\n/**\n * Constructs the default {@link ConnectivityController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link ConnectivityController} state.\n */\nexport function getDefaultConnectivityControllerState(): ConnectivityControllerState {\n return {\n connectivityStatus: CONNECTIVITY_STATUSES.Online,\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = ['setConnectivityStatus'] as const;\n\n/**\n * Retrieves the state of the {@link ConnectivityController}.\n */\nexport type ConnectivityControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n ConnectivityControllerState\n>;\n\n/**\n * Actions that {@link ConnectivityControllerMessenger} exposes to other consumers.\n */\nexport type ConnectivityControllerActions =\n | ConnectivityControllerGetStateAction\n | ConnectivityControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link ConnectivityControllerMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Published when the state of {@link ConnectivityController} changes.\n */\nexport type ConnectivityControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n ConnectivityControllerState\n>;\n\n/**\n * Events that {@link ConnectivityControllerMessenger} exposes to other consumers.\n */\nexport type ConnectivityControllerEvents =\n ConnectivityControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link ConnectivityControllerMessenger} subscribes\n * to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link ConnectivityController}.\n */\nexport type ConnectivityControllerMessenger = Messenger<\n typeof controllerName,\n ConnectivityControllerActions | AllowedActions,\n ConnectivityControllerEvents | AllowedEvents\n>;\n\n/**\n * Options for constructing the {@link ConnectivityController}.\n */\nexport type ConnectivityControllerOptions = {\n /**\n * The messenger for inter-controller communication.\n */\n messenger: ConnectivityControllerMessenger;\n\n /**\n * Connectivity adapter for platform-specific detection.\n */\n connectivityAdapter: ConnectivityAdapter;\n};\n\n/**\n * ConnectivityController stores the device's internet connectivity status.\n *\n * This controller is platform-agnostic and designed to be used across different\n * MetaMask clients (extension, mobile). It requires a `ConnectivityAdapter` to\n * be injected, which provides platform-specific connectivity detection.\n *\n * The controller subscribes to the adapter's `onConnectivityChange` callback\n * and updates its state accordingly. All connectivity updates flow through\n * the adapter, ensuring a single source of truth.\n *\n * This controller provides a centralized state for connectivity status,\n * enabling the UI and other controllers to adapt when the user goes offline.\n */\nexport class ConnectivityController extends BaseController<\n typeof controllerName,\n ConnectivityControllerState,\n ConnectivityControllerMessenger\n> {\n readonly #connectivityAdapter: ConnectivityAdapter;\n\n /**\n * Constructs a new {@link ConnectivityController}.\n *\n * @param args - The arguments to this controller.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.connectivityAdapter - The connectivity adapter to use.\n */\n constructor({\n messenger,\n connectivityAdapter,\n }: ConnectivityControllerOptions) {\n super({\n messenger,\n metadata: connectivityControllerMetadata,\n name: controllerName,\n state: getDefaultConnectivityControllerState(),\n });\n\n this.#connectivityAdapter = connectivityAdapter;\n\n this.#connectivityAdapter.onConnectivityChange(\n this.setConnectivityStatus.bind(this),\n );\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Initializes the controller by fetching the initial connectivity status.\n */\n async init(): Promise<void> {\n const initialStatus = await this.#connectivityAdapter.getStatus();\n this.setConnectivityStatus(initialStatus);\n }\n\n /**\n * Sets the connectivity status.\n *\n * @param status - The connectivity status to set.\n */\n setConnectivityStatus(status: ConnectivityStatus): void {\n this.update((draftState) => {\n draftState.connectivityStatus = status;\n });\n }\n}\n"]}
|
|
@@ -42,7 +42,7 @@ export function getDefaultConnectivityControllerState() {
|
|
|
42
42
|
connectivityStatus: CONNECTIVITY_STATUSES.Online,
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
|
-
const MESSENGER_EXPOSED_METHODS = ['
|
|
45
|
+
const MESSENGER_EXPOSED_METHODS = ['setConnectivityStatus'];
|
|
46
46
|
/**
|
|
47
47
|
* ConnectivityController stores the device's internet connectivity status.
|
|
48
48
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConnectivityController.mjs","sourceRoot":"","sources":["../src/ConnectivityController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAI3D,OAAO,EAAE,qBAAqB,EAAE,oBAAgB;AAGhD;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,wBAAwB,CAAC;AAavD;;GAEG;AACH,MAAM,8BAA8B,GAAG;IACrC,kBAAkB,EAAE;QAClB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;CACmD,CAAC;AAEvD;;;;;;;GAOG;AACH,MAAM,UAAU,qCAAqC;IACnD,OAAO;QACL,kBAAkB,EAAE,qBAAqB,CAAC,MAAM;KACjD,CAAC;AACJ,CAAC;AAED,MAAM,yBAAyB,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"ConnectivityController.mjs","sourceRoot":"","sources":["../src/ConnectivityController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAI3D,OAAO,EAAE,qBAAqB,EAAE,oBAAgB;AAGhD;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,wBAAwB,CAAC;AAavD;;GAEG;AACH,MAAM,8BAA8B,GAAG;IACrC,kBAAkB,EAAE;QAClB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;CACmD,CAAC;AAEvD;;;;;;;GAOG;AACH,MAAM,UAAU,qCAAqC;IACnD,OAAO;QACL,kBAAkB,EAAE,qBAAqB,CAAC,MAAM;KACjD,CAAC;AACJ,CAAC;AAED,MAAM,yBAAyB,GAAG,CAAC,uBAAuB,CAAU,CAAC;AAmErE;;;;;;;;;;;;;GAaG;AACH,MAAM,OAAO,sBAAuB,SAAQ,cAI3C;IAGC;;;;;;OAMG;IACH,YAAY,EACV,SAAS,EACT,mBAAmB,GACW;QAC9B,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,8BAA8B;YACxC,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE,qCAAqC,EAAE;SAC/C,CAAC,CAAC;QAlBI,8DAA0C;QAoBjD,uBAAA,IAAI,+CAAwB,mBAAmB,MAAA,CAAC;QAEhD,uBAAA,IAAI,mDAAqB,CAAC,oBAAoB,CAC5C,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,CACtC,CAAC;QAEF,IAAI,CAAC,SAAS,CAAC,4BAA4B,CACzC,IAAI,EACJ,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAI;QACR,MAAM,aAAa,GAAG,MAAM,uBAAA,IAAI,mDAAqB,CAAC,SAAS,EAAE,CAAC;QAClE,IAAI,CAAC,qBAAqB,CAAC,aAAa,CAAC,CAAC;IAC5C,CAAC;IAED;;;;OAIG;IACH,qBAAqB,CAAC,MAA0B;QAC9C,IAAI,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE;YACzB,UAAU,CAAC,kBAAkB,GAAG,MAAM,CAAC;QACzC,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\n\nimport { ConnectivityControllerMethodActions } from './ConnectivityController-method-action-types';\nimport { CONNECTIVITY_STATUSES } from './types';\nimport type { ConnectivityAdapter, ConnectivityStatus } from './types';\n\n/**\n * The name of the {@link ConnectivityController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'ConnectivityController';\n\n/**\n * State for the {@link ConnectivityController}.\n */\nexport type ConnectivityControllerState = {\n /**\n * The current device connectivity status.\n * Named with 'connectivity' prefix to avoid conflicts when state is flattened in Redux.\n */\n connectivityStatus: ConnectivityStatus;\n};\n\n/**\n * The metadata for each property in {@link ConnectivityControllerState}.\n */\nconst connectivityControllerMetadata = {\n connectivityStatus: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n} satisfies StateMetadata<ConnectivityControllerState>;\n\n/**\n * Constructs the default {@link ConnectivityController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link ConnectivityController} state.\n */\nexport function getDefaultConnectivityControllerState(): ConnectivityControllerState {\n return {\n connectivityStatus: CONNECTIVITY_STATUSES.Online,\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = ['setConnectivityStatus'] as const;\n\n/**\n * Retrieves the state of the {@link ConnectivityController}.\n */\nexport type ConnectivityControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n ConnectivityControllerState\n>;\n\n/**\n * Actions that {@link ConnectivityControllerMessenger} exposes to other consumers.\n */\nexport type ConnectivityControllerActions =\n | ConnectivityControllerGetStateAction\n | ConnectivityControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link ConnectivityControllerMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Published when the state of {@link ConnectivityController} changes.\n */\nexport type ConnectivityControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n ConnectivityControllerState\n>;\n\n/**\n * Events that {@link ConnectivityControllerMessenger} exposes to other consumers.\n */\nexport type ConnectivityControllerEvents =\n ConnectivityControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link ConnectivityControllerMessenger} subscribes\n * to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link ConnectivityController}.\n */\nexport type ConnectivityControllerMessenger = Messenger<\n typeof controllerName,\n ConnectivityControllerActions | AllowedActions,\n ConnectivityControllerEvents | AllowedEvents\n>;\n\n/**\n * Options for constructing the {@link ConnectivityController}.\n */\nexport type ConnectivityControllerOptions = {\n /**\n * The messenger for inter-controller communication.\n */\n messenger: ConnectivityControllerMessenger;\n\n /**\n * Connectivity adapter for platform-specific detection.\n */\n connectivityAdapter: ConnectivityAdapter;\n};\n\n/**\n * ConnectivityController stores the device's internet connectivity status.\n *\n * This controller is platform-agnostic and designed to be used across different\n * MetaMask clients (extension, mobile). It requires a `ConnectivityAdapter` to\n * be injected, which provides platform-specific connectivity detection.\n *\n * The controller subscribes to the adapter's `onConnectivityChange` callback\n * and updates its state accordingly. All connectivity updates flow through\n * the adapter, ensuring a single source of truth.\n *\n * This controller provides a centralized state for connectivity status,\n * enabling the UI and other controllers to adapt when the user goes offline.\n */\nexport class ConnectivityController extends BaseController<\n typeof controllerName,\n ConnectivityControllerState,\n ConnectivityControllerMessenger\n> {\n readonly #connectivityAdapter: ConnectivityAdapter;\n\n /**\n * Constructs a new {@link ConnectivityController}.\n *\n * @param args - The arguments to this controller.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.connectivityAdapter - The connectivity adapter to use.\n */\n constructor({\n messenger,\n connectivityAdapter,\n }: ConnectivityControllerOptions) {\n super({\n messenger,\n metadata: connectivityControllerMetadata,\n name: controllerName,\n state: getDefaultConnectivityControllerState(),\n });\n\n this.#connectivityAdapter = connectivityAdapter;\n\n this.#connectivityAdapter.onConnectivityChange(\n this.setConnectivityStatus.bind(this),\n );\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Initializes the controller by fetching the initial connectivity status.\n */\n async init(): Promise<void> {\n const initialStatus = await this.#connectivityAdapter.getStatus();\n this.setConnectivityStatus(initialStatus);\n }\n\n /**\n * Sets the connectivity status.\n *\n * @param status - The connectivity status to set.\n */\n setConnectivityStatus(status: ConnectivityStatus): void {\n this.update((draftState) => {\n draftState.connectivityStatus = status;\n });\n }\n}\n"]}
|
package/package.json
CHANGED