2021-02-04 19:15:23 +01:00
|
|
|
import { ObservableStore } from '@metamask/obs-store';
|
2022-11-24 20:59:07 +01:00
|
|
|
import { getPersistentState } from '@metamask/base-controller';
|
2018-04-13 05:26:50 +02:00
|
|
|
|
2021-05-20 04:57:51 +02:00
|
|
|
/**
|
2022-11-24 20:59:07 +01:00
|
|
|
* @typedef {import('@metamask/base-controller').ControllerMessenger} ControllerMessenger
|
2021-05-20 04:57:51 +02:00
|
|
|
*/
|
|
|
|
|
2018-04-13 05:26:50 +02:00
|
|
|
/**
|
|
|
|
* An ObservableStore that can composes a flat
|
|
|
|
* structure of child stores based on configuration
|
|
|
|
*/
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class ComposableObservableStore extends ObservableStore {
|
2021-05-20 04:57:51 +02:00
|
|
|
/**
|
|
|
|
* Describes which stores are being composed. The key is the name of the
|
|
|
|
* store, and the value is either an ObserableStore, or a controller that
|
2022-11-24 20:59:07 +01:00
|
|
|
* extends one of the two base controllers in the `@metamask/base-controller`
|
2021-05-20 04:57:51 +02:00
|
|
|
* package.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-26 20:10:51 +02:00
|
|
|
* @type {Record<string, object>}
|
2021-05-20 04:57:51 +02:00
|
|
|
*/
|
|
|
|
config = {};
|
|
|
|
|
2018-04-13 05:26:50 +02:00
|
|
|
/**
|
|
|
|
* Create a new store
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} options
|
|
|
|
* @param {object} [options.config] - Map of internal state keys to child stores
|
2021-05-20 04:57:51 +02:00
|
|
|
* @param {ControllerMessenger} options.controllerMessenger - The controller
|
|
|
|
* messenger, used for subscribing to events from BaseControllerV2-based
|
|
|
|
* controllers.
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} [options.state] - The initial store state
|
2022-01-04 19:59:08 +01:00
|
|
|
* @param {boolean} [options.persist] - Whether or not to apply the persistence for v2 controllers
|
2018-04-13 05:26:50 +02:00
|
|
|
*/
|
2021-09-06 04:33:37 +02:00
|
|
|
constructor({ config, controllerMessenger, state, persist }) {
|
2021-05-20 04:57:51 +02:00
|
|
|
super(state);
|
2021-09-06 04:33:37 +02:00
|
|
|
this.persist = persist;
|
2021-05-20 04:57:51 +02:00
|
|
|
this.controllerMessenger = controllerMessenger;
|
2021-05-11 18:39:17 +02:00
|
|
|
if (config) {
|
|
|
|
this.updateStructure(config);
|
|
|
|
}
|
2018-04-13 19:13:36 +02:00
|
|
|
}
|
2018-04-13 05:26:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Composes a new internal store subscription structure
|
|
|
|
*
|
2022-07-26 20:10:51 +02:00
|
|
|
* @param {Record<string, object>} config - Describes which stores are being
|
2021-05-20 04:57:51 +02:00
|
|
|
* composed. The key is the name of the store, and the value is either an
|
|
|
|
* ObserableStore, or a controller that extends one of the two base
|
2022-11-24 20:59:07 +01:00
|
|
|
* controllers in the `@metamask/base-controller` package.
|
2018-04-13 05:26:50 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
updateStructure(config) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.config = config;
|
|
|
|
this.removeAllListeners();
|
2021-05-20 04:57:51 +02:00
|
|
|
for (const key of Object.keys(config)) {
|
|
|
|
if (!config[key]) {
|
|
|
|
throw new Error(`Undefined '${key}'`);
|
|
|
|
}
|
|
|
|
const store = config[key];
|
|
|
|
if (store.subscribe) {
|
|
|
|
config[key].subscribe((state) => {
|
|
|
|
this.updateState({ [key]: state });
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.controllerMessenger.subscribe(
|
|
|
|
`${store.name}:stateChange`,
|
|
|
|
(state) => {
|
2021-09-06 04:33:37 +02:00
|
|
|
let updatedState = state;
|
|
|
|
if (this.persist) {
|
|
|
|
updatedState = getPersistentState(state, config[key].metadata);
|
|
|
|
}
|
|
|
|
this.updateState({ [key]: updatedState });
|
2021-05-20 04:57:51 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2018-04-13 19:13:36 +02:00
|
|
|
}
|
2018-04-13 05:26:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges all child store state into a single object rather than
|
|
|
|
* returning an object keyed by child store class name
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @returns {object} Object containing merged child store state
|
2018-04-13 05:26:50 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getFlatState() {
|
2021-05-11 18:39:17 +02:00
|
|
|
if (!this.config) {
|
|
|
|
return {};
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
let flatState = {};
|
2021-05-11 18:39:17 +02:00
|
|
|
for (const key of Object.keys(this.config)) {
|
|
|
|
const controller = this.config[key];
|
|
|
|
const state = controller.getState
|
|
|
|
? controller.getState()
|
|
|
|
: controller.state;
|
|
|
|
flatState = { ...flatState, ...state };
|
2018-04-13 05:26:50 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return flatState;
|
2018-04-13 05:26:50 +02:00
|
|
|
}
|
|
|
|
}
|