UNPKG

820 BJavaScriptView Raw
1import { getCollection } from "./collection.js";
2import { getConfig } from "./commands.js";
3function processComponentLoaded(state, event) {
4 if (state === undefined)
5 return null;
6 return {
7 components: state.components.concat(event.data.component)
8 };
9}
10const fetchConfig = (conn) => getConfig(conn);
11const 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()));
15const configColl = (conn) => getCollection(conn, "_cnf", fetchConfig, subscribeUpdates);
16export const subscribeConfig = (conn, onChange) => configColl(conn).subscribe(onChange);