@metamask-previews/connectivity-controller 0.0.0-preview-9e00b40b
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 +14 -0
- package/LICENSE +20 -0
- package/README.md +15 -0
- package/dist/ConnectivityController.cjs +86 -0
- package/dist/ConnectivityController.cjs.map +1 -0
- package/dist/ConnectivityController.d.cts +116 -0
- package/dist/ConnectivityController.d.cts.map +1 -0
- package/dist/ConnectivityController.d.mts +116 -0
- package/dist/ConnectivityController.d.mts.map +1 -0
- package/dist/ConnectivityController.mjs +81 -0
- package/dist/ConnectivityController.mjs.map +1 -0
- package/dist/index.cjs +9 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +3 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types.cjs +12 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +39 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +39 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +9 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +72 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Initial release ([#7623](https://github.com/MetaMask/core/pull/7623))
|
|
13
|
+
|
|
14
|
+
[Unreleased]: https://github.com/MetaMask/core/
|
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MetaMask
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# `@metamask/connectivity-controller`
|
|
2
|
+
|
|
3
|
+
ConnectivityController stores the device's internet connectivity status.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
`yarn add @metamask/connectivity-controller`
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
`npm install @metamask/connectivity-controller`
|
|
12
|
+
|
|
13
|
+
## Contributing
|
|
14
|
+
|
|
15
|
+
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConnectivityController = exports.getDefaultConnectivityControllerState = exports.controllerName = void 0;
|
|
4
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
5
|
+
const types_1 = require("./types.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* The name of the {@link ConnectivityController}, used to namespace the
|
|
8
|
+
* controller's actions and events and to namespace the controller's state data
|
|
9
|
+
* when composed with other controllers.
|
|
10
|
+
*/
|
|
11
|
+
exports.controllerName = 'ConnectivityController';
|
|
12
|
+
/**
|
|
13
|
+
* The metadata for each property in {@link ConnectivityControllerState}.
|
|
14
|
+
*/
|
|
15
|
+
const connectivityControllerMetadata = {
|
|
16
|
+
connectivityStatus: {
|
|
17
|
+
persist: false,
|
|
18
|
+
includeInDebugSnapshot: true,
|
|
19
|
+
includeInStateLogs: true,
|
|
20
|
+
usedInUi: true,
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Constructs the default {@link ConnectivityController} state. This allows
|
|
25
|
+
* consumers to provide a partial state object when initializing the controller
|
|
26
|
+
* and also helps in constructing complete state objects for this controller in
|
|
27
|
+
* tests.
|
|
28
|
+
*
|
|
29
|
+
* @returns The default {@link ConnectivityController} state.
|
|
30
|
+
*/
|
|
31
|
+
function getDefaultConnectivityControllerState() {
|
|
32
|
+
return {
|
|
33
|
+
connectivityStatus: types_1.ConnectivityStatus.Online,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
exports.getDefaultConnectivityControllerState = getDefaultConnectivityControllerState;
|
|
37
|
+
/**
|
|
38
|
+
* ConnectivityController stores the device's internet connectivity status.
|
|
39
|
+
*
|
|
40
|
+
* This controller is platform-agnostic and designed to be used across different
|
|
41
|
+
* MetaMask clients (extension, mobile). It requires a `ConnectivityService` to
|
|
42
|
+
* be injected, which provides platform-specific connectivity detection.
|
|
43
|
+
*
|
|
44
|
+
* The controller subscribes to the service's `onConnectivityChange` callback
|
|
45
|
+
* and updates its state accordingly. All connectivity updates flow through
|
|
46
|
+
* the service, ensuring a single source of truth.
|
|
47
|
+
*
|
|
48
|
+
* **Platform implementations:**
|
|
49
|
+
*
|
|
50
|
+
* - **Mobile:** Inject `NetInfoConnectivityService` using `@react-native-community/netinfo`
|
|
51
|
+
* - **Extension:** Inject `PassiveConnectivityService` in the background.
|
|
52
|
+
* Status is updated via the `setDeviceConnectivityStatus` API, which is called from:
|
|
53
|
+
* - MV3: Offscreen document (where browser events work reliably)
|
|
54
|
+
* - MV2: Background page (where browser events work directly)
|
|
55
|
+
*
|
|
56
|
+
* This controller provides a centralized state for connectivity status,
|
|
57
|
+
* enabling the UI and other controllers to adapt when the user goes offline.
|
|
58
|
+
*/
|
|
59
|
+
class ConnectivityController extends base_controller_1.BaseController {
|
|
60
|
+
/**
|
|
61
|
+
* Constructs a new {@link ConnectivityController}.
|
|
62
|
+
*
|
|
63
|
+
* @param args - The arguments to this controller.
|
|
64
|
+
* @param args.messenger - The messenger suited for this controller.
|
|
65
|
+
* @param args.connectivityService - The connectivity service to use.
|
|
66
|
+
*/
|
|
67
|
+
constructor({ messenger, connectivityService, }) {
|
|
68
|
+
const initialStatus = connectivityService.getStatus();
|
|
69
|
+
super({
|
|
70
|
+
messenger,
|
|
71
|
+
metadata: connectivityControllerMetadata,
|
|
72
|
+
name: exports.controllerName,
|
|
73
|
+
state: {
|
|
74
|
+
...getDefaultConnectivityControllerState(),
|
|
75
|
+
connectivityStatus: initialStatus,
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
connectivityService.onConnectivityChange((status) => {
|
|
79
|
+
this.update((draftState) => {
|
|
80
|
+
draftState.connectivityStatus = status;
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.ConnectivityController = ConnectivityController;
|
|
86
|
+
//# sourceMappingURL=ConnectivityController.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConnectivityController.cjs","sourceRoot":"","sources":["../src/ConnectivityController.ts"],"names":[],"mappings":";;;AAKA,+DAA2D;AAG3D,uCAA6C;AAG7C;;;;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,0BAAkB,CAAC,MAAM;KAC9C,CAAC;AACJ,CAAC;AAJD,sFAIC;AA2ED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAa,sBAAuB,SAAQ,gCAI3C;IACC;;;;;;OAMG;IACH,YAAY,EACV,SAAS,EACT,mBAAmB,GACW;QAC9B,MAAM,aAAa,GAAG,mBAAmB,CAAC,SAAS,EAAE,CAAC;QAEtD,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,8BAA8B;YACxC,IAAI,EAAE,sBAAc;YACpB,KAAK,EAAE;gBACL,GAAG,qCAAqC,EAAE;gBAC1C,kBAAkB,EAAE,aAAa;aAClC;SACF,CAAC,CAAC;QAEH,mBAAmB,CAAC,oBAAoB,CAAC,CAAC,MAAM,EAAE,EAAE;YAClD,IAAI,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE;gBACzB,UAAU,CAAC,kBAAkB,GAAG,MAAM,CAAC;YACzC,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAlCD,wDAkCC","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 { ConnectivityStatus } from './types';\nimport type { ConnectivityService } 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: ConnectivityStatus.Online,\n };\n}\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\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 service for platform-specific detection.\n *\n * The controller subscribes to the service's `onConnectivityChange`\n * callback to receive connectivity updates.\n *\n * Platform implementations:\n * - Mobile: Use `NetInfoConnectivityService` with `@react-native-community/netinfo`\n * - Extension (same context): Use `BrowserConnectivityService`\n * - Extension (cross-context): Use `PassiveConnectivityService` and call\n * `setStatus()` from the UI context\n */\n connectivityService: ConnectivityService;\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 `ConnectivityService` to\n * be injected, which provides platform-specific connectivity detection.\n *\n * The controller subscribes to the service's `onConnectivityChange` callback\n * and updates its state accordingly. All connectivity updates flow through\n * the service, ensuring a single source of truth.\n *\n * **Platform implementations:**\n *\n * - **Mobile:** Inject `NetInfoConnectivityService` using `@react-native-community/netinfo`\n * - **Extension:** Inject `PassiveConnectivityService` in the background.\n * Status is updated via the `setDeviceConnectivityStatus` API, which is called from:\n * - MV3: Offscreen document (where browser events work reliably)\n * - MV2: Background page (where browser events work directly)\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 /**\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.connectivityService - The connectivity service to use.\n */\n constructor({\n messenger,\n connectivityService,\n }: ConnectivityControllerOptions) {\n const initialStatus = connectivityService.getStatus();\n\n super({\n messenger,\n metadata: connectivityControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultConnectivityControllerState(),\n connectivityStatus: initialStatus,\n },\n });\n\n connectivityService.onConnectivityChange((status) => {\n this.update((draftState) => {\n draftState.connectivityStatus = status;\n });\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
3
|
+
import type { Messenger } from "@metamask/messenger";
|
|
4
|
+
import { ConnectivityStatus } from "./types.cjs";
|
|
5
|
+
import type { ConnectivityService } from "./types.cjs";
|
|
6
|
+
/**
|
|
7
|
+
* The name of the {@link ConnectivityController}, used to namespace the
|
|
8
|
+
* controller's actions and events and to namespace the controller's state data
|
|
9
|
+
* when composed with other controllers.
|
|
10
|
+
*/
|
|
11
|
+
export declare const controllerName = "ConnectivityController";
|
|
12
|
+
/**
|
|
13
|
+
* State for the {@link ConnectivityController}.
|
|
14
|
+
*/
|
|
15
|
+
export type ConnectivityControllerState = {
|
|
16
|
+
/**
|
|
17
|
+
* The current device connectivity status.
|
|
18
|
+
* Named with 'connectivity' prefix to avoid conflicts when state is flattened in Redux.
|
|
19
|
+
*/
|
|
20
|
+
connectivityStatus: ConnectivityStatus;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Constructs the default {@link ConnectivityController} state. This allows
|
|
24
|
+
* consumers to provide a partial state object when initializing the controller
|
|
25
|
+
* and also helps in constructing complete state objects for this controller in
|
|
26
|
+
* tests.
|
|
27
|
+
*
|
|
28
|
+
* @returns The default {@link ConnectivityController} state.
|
|
29
|
+
*/
|
|
30
|
+
export declare function getDefaultConnectivityControllerState(): ConnectivityControllerState;
|
|
31
|
+
/**
|
|
32
|
+
* Retrieves the state of the {@link ConnectivityController}.
|
|
33
|
+
*/
|
|
34
|
+
export type ConnectivityControllerGetStateAction = ControllerGetStateAction<typeof controllerName, ConnectivityControllerState>;
|
|
35
|
+
/**
|
|
36
|
+
* Actions that {@link ConnectivityControllerMessenger} exposes to other consumers.
|
|
37
|
+
*/
|
|
38
|
+
export type ConnectivityControllerActions = ConnectivityControllerGetStateAction;
|
|
39
|
+
/**
|
|
40
|
+
* Actions from other messengers that {@link ConnectivityControllerMessenger} calls.
|
|
41
|
+
*/
|
|
42
|
+
type AllowedActions = never;
|
|
43
|
+
/**
|
|
44
|
+
* Published when the state of {@link ConnectivityController} changes.
|
|
45
|
+
*/
|
|
46
|
+
export type ConnectivityControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, ConnectivityControllerState>;
|
|
47
|
+
/**
|
|
48
|
+
* Events that {@link ConnectivityControllerMessenger} exposes to other consumers.
|
|
49
|
+
*/
|
|
50
|
+
export type ConnectivityControllerEvents = ConnectivityControllerStateChangeEvent;
|
|
51
|
+
/**
|
|
52
|
+
* Events from other messengers that {@link ConnectivityControllerMessenger} subscribes
|
|
53
|
+
* to.
|
|
54
|
+
*/
|
|
55
|
+
type AllowedEvents = never;
|
|
56
|
+
/**
|
|
57
|
+
* The messenger restricted to actions and events accessed by
|
|
58
|
+
* {@link ConnectivityController}.
|
|
59
|
+
*/
|
|
60
|
+
export type ConnectivityControllerMessenger = Messenger<typeof controllerName, ConnectivityControllerActions | AllowedActions, ConnectivityControllerEvents | AllowedEvents>;
|
|
61
|
+
/**
|
|
62
|
+
* Options for constructing the {@link ConnectivityController}.
|
|
63
|
+
*/
|
|
64
|
+
export type ConnectivityControllerOptions = {
|
|
65
|
+
/**
|
|
66
|
+
* The messenger for inter-controller communication.
|
|
67
|
+
*/
|
|
68
|
+
messenger: ConnectivityControllerMessenger;
|
|
69
|
+
/**
|
|
70
|
+
* Connectivity service for platform-specific detection.
|
|
71
|
+
*
|
|
72
|
+
* The controller subscribes to the service's `onConnectivityChange`
|
|
73
|
+
* callback to receive connectivity updates.
|
|
74
|
+
*
|
|
75
|
+
* Platform implementations:
|
|
76
|
+
* - Mobile: Use `NetInfoConnectivityService` with `@react-native-community/netinfo`
|
|
77
|
+
* - Extension (same context): Use `BrowserConnectivityService`
|
|
78
|
+
* - Extension (cross-context): Use `PassiveConnectivityService` and call
|
|
79
|
+
* `setStatus()` from the UI context
|
|
80
|
+
*/
|
|
81
|
+
connectivityService: ConnectivityService;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* ConnectivityController stores the device's internet connectivity status.
|
|
85
|
+
*
|
|
86
|
+
* This controller is platform-agnostic and designed to be used across different
|
|
87
|
+
* MetaMask clients (extension, mobile). It requires a `ConnectivityService` to
|
|
88
|
+
* be injected, which provides platform-specific connectivity detection.
|
|
89
|
+
*
|
|
90
|
+
* The controller subscribes to the service's `onConnectivityChange` callback
|
|
91
|
+
* and updates its state accordingly. All connectivity updates flow through
|
|
92
|
+
* the service, ensuring a single source of truth.
|
|
93
|
+
*
|
|
94
|
+
* **Platform implementations:**
|
|
95
|
+
*
|
|
96
|
+
* - **Mobile:** Inject `NetInfoConnectivityService` using `@react-native-community/netinfo`
|
|
97
|
+
* - **Extension:** Inject `PassiveConnectivityService` in the background.
|
|
98
|
+
* Status is updated via the `setDeviceConnectivityStatus` API, which is called from:
|
|
99
|
+
* - MV3: Offscreen document (where browser events work reliably)
|
|
100
|
+
* - MV2: Background page (where browser events work directly)
|
|
101
|
+
*
|
|
102
|
+
* This controller provides a centralized state for connectivity status,
|
|
103
|
+
* enabling the UI and other controllers to adapt when the user goes offline.
|
|
104
|
+
*/
|
|
105
|
+
export declare class ConnectivityController extends BaseController<typeof controllerName, ConnectivityControllerState, ConnectivityControllerMessenger> {
|
|
106
|
+
/**
|
|
107
|
+
* Constructs a new {@link ConnectivityController}.
|
|
108
|
+
*
|
|
109
|
+
* @param args - The arguments to this controller.
|
|
110
|
+
* @param args.messenger - The messenger suited for this controller.
|
|
111
|
+
* @param args.connectivityService - The connectivity service to use.
|
|
112
|
+
*/
|
|
113
|
+
constructor({ messenger, connectivityService, }: ConnectivityControllerOptions);
|
|
114
|
+
}
|
|
115
|
+
export {};
|
|
116
|
+
//# sourceMappingURL=ConnectivityController.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConnectivityController.d.cts","sourceRoot":"","sources":["../src/ConnectivityController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,EAAE,kBAAkB,EAAE,oBAAgB;AAC7C,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAgB;AAEnD;;;;GAIG;AACH,eAAO,MAAM,cAAc,2BAA2B,CAAC;AAEvD;;GAEG;AACH,MAAM,MAAM,2BAA2B,GAAG;IACxC;;;OAGG;IACH,kBAAkB,EAAE,kBAAkB,CAAC;CACxC,CAAC;AAcF;;;;;;;GAOG;AACH,wBAAgB,qCAAqC,IAAI,2BAA2B,CAInF;AAED;;GAEG;AACH,MAAM,MAAM,oCAAoC,GAAG,wBAAwB,CACzE,OAAO,cAAc,EACrB,2BAA2B,CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,6BAA6B,GACvC,oCAAoC,CAAC;AAEvC;;GAEG;AACH,KAAK,cAAc,GAAG,KAAK,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,sCAAsC,GAAG,0BAA0B,CAC7E,OAAO,cAAc,EACrB,2BAA2B,CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,4BAA4B,GACtC,sCAAsC,CAAC;AAEzC;;;GAGG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,+BAA+B,GAAG,SAAS,CACrD,OAAO,cAAc,EACrB,6BAA6B,GAAG,cAAc,EAC9C,4BAA4B,GAAG,aAAa,CAC7C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;OAEG;IACH,SAAS,EAAE,+BAA+B,CAAC;IAE3C;;;;;;;;;;;OAWG;IACH,mBAAmB,EAAE,mBAAmB,CAAC;CAC1C,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,sBAAuB,SAAQ,cAAc,CACxD,OAAO,cAAc,EACrB,2BAA2B,EAC3B,+BAA+B,CAChC;IACC;;;;;;OAMG;gBACS,EACV,SAAS,EACT,mBAAmB,GACpB,EAAE,6BAA6B;CAmBjC"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
3
|
+
import type { Messenger } from "@metamask/messenger";
|
|
4
|
+
import { ConnectivityStatus } from "./types.mjs";
|
|
5
|
+
import type { ConnectivityService } from "./types.mjs";
|
|
6
|
+
/**
|
|
7
|
+
* The name of the {@link ConnectivityController}, used to namespace the
|
|
8
|
+
* controller's actions and events and to namespace the controller's state data
|
|
9
|
+
* when composed with other controllers.
|
|
10
|
+
*/
|
|
11
|
+
export declare const controllerName = "ConnectivityController";
|
|
12
|
+
/**
|
|
13
|
+
* State for the {@link ConnectivityController}.
|
|
14
|
+
*/
|
|
15
|
+
export type ConnectivityControllerState = {
|
|
16
|
+
/**
|
|
17
|
+
* The current device connectivity status.
|
|
18
|
+
* Named with 'connectivity' prefix to avoid conflicts when state is flattened in Redux.
|
|
19
|
+
*/
|
|
20
|
+
connectivityStatus: ConnectivityStatus;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Constructs the default {@link ConnectivityController} state. This allows
|
|
24
|
+
* consumers to provide a partial state object when initializing the controller
|
|
25
|
+
* and also helps in constructing complete state objects for this controller in
|
|
26
|
+
* tests.
|
|
27
|
+
*
|
|
28
|
+
* @returns The default {@link ConnectivityController} state.
|
|
29
|
+
*/
|
|
30
|
+
export declare function getDefaultConnectivityControllerState(): ConnectivityControllerState;
|
|
31
|
+
/**
|
|
32
|
+
* Retrieves the state of the {@link ConnectivityController}.
|
|
33
|
+
*/
|
|
34
|
+
export type ConnectivityControllerGetStateAction = ControllerGetStateAction<typeof controllerName, ConnectivityControllerState>;
|
|
35
|
+
/**
|
|
36
|
+
* Actions that {@link ConnectivityControllerMessenger} exposes to other consumers.
|
|
37
|
+
*/
|
|
38
|
+
export type ConnectivityControllerActions = ConnectivityControllerGetStateAction;
|
|
39
|
+
/**
|
|
40
|
+
* Actions from other messengers that {@link ConnectivityControllerMessenger} calls.
|
|
41
|
+
*/
|
|
42
|
+
type AllowedActions = never;
|
|
43
|
+
/**
|
|
44
|
+
* Published when the state of {@link ConnectivityController} changes.
|
|
45
|
+
*/
|
|
46
|
+
export type ConnectivityControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, ConnectivityControllerState>;
|
|
47
|
+
/**
|
|
48
|
+
* Events that {@link ConnectivityControllerMessenger} exposes to other consumers.
|
|
49
|
+
*/
|
|
50
|
+
export type ConnectivityControllerEvents = ConnectivityControllerStateChangeEvent;
|
|
51
|
+
/**
|
|
52
|
+
* Events from other messengers that {@link ConnectivityControllerMessenger} subscribes
|
|
53
|
+
* to.
|
|
54
|
+
*/
|
|
55
|
+
type AllowedEvents = never;
|
|
56
|
+
/**
|
|
57
|
+
* The messenger restricted to actions and events accessed by
|
|
58
|
+
* {@link ConnectivityController}.
|
|
59
|
+
*/
|
|
60
|
+
export type ConnectivityControllerMessenger = Messenger<typeof controllerName, ConnectivityControllerActions | AllowedActions, ConnectivityControllerEvents | AllowedEvents>;
|
|
61
|
+
/**
|
|
62
|
+
* Options for constructing the {@link ConnectivityController}.
|
|
63
|
+
*/
|
|
64
|
+
export type ConnectivityControllerOptions = {
|
|
65
|
+
/**
|
|
66
|
+
* The messenger for inter-controller communication.
|
|
67
|
+
*/
|
|
68
|
+
messenger: ConnectivityControllerMessenger;
|
|
69
|
+
/**
|
|
70
|
+
* Connectivity service for platform-specific detection.
|
|
71
|
+
*
|
|
72
|
+
* The controller subscribes to the service's `onConnectivityChange`
|
|
73
|
+
* callback to receive connectivity updates.
|
|
74
|
+
*
|
|
75
|
+
* Platform implementations:
|
|
76
|
+
* - Mobile: Use `NetInfoConnectivityService` with `@react-native-community/netinfo`
|
|
77
|
+
* - Extension (same context): Use `BrowserConnectivityService`
|
|
78
|
+
* - Extension (cross-context): Use `PassiveConnectivityService` and call
|
|
79
|
+
* `setStatus()` from the UI context
|
|
80
|
+
*/
|
|
81
|
+
connectivityService: ConnectivityService;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* ConnectivityController stores the device's internet connectivity status.
|
|
85
|
+
*
|
|
86
|
+
* This controller is platform-agnostic and designed to be used across different
|
|
87
|
+
* MetaMask clients (extension, mobile). It requires a `ConnectivityService` to
|
|
88
|
+
* be injected, which provides platform-specific connectivity detection.
|
|
89
|
+
*
|
|
90
|
+
* The controller subscribes to the service's `onConnectivityChange` callback
|
|
91
|
+
* and updates its state accordingly. All connectivity updates flow through
|
|
92
|
+
* the service, ensuring a single source of truth.
|
|
93
|
+
*
|
|
94
|
+
* **Platform implementations:**
|
|
95
|
+
*
|
|
96
|
+
* - **Mobile:** Inject `NetInfoConnectivityService` using `@react-native-community/netinfo`
|
|
97
|
+
* - **Extension:** Inject `PassiveConnectivityService` in the background.
|
|
98
|
+
* Status is updated via the `setDeviceConnectivityStatus` API, which is called from:
|
|
99
|
+
* - MV3: Offscreen document (where browser events work reliably)
|
|
100
|
+
* - MV2: Background page (where browser events work directly)
|
|
101
|
+
*
|
|
102
|
+
* This controller provides a centralized state for connectivity status,
|
|
103
|
+
* enabling the UI and other controllers to adapt when the user goes offline.
|
|
104
|
+
*/
|
|
105
|
+
export declare class ConnectivityController extends BaseController<typeof controllerName, ConnectivityControllerState, ConnectivityControllerMessenger> {
|
|
106
|
+
/**
|
|
107
|
+
* Constructs a new {@link ConnectivityController}.
|
|
108
|
+
*
|
|
109
|
+
* @param args - The arguments to this controller.
|
|
110
|
+
* @param args.messenger - The messenger suited for this controller.
|
|
111
|
+
* @param args.connectivityService - The connectivity service to use.
|
|
112
|
+
*/
|
|
113
|
+
constructor({ messenger, connectivityService, }: ConnectivityControllerOptions);
|
|
114
|
+
}
|
|
115
|
+
export {};
|
|
116
|
+
//# sourceMappingURL=ConnectivityController.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConnectivityController.d.mts","sourceRoot":"","sources":["../src/ConnectivityController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,EAAE,kBAAkB,EAAE,oBAAgB;AAC7C,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAgB;AAEnD;;;;GAIG;AACH,eAAO,MAAM,cAAc,2BAA2B,CAAC;AAEvD;;GAEG;AACH,MAAM,MAAM,2BAA2B,GAAG;IACxC;;;OAGG;IACH,kBAAkB,EAAE,kBAAkB,CAAC;CACxC,CAAC;AAcF;;;;;;;GAOG;AACH,wBAAgB,qCAAqC,IAAI,2BAA2B,CAInF;AAED;;GAEG;AACH,MAAM,MAAM,oCAAoC,GAAG,wBAAwB,CACzE,OAAO,cAAc,EACrB,2BAA2B,CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,6BAA6B,GACvC,oCAAoC,CAAC;AAEvC;;GAEG;AACH,KAAK,cAAc,GAAG,KAAK,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,sCAAsC,GAAG,0BAA0B,CAC7E,OAAO,cAAc,EACrB,2BAA2B,CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,4BAA4B,GACtC,sCAAsC,CAAC;AAEzC;;;GAGG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,+BAA+B,GAAG,SAAS,CACrD,OAAO,cAAc,EACrB,6BAA6B,GAAG,cAAc,EAC9C,4BAA4B,GAAG,aAAa,CAC7C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;OAEG;IACH,SAAS,EAAE,+BAA+B,CAAC;IAE3C;;;;;;;;;;;OAWG;IACH,mBAAmB,EAAE,mBAAmB,CAAC;CAC1C,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,sBAAuB,SAAQ,cAAc,CACxD,OAAO,cAAc,EACrB,2BAA2B,EAC3B,+BAA+B,CAChC;IACC;;;;;;OAMG;gBACS,EACV,SAAS,EACT,mBAAmB,GACpB,EAAE,6BAA6B;CAmBjC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { BaseController } from "@metamask/base-controller";
|
|
2
|
+
import { ConnectivityStatus } from "./types.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* The name of the {@link ConnectivityController}, used to namespace the
|
|
5
|
+
* controller's actions and events and to namespace the controller's state data
|
|
6
|
+
* when composed with other controllers.
|
|
7
|
+
*/
|
|
8
|
+
export const controllerName = 'ConnectivityController';
|
|
9
|
+
/**
|
|
10
|
+
* The metadata for each property in {@link ConnectivityControllerState}.
|
|
11
|
+
*/
|
|
12
|
+
const connectivityControllerMetadata = {
|
|
13
|
+
connectivityStatus: {
|
|
14
|
+
persist: false,
|
|
15
|
+
includeInDebugSnapshot: true,
|
|
16
|
+
includeInStateLogs: true,
|
|
17
|
+
usedInUi: true,
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Constructs the default {@link ConnectivityController} state. This allows
|
|
22
|
+
* consumers to provide a partial state object when initializing the controller
|
|
23
|
+
* and also helps in constructing complete state objects for this controller in
|
|
24
|
+
* tests.
|
|
25
|
+
*
|
|
26
|
+
* @returns The default {@link ConnectivityController} state.
|
|
27
|
+
*/
|
|
28
|
+
export function getDefaultConnectivityControllerState() {
|
|
29
|
+
return {
|
|
30
|
+
connectivityStatus: ConnectivityStatus.Online,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* ConnectivityController stores the device's internet connectivity status.
|
|
35
|
+
*
|
|
36
|
+
* This controller is platform-agnostic and designed to be used across different
|
|
37
|
+
* MetaMask clients (extension, mobile). It requires a `ConnectivityService` to
|
|
38
|
+
* be injected, which provides platform-specific connectivity detection.
|
|
39
|
+
*
|
|
40
|
+
* The controller subscribes to the service's `onConnectivityChange` callback
|
|
41
|
+
* and updates its state accordingly. All connectivity updates flow through
|
|
42
|
+
* the service, ensuring a single source of truth.
|
|
43
|
+
*
|
|
44
|
+
* **Platform implementations:**
|
|
45
|
+
*
|
|
46
|
+
* - **Mobile:** Inject `NetInfoConnectivityService` using `@react-native-community/netinfo`
|
|
47
|
+
* - **Extension:** Inject `PassiveConnectivityService` in the background.
|
|
48
|
+
* Status is updated via the `setDeviceConnectivityStatus` API, which is called from:
|
|
49
|
+
* - MV3: Offscreen document (where browser events work reliably)
|
|
50
|
+
* - MV2: Background page (where browser events work directly)
|
|
51
|
+
*
|
|
52
|
+
* This controller provides a centralized state for connectivity status,
|
|
53
|
+
* enabling the UI and other controllers to adapt when the user goes offline.
|
|
54
|
+
*/
|
|
55
|
+
export class ConnectivityController extends BaseController {
|
|
56
|
+
/**
|
|
57
|
+
* Constructs a new {@link ConnectivityController}.
|
|
58
|
+
*
|
|
59
|
+
* @param args - The arguments to this controller.
|
|
60
|
+
* @param args.messenger - The messenger suited for this controller.
|
|
61
|
+
* @param args.connectivityService - The connectivity service to use.
|
|
62
|
+
*/
|
|
63
|
+
constructor({ messenger, connectivityService, }) {
|
|
64
|
+
const initialStatus = connectivityService.getStatus();
|
|
65
|
+
super({
|
|
66
|
+
messenger,
|
|
67
|
+
metadata: connectivityControllerMetadata,
|
|
68
|
+
name: controllerName,
|
|
69
|
+
state: {
|
|
70
|
+
...getDefaultConnectivityControllerState(),
|
|
71
|
+
connectivityStatus: initialStatus,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
connectivityService.onConnectivityChange((status) => {
|
|
75
|
+
this.update((draftState) => {
|
|
76
|
+
draftState.connectivityStatus = status;
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=ConnectivityController.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConnectivityController.mjs","sourceRoot":"","sources":["../src/ConnectivityController.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAG3D,OAAO,EAAE,kBAAkB,EAAE,oBAAgB;AAG7C;;;;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,kBAAkB,CAAC,MAAM;KAC9C,CAAC;AACJ,CAAC;AA2ED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,OAAO,sBAAuB,SAAQ,cAI3C;IACC;;;;;;OAMG;IACH,YAAY,EACV,SAAS,EACT,mBAAmB,GACW;QAC9B,MAAM,aAAa,GAAG,mBAAmB,CAAC,SAAS,EAAE,CAAC;QAEtD,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,8BAA8B;YACxC,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE;gBACL,GAAG,qCAAqC,EAAE;gBAC1C,kBAAkB,EAAE,aAAa;aAClC;SACF,CAAC,CAAC;QAEH,mBAAmB,CAAC,oBAAoB,CAAC,CAAC,MAAM,EAAE,EAAE;YAClD,IAAI,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE;gBACzB,UAAU,CAAC,kBAAkB,GAAG,MAAM,CAAC;YACzC,CAAC,CAAC,CAAC;QACL,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 { ConnectivityStatus } from './types';\nimport type { ConnectivityService } 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: ConnectivityStatus.Online,\n };\n}\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\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 service for platform-specific detection.\n *\n * The controller subscribes to the service's `onConnectivityChange`\n * callback to receive connectivity updates.\n *\n * Platform implementations:\n * - Mobile: Use `NetInfoConnectivityService` with `@react-native-community/netinfo`\n * - Extension (same context): Use `BrowserConnectivityService`\n * - Extension (cross-context): Use `PassiveConnectivityService` and call\n * `setStatus()` from the UI context\n */\n connectivityService: ConnectivityService;\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 `ConnectivityService` to\n * be injected, which provides platform-specific connectivity detection.\n *\n * The controller subscribes to the service's `onConnectivityChange` callback\n * and updates its state accordingly. All connectivity updates flow through\n * the service, ensuring a single source of truth.\n *\n * **Platform implementations:**\n *\n * - **Mobile:** Inject `NetInfoConnectivityService` using `@react-native-community/netinfo`\n * - **Extension:** Inject `PassiveConnectivityService` in the background.\n * Status is updated via the `setDeviceConnectivityStatus` API, which is called from:\n * - MV3: Offscreen document (where browser events work reliably)\n * - MV2: Background page (where browser events work directly)\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 /**\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.connectivityService - The connectivity service to use.\n */\n constructor({\n messenger,\n connectivityService,\n }: ConnectivityControllerOptions) {\n const initialStatus = connectivityService.getStatus();\n\n super({\n messenger,\n metadata: connectivityControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultConnectivityControllerState(),\n connectivityStatus: initialStatus,\n },\n });\n\n connectivityService.onConnectivityChange((status) => {\n this.update((draftState) => {\n draftState.connectivityStatus = status;\n });\n });\n }\n}\n"]}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDefaultConnectivityControllerState = exports.ConnectivityController = exports.ConnectivityStatus = void 0;
|
|
4
|
+
var types_1 = require("./types.cjs");
|
|
5
|
+
Object.defineProperty(exports, "ConnectivityStatus", { enumerable: true, get: function () { return types_1.ConnectivityStatus; } });
|
|
6
|
+
var ConnectivityController_1 = require("./ConnectivityController.cjs");
|
|
7
|
+
Object.defineProperty(exports, "ConnectivityController", { enumerable: true, get: function () { return ConnectivityController_1.ConnectivityController; } });
|
|
8
|
+
Object.defineProperty(exports, "getDefaultConnectivityControllerState", { enumerable: true, get: function () { return ConnectivityController_1.getDefaultConnectivityControllerState; } });
|
|
9
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AASA,qCAA6C;AAApC,2GAAA,kBAAkB,OAAA;AAC3B,uEAGkC;AAFhC,gIAAA,sBAAsB,OAAA;AACtB,+IAAA,qCAAqC,OAAA","sourcesContent":["export type {\n ConnectivityControllerState,\n ConnectivityControllerGetStateAction,\n ConnectivityControllerActions,\n ConnectivityControllerStateChangeEvent,\n ConnectivityControllerEvents,\n ConnectivityControllerMessenger,\n} from './ConnectivityController';\nexport type { ConnectivityService } from './types';\nexport { ConnectivityStatus } from './types';\nexport {\n ConnectivityController,\n getDefaultConnectivityControllerState,\n} from './ConnectivityController';\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { ConnectivityControllerState, ConnectivityControllerGetStateAction, ConnectivityControllerActions, ConnectivityControllerStateChangeEvent, ConnectivityControllerEvents, ConnectivityControllerMessenger, } from "./ConnectivityController.cjs";
|
|
2
|
+
export type { ConnectivityService } from "./types.cjs";
|
|
3
|
+
export { ConnectivityStatus } from "./types.cjs";
|
|
4
|
+
export { ConnectivityController, getDefaultConnectivityControllerState, } from "./ConnectivityController.cjs";
|
|
5
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,2BAA2B,EAC3B,oCAAoC,EACpC,6BAA6B,EAC7B,sCAAsC,EACtC,4BAA4B,EAC5B,+BAA+B,GAChC,qCAAiC;AAClC,YAAY,EAAE,mBAAmB,EAAE,oBAAgB;AACnD,OAAO,EAAE,kBAAkB,EAAE,oBAAgB;AAC7C,OAAO,EACL,sBAAsB,EACtB,qCAAqC,GACtC,qCAAiC"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { ConnectivityControllerState, ConnectivityControllerGetStateAction, ConnectivityControllerActions, ConnectivityControllerStateChangeEvent, ConnectivityControllerEvents, ConnectivityControllerMessenger, } from "./ConnectivityController.mjs";
|
|
2
|
+
export type { ConnectivityService } from "./types.mjs";
|
|
3
|
+
export { ConnectivityStatus } from "./types.mjs";
|
|
4
|
+
export { ConnectivityController, getDefaultConnectivityControllerState, } from "./ConnectivityController.mjs";
|
|
5
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,2BAA2B,EAC3B,oCAAoC,EACpC,6BAA6B,EAC7B,sCAAsC,EACtC,4BAA4B,EAC5B,+BAA+B,GAChC,qCAAiC;AAClC,YAAY,EAAE,mBAAmB,EAAE,oBAAgB;AACnD,OAAO,EAAE,kBAAkB,EAAE,oBAAgB;AAC7C,OAAO,EACL,sBAAsB,EACtB,qCAAqC,GACtC,qCAAiC"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,kBAAkB,EAAE,oBAAgB;AAC7C,OAAO,EACL,sBAAsB,EACtB,qCAAqC,EACtC,qCAAiC","sourcesContent":["export type {\n ConnectivityControllerState,\n ConnectivityControllerGetStateAction,\n ConnectivityControllerActions,\n ConnectivityControllerStateChangeEvent,\n ConnectivityControllerEvents,\n ConnectivityControllerMessenger,\n} from './ConnectivityController';\nexport type { ConnectivityService } from './types';\nexport { ConnectivityStatus } from './types';\nexport {\n ConnectivityController,\n getDefaultConnectivityControllerState,\n} from './ConnectivityController';\n"]}
|
package/dist/types.cjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConnectivityStatus = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Connectivity status constants.
|
|
6
|
+
* Used to represent whether the device has internet connectivity.
|
|
7
|
+
*/
|
|
8
|
+
exports.ConnectivityStatus = {
|
|
9
|
+
Online: 'online',
|
|
10
|
+
Offline: 'offline',
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=types.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.cjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACU,QAAA,kBAAkB,GAAG;IAChC,MAAM,EAAE,QAAQ;IAChB,OAAO,EAAE,SAAS;CACV,CAAC","sourcesContent":["/**\n * Connectivity status constants.\n * Used to represent whether the device has internet connectivity.\n */\nexport const ConnectivityStatus = {\n Online: 'online',\n Offline: 'offline',\n} as const;\n\nexport type ConnectivityStatus =\n (typeof ConnectivityStatus)[keyof typeof ConnectivityStatus];\n\n/**\n * Service interface for platform-specific connectivity detection.\n *\n * Each platform (extension, mobile) implements this interface using\n * platform-specific APIs:\n * - Extension: `navigator.onLine` and `online`/`offline` events\n * - Mobile: `@react-native-community/netinfo`\n *\n * The service is injected into the ConnectivityController, which\n * subscribes to connectivity changes and updates its state accordingly.\n */\nexport type ConnectivityService = {\n /**\n * Returns the current connectivity status.\n *\n * @returns 'online' if the device is online, 'offline' otherwise.\n */\n getStatus(): ConnectivityStatus;\n\n /**\n * Registers a callback to be called when connectivity status changes.\n *\n * @param callback - Function called with 'online' when online, 'offline' when offline.\n */\n onConnectivityChange(callback: (status: ConnectivityStatus) => void): void;\n\n /**\n * Cleans up any resources (event listeners, subscriptions).\n */\n destroy(): void;\n};\n"]}
|
package/dist/types.d.cts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connectivity status constants.
|
|
3
|
+
* Used to represent whether the device has internet connectivity.
|
|
4
|
+
*/
|
|
5
|
+
export declare const ConnectivityStatus: {
|
|
6
|
+
readonly Online: "online";
|
|
7
|
+
readonly Offline: "offline";
|
|
8
|
+
};
|
|
9
|
+
export type ConnectivityStatus = (typeof ConnectivityStatus)[keyof typeof ConnectivityStatus];
|
|
10
|
+
/**
|
|
11
|
+
* Service interface for platform-specific connectivity detection.
|
|
12
|
+
*
|
|
13
|
+
* Each platform (extension, mobile) implements this interface using
|
|
14
|
+
* platform-specific APIs:
|
|
15
|
+
* - Extension: `navigator.onLine` and `online`/`offline` events
|
|
16
|
+
* - Mobile: `@react-native-community/netinfo`
|
|
17
|
+
*
|
|
18
|
+
* The service is injected into the ConnectivityController, which
|
|
19
|
+
* subscribes to connectivity changes and updates its state accordingly.
|
|
20
|
+
*/
|
|
21
|
+
export type ConnectivityService = {
|
|
22
|
+
/**
|
|
23
|
+
* Returns the current connectivity status.
|
|
24
|
+
*
|
|
25
|
+
* @returns 'online' if the device is online, 'offline' otherwise.
|
|
26
|
+
*/
|
|
27
|
+
getStatus(): ConnectivityStatus;
|
|
28
|
+
/**
|
|
29
|
+
* Registers a callback to be called when connectivity status changes.
|
|
30
|
+
*
|
|
31
|
+
* @param callback - Function called with 'online' when online, 'offline' when offline.
|
|
32
|
+
*/
|
|
33
|
+
onConnectivityChange(callback: (status: ConnectivityStatus) => void): void;
|
|
34
|
+
/**
|
|
35
|
+
* Cleans up any resources (event listeners, subscriptions).
|
|
36
|
+
*/
|
|
37
|
+
destroy(): void;
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,kBAAkB;;;CAGrB,CAAC;AAEX,MAAM,MAAM,kBAAkB,GAC5B,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,OAAO,kBAAkB,CAAC,CAAC;AAE/D;;;;;;;;;;GAUG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,SAAS,IAAI,kBAAkB,CAAC;IAEhC;;;;OAIG;IACH,oBAAoB,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,kBAAkB,KAAK,IAAI,GAAG,IAAI,CAAC;IAE3E;;OAEG;IACH,OAAO,IAAI,IAAI,CAAC;CACjB,CAAC"}
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connectivity status constants.
|
|
3
|
+
* Used to represent whether the device has internet connectivity.
|
|
4
|
+
*/
|
|
5
|
+
export declare const ConnectivityStatus: {
|
|
6
|
+
readonly Online: "online";
|
|
7
|
+
readonly Offline: "offline";
|
|
8
|
+
};
|
|
9
|
+
export type ConnectivityStatus = (typeof ConnectivityStatus)[keyof typeof ConnectivityStatus];
|
|
10
|
+
/**
|
|
11
|
+
* Service interface for platform-specific connectivity detection.
|
|
12
|
+
*
|
|
13
|
+
* Each platform (extension, mobile) implements this interface using
|
|
14
|
+
* platform-specific APIs:
|
|
15
|
+
* - Extension: `navigator.onLine` and `online`/`offline` events
|
|
16
|
+
* - Mobile: `@react-native-community/netinfo`
|
|
17
|
+
*
|
|
18
|
+
* The service is injected into the ConnectivityController, which
|
|
19
|
+
* subscribes to connectivity changes and updates its state accordingly.
|
|
20
|
+
*/
|
|
21
|
+
export type ConnectivityService = {
|
|
22
|
+
/**
|
|
23
|
+
* Returns the current connectivity status.
|
|
24
|
+
*
|
|
25
|
+
* @returns 'online' if the device is online, 'offline' otherwise.
|
|
26
|
+
*/
|
|
27
|
+
getStatus(): ConnectivityStatus;
|
|
28
|
+
/**
|
|
29
|
+
* Registers a callback to be called when connectivity status changes.
|
|
30
|
+
*
|
|
31
|
+
* @param callback - Function called with 'online' when online, 'offline' when offline.
|
|
32
|
+
*/
|
|
33
|
+
onConnectivityChange(callback: (status: ConnectivityStatus) => void): void;
|
|
34
|
+
/**
|
|
35
|
+
* Cleans up any resources (event listeners, subscriptions).
|
|
36
|
+
*/
|
|
37
|
+
destroy(): void;
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,kBAAkB;;;CAGrB,CAAC;AAEX,MAAM,MAAM,kBAAkB,GAC5B,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,OAAO,kBAAkB,CAAC,CAAC;AAE/D;;;;;;;;;;GAUG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,SAAS,IAAI,kBAAkB,CAAC;IAEhC;;;;OAIG;IACH,oBAAoB,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,kBAAkB,KAAK,IAAI,GAAG,IAAI,CAAC;IAE3E;;OAEG;IACH,OAAO,IAAI,IAAI,CAAC;CACjB,CAAC"}
|
package/dist/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,MAAM,EAAE,QAAQ;IAChB,OAAO,EAAE,SAAS;CACV,CAAC","sourcesContent":["/**\n * Connectivity status constants.\n * Used to represent whether the device has internet connectivity.\n */\nexport const ConnectivityStatus = {\n Online: 'online',\n Offline: 'offline',\n} as const;\n\nexport type ConnectivityStatus =\n (typeof ConnectivityStatus)[keyof typeof ConnectivityStatus];\n\n/**\n * Service interface for platform-specific connectivity detection.\n *\n * Each platform (extension, mobile) implements this interface using\n * platform-specific APIs:\n * - Extension: `navigator.onLine` and `online`/`offline` events\n * - Mobile: `@react-native-community/netinfo`\n *\n * The service is injected into the ConnectivityController, which\n * subscribes to connectivity changes and updates its state accordingly.\n */\nexport type ConnectivityService = {\n /**\n * Returns the current connectivity status.\n *\n * @returns 'online' if the device is online, 'offline' otherwise.\n */\n getStatus(): ConnectivityStatus;\n\n /**\n * Registers a callback to be called when connectivity status changes.\n *\n * @param callback - Function called with 'online' when online, 'offline' when offline.\n */\n onConnectivityChange(callback: (status: ConnectivityStatus) => void): void;\n\n /**\n * Cleans up any resources (event listeners, subscriptions).\n */\n destroy(): void;\n};\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metamask-previews/connectivity-controller",
|
|
3
|
+
"version": "0.0.0-preview-9e00b40b",
|
|
4
|
+
"description": "ConnectivityController stores the device's internet connectivity status",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"MetaMask",
|
|
7
|
+
"Ethereum"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/MetaMask/core/tree/main/packages/connectivity-controller#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/MetaMask/core/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/MetaMask/core.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"import": {
|
|
22
|
+
"types": "./dist/index.d.mts",
|
|
23
|
+
"default": "./dist/index.mjs"
|
|
24
|
+
},
|
|
25
|
+
"require": {
|
|
26
|
+
"types": "./dist/index.d.cts",
|
|
27
|
+
"default": "./dist/index.cjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"main": "./dist/index.cjs",
|
|
33
|
+
"types": "./dist/index.d.cts",
|
|
34
|
+
"files": [
|
|
35
|
+
"dist/"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
|
|
39
|
+
"build:all": "ts-bridge --project tsconfig.build.json --verbose --clean",
|
|
40
|
+
"build:docs": "typedoc",
|
|
41
|
+
"changelog:update": "../../scripts/update-changelog.sh @metamask/connectivity-controller",
|
|
42
|
+
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/connectivity-controller",
|
|
43
|
+
"publish:preview": "yarn npm publish --tag preview",
|
|
44
|
+
"since-latest-release": "../../scripts/since-latest-release.sh",
|
|
45
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
|
|
46
|
+
"test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
|
|
47
|
+
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
|
|
48
|
+
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@metamask/base-controller": "^9.0.0",
|
|
52
|
+
"@metamask/messenger": "^0.3.0"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@metamask/auto-changelog": "^3.4.4",
|
|
56
|
+
"@ts-bridge/cli": "^0.6.4",
|
|
57
|
+
"@types/jest": "^27.4.1",
|
|
58
|
+
"deepmerge": "^4.2.2",
|
|
59
|
+
"jest": "^27.5.1",
|
|
60
|
+
"ts-jest": "^27.1.4",
|
|
61
|
+
"typedoc": "^0.24.8",
|
|
62
|
+
"typedoc-plugin-missing-exports": "^2.0.0",
|
|
63
|
+
"typescript": "~5.3.3"
|
|
64
|
+
},
|
|
65
|
+
"engines": {
|
|
66
|
+
"node": "^18.18 || >=20"
|
|
67
|
+
},
|
|
68
|
+
"publishConfig": {
|
|
69
|
+
"access": "public",
|
|
70
|
+
"registry": "https://registry.npmjs.org/"
|
|
71
|
+
}
|
|
72
|
+
}
|