1 | import { getCollection } from "./collection.js";
|
2 | import { getConfig } from "./commands.js";
|
3 | function processComponentLoaded(state, event) {
|
4 | if (state === undefined)
|
5 | return null;
|
6 | return {
|
7 | components: state.components.concat(event.data.component)
|
8 | };
|
9 | }
|
10 | const fetchConfig = (conn) => getConfig(conn);
|
11 | const subscribeUpdates = (conn, store) => Promise.all([
|
12 | conn.subscribeEvents(store.action(processComponentLoaded), "component_loaded"),
|
13 | conn.subscribeEvents(() => fetchConfig(conn).then(config => store.setState(config, true)), "core_config_updated")
|
14 | ]).then(unsubs => () => unsubs.forEach(unsub => unsub()));
|
15 | const configColl = (conn) => getCollection(conn, "_cnf", fetchConfig, subscribeUpdates);
|
16 | export const subscribeConfig = (conn, onChange) => configColl(conn).subscribe(onChange);
|