@metamask-previews/remote-feature-flag-controller 1.0.0-preview-a82fd89
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 +23 -0
- package/LICENSE +6 -0
- package/LICENSE.APACHE2 +201 -0
- package/LICENSE.MIT +20 -0
- package/README.md +15 -0
- package/dist/client-config-api-service/abstract-client-config-api-service.cjs +3 -0
- package/dist/client-config-api-service/abstract-client-config-api-service.cjs.map +1 -0
- package/dist/client-config-api-service/abstract-client-config-api-service.d.cts +7 -0
- package/dist/client-config-api-service/abstract-client-config-api-service.d.cts.map +1 -0
- package/dist/client-config-api-service/abstract-client-config-api-service.d.mts +7 -0
- package/dist/client-config-api-service/abstract-client-config-api-service.d.mts.map +1 -0
- package/dist/client-config-api-service/abstract-client-config-api-service.mjs +2 -0
- package/dist/client-config-api-service/abstract-client-config-api-service.mjs.map +1 -0
- package/dist/client-config-api-service/client-config-api-service.cjs +117 -0
- package/dist/client-config-api-service/client-config-api-service.cjs.map +1 -0
- package/dist/client-config-api-service/client-config-api-service.d.cts +54 -0
- package/dist/client-config-api-service/client-config-api-service.d.cts.map +1 -0
- package/dist/client-config-api-service/client-config-api-service.d.mts +54 -0
- package/dist/client-config-api-service/client-config-api-service.d.mts.map +1 -0
- package/dist/client-config-api-service/client-config-api-service.mjs +113 -0
- package/dist/client-config-api-service/client-config-api-service.mjs.map +1 -0
- package/dist/constants.cjs +8 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.cts +5 -0
- package/dist/constants.d.cts.map +1 -0
- package/dist/constants.d.mts +5 -0
- package/dist/constants.d.mts.map +1 -0
- package/dist/constants.mjs +5 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/index.cjs +12 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +4 -0
- package/dist/index.mjs.map +1 -0
- package/dist/remote-feature-flag-controller-types.cjs +21 -0
- package/dist/remote-feature-flag-controller-types.cjs.map +1 -0
- package/dist/remote-feature-flag-controller-types.d.cts +42 -0
- package/dist/remote-feature-flag-controller-types.d.cts.map +1 -0
- package/dist/remote-feature-flag-controller-types.d.mts +42 -0
- package/dist/remote-feature-flag-controller-types.d.mts.map +1 -0
- package/dist/remote-feature-flag-controller-types.mjs +18 -0
- package/dist/remote-feature-flag-controller-types.mjs.map +1 -0
- package/dist/remote-feature-flag-controller.cjs +120 -0
- package/dist/remote-feature-flag-controller.cjs.map +1 -0
- package/dist/remote-feature-flag-controller.d.cts +69 -0
- package/dist/remote-feature-flag-controller.d.cts.map +1 -0
- package/dist/remote-feature-flag-controller.d.mts +69 -0
- package/dist/remote-feature-flag-controller.d.mts.map +1 -0
- package/dist/remote-feature-flag-controller.mjs +115 -0
- package/dist/remote-feature-flag-controller.mjs.map +1 -0
- package/package.json +74 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedControllerMessenger } from "@metamask/base-controller";
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
3
|
+
import type { AbstractClientConfigApiService } from "./client-config-api-service/abstract-client-config-api-service.mjs";
|
|
4
|
+
import type { FeatureFlags } from "./remote-feature-flag-controller-types.mjs";
|
|
5
|
+
export declare const controllerName = "RemoteFeatureFlagController";
|
|
6
|
+
export declare const DEFAULT_CACHE_DURATION: number;
|
|
7
|
+
export type RemoteFeatureFlagControllerState = {
|
|
8
|
+
remoteFeatureFlags: FeatureFlags;
|
|
9
|
+
cacheTimestamp: number;
|
|
10
|
+
};
|
|
11
|
+
export type RemoteFeatureFlagControllerGetStateAction = ControllerGetStateAction<typeof controllerName, RemoteFeatureFlagControllerState>;
|
|
12
|
+
export type RemoteFeatureFlagControllerGetRemoteFeatureFlagAction = {
|
|
13
|
+
type: `${typeof controllerName}:updateRemoteFeatureFlags`;
|
|
14
|
+
handler: RemoteFeatureFlagController['updateRemoteFeatureFlags'];
|
|
15
|
+
};
|
|
16
|
+
export type RemoteFeatureFlagControllerActions = RemoteFeatureFlagControllerGetStateAction;
|
|
17
|
+
export type AllowedActions = never;
|
|
18
|
+
export type RemoteFeatureFlagControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, RemoteFeatureFlagControllerState>;
|
|
19
|
+
export type RemoteFeatureFlagControllerEvents = RemoteFeatureFlagControllerStateChangeEvent;
|
|
20
|
+
export type AllowedEvents = never;
|
|
21
|
+
export type RemoteFeatureFlagControllerMessenger = RestrictedControllerMessenger<typeof controllerName, RemoteFeatureFlagControllerActions | AllowedActions, RemoteFeatureFlagControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
22
|
+
/**
|
|
23
|
+
* Returns the default state for the RemoteFeatureFlagController.
|
|
24
|
+
*
|
|
25
|
+
* @returns The default controller state.
|
|
26
|
+
*/
|
|
27
|
+
export declare function getDefaultRemoteFeatureFlagControllerState(): RemoteFeatureFlagControllerState;
|
|
28
|
+
/**
|
|
29
|
+
* The RemoteFeatureFlagController manages the retrieval and caching of remote feature flags.
|
|
30
|
+
* It fetches feature flags from a remote API, caches them, and provides methods to access
|
|
31
|
+
* and manage these flags. The controller ensures that feature flags are refreshed based on
|
|
32
|
+
* a specified interval and handles cases where the controller is disabled or the network is unavailable.
|
|
33
|
+
*/
|
|
34
|
+
export declare class RemoteFeatureFlagController extends BaseController<typeof controllerName, RemoteFeatureFlagControllerState, RemoteFeatureFlagControllerMessenger> {
|
|
35
|
+
#private;
|
|
36
|
+
/**
|
|
37
|
+
* Constructs a new RemoteFeatureFlagController instance.
|
|
38
|
+
*
|
|
39
|
+
* @param options - The controller options.
|
|
40
|
+
* @param options.messenger - The controller messenger used for communication.
|
|
41
|
+
* @param options.state - The initial state of the controller.
|
|
42
|
+
* @param options.clientConfigApiService - The service instance to fetch remote feature flags.
|
|
43
|
+
* @param options.fetchInterval - The interval in milliseconds before cached flags expire. Defaults to 1 day.
|
|
44
|
+
* @param options.disabled - Determines if the controller should be disabled initially. Defaults to false.
|
|
45
|
+
*/
|
|
46
|
+
constructor({ messenger, state, clientConfigApiService, fetchInterval, disabled, }: {
|
|
47
|
+
messenger: RemoteFeatureFlagControllerMessenger;
|
|
48
|
+
state?: Partial<RemoteFeatureFlagControllerState>;
|
|
49
|
+
clientConfigApiService: AbstractClientConfigApiService;
|
|
50
|
+
fetchInterval?: number;
|
|
51
|
+
disabled?: boolean;
|
|
52
|
+
});
|
|
53
|
+
/**
|
|
54
|
+
* Retrieves the remote feature flags, fetching from the API if necessary.
|
|
55
|
+
* Uses caching to prevent redundant API calls and handles concurrent fetches.
|
|
56
|
+
*
|
|
57
|
+
* @returns A promise that resolves to the current set of feature flags.
|
|
58
|
+
*/
|
|
59
|
+
updateRemoteFeatureFlags(): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Enables the controller, allowing it to make network requests.
|
|
62
|
+
*/
|
|
63
|
+
enable(): void;
|
|
64
|
+
/**
|
|
65
|
+
* Disables the controller, preventing it from making network requests.
|
|
66
|
+
*/
|
|
67
|
+
disable(): void;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=remote-feature-flag-controller.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote-feature-flag-controller.d.mts","sourceRoot":"","sources":["../src/remote-feature-flag-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,6BAA6B,EAC9B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,KAAK,EAAE,8BAA8B,EAAE,2EAAuE;AACrH,OAAO,KAAK,EACV,YAAY,EAEb,mDAA+C;AAIhD,eAAO,MAAM,cAAc,gCAAgC,CAAC;AAC5D,eAAO,MAAM,sBAAsB,QAAsB,CAAC;AAI1D,MAAM,MAAM,gCAAgC,GAAG;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;IA0BD;;;;;OAKG;IACG,wBAAwB,IAAI,OAAO,CAAC,IAAI,CAAC;IAuC/C;;OAEG;IACH,MAAM,IAAI,IAAI;IAId;;OAEG;IACH,OAAO,IAAI,IAAI;CAGhB"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _RemoteFeatureFlagController_instances, _RemoteFeatureFlagController_fetchInterval, _RemoteFeatureFlagController_disabled, _RemoteFeatureFlagController_clientConfigApiService, _RemoteFeatureFlagController_inProgressFlagUpdate, _RemoteFeatureFlagController_isCacheExpired, _RemoteFeatureFlagController_updateCache;
|
|
13
|
+
import { BaseController } from "@metamask/base-controller";
|
|
14
|
+
// === GENERAL ===
|
|
15
|
+
export const controllerName = 'RemoteFeatureFlagController';
|
|
16
|
+
export const DEFAULT_CACHE_DURATION = 24 * 60 * 60 * 1000; // 1 day
|
|
17
|
+
const remoteFeatureFlagControllerMetadata = {
|
|
18
|
+
remoteFeatureFlags: { persist: true, anonymous: true },
|
|
19
|
+
cacheTimestamp: { persist: true, anonymous: true },
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Returns the default state for the RemoteFeatureFlagController.
|
|
23
|
+
*
|
|
24
|
+
* @returns The default controller state.
|
|
25
|
+
*/
|
|
26
|
+
export function getDefaultRemoteFeatureFlagControllerState() {
|
|
27
|
+
return {
|
|
28
|
+
remoteFeatureFlags: {},
|
|
29
|
+
cacheTimestamp: 0,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The RemoteFeatureFlagController manages the retrieval and caching of remote feature flags.
|
|
34
|
+
* It fetches feature flags from a remote API, caches them, and provides methods to access
|
|
35
|
+
* and manage these flags. The controller ensures that feature flags are refreshed based on
|
|
36
|
+
* a specified interval and handles cases where the controller is disabled or the network is unavailable.
|
|
37
|
+
*/
|
|
38
|
+
export class RemoteFeatureFlagController extends BaseController {
|
|
39
|
+
/**
|
|
40
|
+
* Constructs a new RemoteFeatureFlagController instance.
|
|
41
|
+
*
|
|
42
|
+
* @param options - The controller options.
|
|
43
|
+
* @param options.messenger - The controller messenger used for communication.
|
|
44
|
+
* @param options.state - The initial state of the controller.
|
|
45
|
+
* @param options.clientConfigApiService - The service instance to fetch remote feature flags.
|
|
46
|
+
* @param options.fetchInterval - The interval in milliseconds before cached flags expire. Defaults to 1 day.
|
|
47
|
+
* @param options.disabled - Determines if the controller should be disabled initially. Defaults to false.
|
|
48
|
+
*/
|
|
49
|
+
constructor({ messenger, state, clientConfigApiService, fetchInterval = DEFAULT_CACHE_DURATION, disabled = false, }) {
|
|
50
|
+
super({
|
|
51
|
+
name: controllerName,
|
|
52
|
+
metadata: remoteFeatureFlagControllerMetadata,
|
|
53
|
+
messenger,
|
|
54
|
+
state: {
|
|
55
|
+
...getDefaultRemoteFeatureFlagControllerState(),
|
|
56
|
+
...state,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
_RemoteFeatureFlagController_instances.add(this);
|
|
60
|
+
_RemoteFeatureFlagController_fetchInterval.set(this, void 0);
|
|
61
|
+
_RemoteFeatureFlagController_disabled.set(this, void 0);
|
|
62
|
+
_RemoteFeatureFlagController_clientConfigApiService.set(this, void 0);
|
|
63
|
+
_RemoteFeatureFlagController_inProgressFlagUpdate.set(this, void 0);
|
|
64
|
+
__classPrivateFieldSet(this, _RemoteFeatureFlagController_fetchInterval, fetchInterval, "f");
|
|
65
|
+
__classPrivateFieldSet(this, _RemoteFeatureFlagController_disabled, disabled, "f");
|
|
66
|
+
__classPrivateFieldSet(this, _RemoteFeatureFlagController_clientConfigApiService, clientConfigApiService, "f");
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Retrieves the remote feature flags, fetching from the API if necessary.
|
|
70
|
+
* Uses caching to prevent redundant API calls and handles concurrent fetches.
|
|
71
|
+
*
|
|
72
|
+
* @returns A promise that resolves to the current set of feature flags.
|
|
73
|
+
*/
|
|
74
|
+
async updateRemoteFeatureFlags() {
|
|
75
|
+
if (__classPrivateFieldGet(this, _RemoteFeatureFlagController_disabled, "f") || !__classPrivateFieldGet(this, _RemoteFeatureFlagController_instances, "m", _RemoteFeatureFlagController_isCacheExpired).call(this)) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
let serverData;
|
|
79
|
+
if (__classPrivateFieldGet(this, _RemoteFeatureFlagController_inProgressFlagUpdate, "f")) {
|
|
80
|
+
await __classPrivateFieldGet(this, _RemoteFeatureFlagController_inProgressFlagUpdate, "f");
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
__classPrivateFieldSet(this, _RemoteFeatureFlagController_inProgressFlagUpdate, __classPrivateFieldGet(this, _RemoteFeatureFlagController_clientConfigApiService, "f").fetchRemoteFeatureFlags(), "f");
|
|
85
|
+
serverData = await __classPrivateFieldGet(this, _RemoteFeatureFlagController_inProgressFlagUpdate, "f");
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
__classPrivateFieldSet(this, _RemoteFeatureFlagController_inProgressFlagUpdate, undefined, "f");
|
|
89
|
+
}
|
|
90
|
+
__classPrivateFieldGet(this, _RemoteFeatureFlagController_instances, "m", _RemoteFeatureFlagController_updateCache).call(this, serverData.remoteFeatureFlags);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Enables the controller, allowing it to make network requests.
|
|
94
|
+
*/
|
|
95
|
+
enable() {
|
|
96
|
+
__classPrivateFieldSet(this, _RemoteFeatureFlagController_disabled, false, "f");
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Disables the controller, preventing it from making network requests.
|
|
100
|
+
*/
|
|
101
|
+
disable() {
|
|
102
|
+
__classPrivateFieldSet(this, _RemoteFeatureFlagController_disabled, true, "f");
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
_RemoteFeatureFlagController_fetchInterval = new WeakMap(), _RemoteFeatureFlagController_disabled = new WeakMap(), _RemoteFeatureFlagController_clientConfigApiService = new WeakMap(), _RemoteFeatureFlagController_inProgressFlagUpdate = new WeakMap(), _RemoteFeatureFlagController_instances = new WeakSet(), _RemoteFeatureFlagController_isCacheExpired = function _RemoteFeatureFlagController_isCacheExpired() {
|
|
106
|
+
return Date.now() - this.state.cacheTimestamp > __classPrivateFieldGet(this, _RemoteFeatureFlagController_fetchInterval, "f");
|
|
107
|
+
}, _RemoteFeatureFlagController_updateCache = function _RemoteFeatureFlagController_updateCache(remoteFeatureFlags) {
|
|
108
|
+
this.update(() => {
|
|
109
|
+
return {
|
|
110
|
+
remoteFeatureFlags,
|
|
111
|
+
cacheTimestamp: Date.now(),
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
//# sourceMappingURL=remote-feature-flag-controller.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote-feature-flag-controller.mjs","sourceRoot":"","sources":["../src/remote-feature-flag-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,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AEf,IAAI,uBAAA,IAAI,yDAAsB,EAAE;YAC9B,MAAM,uBAAA,IAAI,yDAAsB,CAAC;YACjC,OAAO;SACR;QAED,IAAI;YACF,uBAAA,IAAI,qDACF,uBAAA,IAAI,2DAAwB,CAAC,uBAAuB,EAAE,MAAA,CAAC;YAEzD,UAAU,GAAG,MAAM,uBAAA,IAAI,yDAAsB,CAAC;SAC/C;gBAAS;YACR,uBAAA,IAAI,qDAAyB,SAAS,MAAA,CAAC;SACxC;QAED,uBAAA,IAAI,wFAAa,MAAjB,IAAI,EAAc,UAAU,CAAC,kBAAkB,CAAC,CAAC;IACnD,CAAC;IAiBD;;OAEG;IACH,MAAM;QACJ,uBAAA,IAAI,yCAAa,KAAK,MAAA,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,OAAO;QACL,uBAAA,IAAI,yCAAa,IAAI,MAAA,CAAC;IACxB,CAAC;CACF;;IA7DG,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,cAAc,GAAG,uBAAA,IAAI,kDAAe,CAAC;AACtE,CAAC,+FAsCY,kBAAgC;IAC3C,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;QACf,OAAO;YACL,kBAAkB;YAClB,cAAc,EAAE,IAAI,CAAC,GAAG,EAAE;SAC3B,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedControllerMessenger,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\n\nimport type { AbstractClientConfigApiService } from './client-config-api-service/abstract-client-config-api-service';\nimport type {\n FeatureFlags,\n ServiceResponse,\n} from './remote-feature-flag-controller-types';\n\n// === GENERAL ===\n\nexport const controllerName = 'RemoteFeatureFlagController';\nexport const DEFAULT_CACHE_DURATION = 24 * 60 * 60 * 1000; // 1 day\n\n// === STATE ===\n\nexport type RemoteFeatureFlagControllerState = {\n remoteFeatureFlags: FeatureFlags;\n cacheTimestamp: number;\n};\n\nconst remoteFeatureFlagControllerMetadata = {\n remoteFeatureFlags: { persist: true, anonymous: true },\n cacheTimestamp: { persist: true, anonymous: true },\n};\n\n// === MESSENGER ===\n\nexport type RemoteFeatureFlagControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n RemoteFeatureFlagControllerState\n >;\n\nexport type RemoteFeatureFlagControllerGetRemoteFeatureFlagAction = {\n type: `${typeof controllerName}:updateRemoteFeatureFlags`;\n handler: RemoteFeatureFlagController['updateRemoteFeatureFlags'];\n};\n\nexport type RemoteFeatureFlagControllerActions =\n RemoteFeatureFlagControllerGetStateAction;\n\nexport type AllowedActions = never;\n\nexport type RemoteFeatureFlagControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n RemoteFeatureFlagControllerState\n >;\n\nexport type RemoteFeatureFlagControllerEvents =\n RemoteFeatureFlagControllerStateChangeEvent;\n\nexport type AllowedEvents = never;\n\nexport type RemoteFeatureFlagControllerMessenger =\n RestrictedControllerMessenger<\n typeof controllerName,\n RemoteFeatureFlagControllerActions | AllowedActions,\n RemoteFeatureFlagControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n >;\n\n/**\n * Returns the default state for the RemoteFeatureFlagController.\n *\n * @returns The default controller state.\n */\nexport function getDefaultRemoteFeatureFlagControllerState(): RemoteFeatureFlagControllerState {\n return {\n remoteFeatureFlags: {},\n cacheTimestamp: 0,\n };\n}\n\n/**\n * The RemoteFeatureFlagController manages the retrieval and caching of remote feature flags.\n * It fetches feature flags from a remote API, caches them, and provides methods to access\n * and manage these flags. The controller ensures that feature flags are refreshed based on\n * a specified interval and handles cases where the controller is disabled or the network is unavailable.\n */\nexport class RemoteFeatureFlagController extends BaseController<\n typeof controllerName,\n RemoteFeatureFlagControllerState,\n RemoteFeatureFlagControllerMessenger\n> {\n readonly #fetchInterval: number;\n\n #disabled: boolean;\n\n #clientConfigApiService: AbstractClientConfigApiService;\n\n #inProgressFlagUpdate?: Promise<ServiceResponse>;\n\n /**\n * Constructs a new RemoteFeatureFlagController instance.\n *\n * @param options - The controller options.\n * @param options.messenger - The controller messenger used for communication.\n * @param options.state - The initial state of the controller.\n * @param options.clientConfigApiService - The service instance to fetch remote feature flags.\n * @param options.fetchInterval - The interval in milliseconds before cached flags expire. Defaults to 1 day.\n * @param options.disabled - Determines if the controller should be disabled initially. Defaults to false.\n */\n constructor({\n messenger,\n state,\n clientConfigApiService,\n fetchInterval = DEFAULT_CACHE_DURATION,\n disabled = false,\n }: {\n messenger: RemoteFeatureFlagControllerMessenger;\n state?: Partial<RemoteFeatureFlagControllerState>;\n clientConfigApiService: AbstractClientConfigApiService;\n fetchInterval?: number;\n disabled?: boolean;\n }) {\n super({\n name: controllerName,\n metadata: remoteFeatureFlagControllerMetadata,\n messenger,\n state: {\n ...getDefaultRemoteFeatureFlagControllerState(),\n ...state,\n },\n });\n\n this.#fetchInterval = fetchInterval;\n this.#disabled = disabled;\n this.#clientConfigApiService = clientConfigApiService;\n }\n\n /**\n * Checks if the cached feature flags are expired based on the fetch interval.\n *\n * @returns Whether the cache is expired (`true`) or still valid (`false`).\n * @private\n */\n #isCacheExpired(): boolean {\n return Date.now() - this.state.cacheTimestamp > this.#fetchInterval;\n }\n\n /**\n * Retrieves the remote feature flags, fetching from the API if necessary.\n * Uses caching to prevent redundant API calls and handles concurrent fetches.\n *\n * @returns A promise that resolves to the current set of feature flags.\n */\n async updateRemoteFeatureFlags(): Promise<void> {\n if (this.#disabled || !this.#isCacheExpired()) {\n return;\n }\n\n let serverData;\n\n if (this.#inProgressFlagUpdate) {\n await this.#inProgressFlagUpdate;\n return;\n }\n\n try {\n this.#inProgressFlagUpdate =\n this.#clientConfigApiService.fetchRemoteFeatureFlags();\n\n serverData = await this.#inProgressFlagUpdate;\n } finally {\n this.#inProgressFlagUpdate = undefined;\n }\n\n this.#updateCache(serverData.remoteFeatureFlags);\n }\n\n /**\n * Updates the controller's state with new feature flags and resets the cache timestamp.\n *\n * @param remoteFeatureFlags - The new feature flags to cache.\n * @private\n */\n #updateCache(remoteFeatureFlags: FeatureFlags) {\n this.update(() => {\n return {\n remoteFeatureFlags,\n cacheTimestamp: Date.now(),\n };\n });\n }\n\n /**\n * Enables the controller, allowing it to make network requests.\n */\n enable(): void {\n this.#disabled = false;\n }\n\n /**\n * Disables the controller, preventing it from making network requests.\n */\n disable(): void {\n this.#disabled = true;\n }\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metamask-previews/remote-feature-flag-controller",
|
|
3
|
+
"version": "1.0.0-preview-a82fd89",
|
|
4
|
+
"description": "The RemoteFeatureFlagController manages the retrieval and caching of remote feature flags",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"MetaMask",
|
|
7
|
+
"Ethereum"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/MetaMask/core/tree/main/packages/remote-feature-flag-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 OR Apache-2.0)",
|
|
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:docs": "typedoc",
|
|
40
|
+
"changelog:update": "../../scripts/update-changelog.sh @metamask/remote-feature-flag-controller",
|
|
41
|
+
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/remote-feature-flag-controller",
|
|
42
|
+
"publish:preview": "yarn npm publish --tag preview",
|
|
43
|
+
"since-latest-release": "../../scripts/since-latest-release.sh",
|
|
44
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
|
|
45
|
+
"test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
|
|
46
|
+
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
|
|
47
|
+
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@metamask/base-controller": "^7.0.2",
|
|
51
|
+
"@metamask/utils": "^10.0.0",
|
|
52
|
+
"cockatiel": "^3.1.2"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@lavamoat/allow-scripts": "^3.0.4",
|
|
56
|
+
"@metamask/auto-changelog": "^3.4.4",
|
|
57
|
+
"@metamask/controller-utils": "^11.4.3",
|
|
58
|
+
"@types/jest": "^27.4.1",
|
|
59
|
+
"deepmerge": "^4.2.2",
|
|
60
|
+
"jest": "^27.5.1",
|
|
61
|
+
"nock": "^13.3.1",
|
|
62
|
+
"ts-jest": "^27.1.4",
|
|
63
|
+
"typedoc": "^0.24.8",
|
|
64
|
+
"typedoc-plugin-missing-exports": "^2.0.0",
|
|
65
|
+
"typescript": "~5.2.2"
|
|
66
|
+
},
|
|
67
|
+
"engines": {
|
|
68
|
+
"node": "^18.18 || >=20"
|
|
69
|
+
},
|
|
70
|
+
"publishConfig": {
|
|
71
|
+
"access": "public",
|
|
72
|
+
"registry": "https://registry.npmjs.org/"
|
|
73
|
+
}
|
|
74
|
+
}
|