|
| 1 | +/* MD |
| 2 | + ## Shared reactive state with AppState |
| 3 | + --- |
| 4 | + Developers building interactive BIM panels often need sibling components — a |
| 5 | + filter selector and a results table, for example — to stay in sync without a |
| 6 | + shared parent. Lifting state to a common ancestor and threading it down as props |
| 7 | + works for shallow trees but becomes unwieldy as the component tree grows. |
| 8 | +
|
| 9 | + top-app provides a typed reactive state object available to every component in |
| 10 | + the tree through context. Any component can write to it, and all subscribers |
| 11 | + re-render automatically — no shared parent required. |
| 12 | +
|
| 13 | + This tutorial covers defining a typed payload for custom app state; building a |
| 14 | + filter selector that updates shared state on button click; building a results |
| 15 | + counter that reads the current filter and count from shared state; composing both |
| 16 | + into a panel inside a top-app layout; and noting when the shared state is |
| 17 | + available relative to the engine boot sequence. |
| 18 | +
|
| 19 | + By the end, you'll have two independent components that stay in sync through |
| 20 | + shared reactive state — a filter selector and a results counter that reflect each |
| 21 | + other's changes without any direct coupling between them. |
| 22 | +*/ |
| 23 | + |
| 24 | +import { html, LitElement } from "lit"; |
| 25 | +import { customElement, state } from "lit/decorators.js"; |
| 26 | +import { consume } from "@lit/context"; |
| 27 | +import * as THREE from "three"; |
| 28 | +import * as OBC from "@thatopen/components"; |
| 29 | +import * as OBF from "@thatopen/components-front"; |
| 30 | +import * as FRAGS from "@thatopen/fragments"; |
| 31 | +import * as BUI from "@thatopen/ui"; |
| 32 | +import { PlatformClient } from "thatopen-services"; |
| 33 | +import uiManagerDef from "../../../../index"; |
| 34 | +import { |
| 35 | + appStateContext, |
| 36 | + AppState, |
| 37 | + AppStateController, |
| 38 | + type App, |
| 39 | +} from "../../index"; |
| 40 | + |
| 41 | +// Define a typed payload for this app's custom state. |
| 42 | +type MyAppState = { selectedFilter: string; count: number }; |
| 43 | + |
| 44 | +// AppStateController subscribes to appStateContext and triggers a re-render |
| 45 | +// on every setCustom call — across any component in the tree that holds one. |
| 46 | +@customElement("filter-selector") |
| 47 | +class FilterSelector extends LitElement { |
| 48 | + @consume({ context: appStateContext, subscribe: true }) |
| 49 | + @state() |
| 50 | + private _appState?: AppState; |
| 51 | + |
| 52 | + // Declare the controller — the LitElement lifecycle handles subscription. |
| 53 | + private _stateCtrl = new AppStateController<MyAppState>(this, () => this._appState); |
| 54 | + |
| 55 | + render() { |
| 56 | + const filters = ["All", "Walls", "Slabs", "Columns"]; |
| 57 | + const active = this._stateCtrl.custom?.selectedFilter ?? "All"; |
| 58 | + return html` |
| 59 | + <bim-panel-section label="Filter" icon="solar:filter-bold"> |
| 60 | + ${filters.map( |
| 61 | + (f) => html` |
| 62 | + <bim-button |
| 63 | + .label=${f} |
| 64 | + ?active=${active === f} |
| 65 | + @click=${() => { |
| 66 | + this._appState?.setCustom<MyAppState>({ selectedFilter: f, count: 0 }); |
| 67 | + }} |
| 68 | + ></bim-button> |
| 69 | + `, |
| 70 | + )} |
| 71 | + </bim-panel-section> |
| 72 | + `; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +@customElement("result-counter") |
| 77 | +class ResultCounter extends LitElement { |
| 78 | + @consume({ context: appStateContext, subscribe: true }) |
| 79 | + @state() |
| 80 | + private _appState?: AppState; |
| 81 | + |
| 82 | + private _stateCtrl = new AppStateController<MyAppState>(this, () => this._appState); |
| 83 | + |
| 84 | + render() { |
| 85 | + const { selectedFilter = "All", count = 0 } = this._stateCtrl.custom ?? {}; |
| 86 | + return html` |
| 87 | + <bim-panel-section label="Results" icon="solar:list-bold"> |
| 88 | + <bim-label>${count} elements match "${selectedFilter}"</bim-label> |
| 89 | + </bim-panel-section> |
| 90 | + `; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// ---- app wiring ---- |
| 95 | + |
| 96 | +const UIManager = uiManagerDef.componentDefinition; |
| 97 | +const client = PlatformClient.fromPlatformContext(); |
| 98 | +const { components } = (await client.setup( |
| 99 | + { OBC, OBF, BUI, THREE, FRAGS }, |
| 100 | + { uuid: UIManager.uuid }, |
| 101 | +)) as { components: OBC.Components }; |
| 102 | +components.get(UIManager).init(); |
| 103 | + |
| 104 | +const app = document.createElement("top-app") as unknown as App; |
| 105 | + |
| 106 | +// appState is available immediately — before setup completes — so components |
| 107 | +// that only consume appStateContext can render while the engine is still booting. |
| 108 | +app.setup = (waitUntil) => { |
| 109 | + waitUntil( |
| 110 | + (async () => { |
| 111 | + const fragments = components.get(OBC.FragmentsManager); |
| 112 | + const workerUrl = await FRAGS.FragmentsModels.getWorker(); |
| 113 | + fragments.init(await FRAGS.toClassicWorker(workerUrl), { |
| 114 | + classicWorker: true, |
| 115 | + }); |
| 116 | + })(), |
| 117 | + "Fragments Core", |
| 118 | + ); |
| 119 | + return { components, client }; |
| 120 | +}; |
| 121 | + |
| 122 | +app.elements = { |
| 123 | + panel: () => html` |
| 124 | + <bim-panel label="State Demo"> |
| 125 | + <filter-selector></filter-selector> |
| 126 | + <result-counter></result-counter> |
| 127 | + </bim-panel> |
| 128 | + `, |
| 129 | +}; |
| 130 | + |
| 131 | +app.layouts = { |
| 132 | + main: { |
| 133 | + label: "Main", |
| 134 | + icon: "solar:home-bold", |
| 135 | + template: `"panel" 1fr / 22rem`, |
| 136 | + }, |
| 137 | +}; |
| 138 | +app.layout = "main"; |
| 139 | + |
| 140 | +const container = document.getElementById("that-open-app") ?? document.body; |
| 141 | +container.appendChild(app); |
| 142 | +document.body.style.margin = "0"; |
0 commit comments