@panyam/tsappkit 0.7.4 → 0.7.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@panyam/tsappkit",
3
- "version": "0.7.4",
3
+ "version": "0.7.5",
4
4
  "description": "TypeScript application toolkit with component lifecycle management, event system, UI utilities, and documentation site components",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
package/src/Port.ts ADDED
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The views a presenter drives through one typed view interface, its "port"
3
+ * (docs/PRESENTER_CONTRACT_THESIS.md, Principle 3). A presenter keeps one Port
4
+ * per interface it talks to, so one presenter can drive several islands on a
5
+ * page without an EventBus or a store between them:
6
+ *
7
+ * readonly tools = new Port<ToolsView>((v) => v.setToolState(this.state));
8
+ * select(t: Tool) { this.state = ...; this.tools.each((v) => v.setToolState(this.state)); }
9
+ *
10
+ * Islands mount at different times (a `visible` one may mount long after the
11
+ * page started), so a view attached late has missed every command sent so far.
12
+ * The replay passed to the constructor runs on each view as it attaches and
13
+ * should send the presenter's current state, which is how a late view catches
14
+ * up. A port takes any number of views, including none: commands to an empty
15
+ * port go nowhere.
16
+ */
17
+ export class Port<V> {
18
+ private readonly views = new Set<V>();
19
+
20
+ /** @param replay runs on each view as it attaches, to bring it up to date. */
21
+ constructor(private readonly replay?: (view: V) => void) {}
22
+
23
+ /**
24
+ * Adds a view, runs the replay on it, and returns the function that removes
25
+ * it again. An island attaches when it activates and detaches when it
26
+ * deactivates (tsappkit-solid's useView does both). Attaching the same view
27
+ * twice keeps one copy, and calling the returned function more than once is
28
+ * harmless.
29
+ */
30
+ attach(view: V): () => void {
31
+ this.views.add(view);
32
+ this.replay?.(view);
33
+ return () => {
34
+ this.views.delete(view);
35
+ };
36
+ }
37
+
38
+ /**
39
+ * Sends a command to every view attached when it's called, in the order they
40
+ * attached. A view may detach (or attach another) from inside the command.
41
+ * A view that throws stops the command reaching the views after it.
42
+ */
43
+ each(send: (view: V) => void): void {
44
+ for (const view of [...this.views]) send(view);
45
+ }
46
+
47
+ /** How many views are attached. */
48
+ get size(): number {
49
+ return this.views.size;
50
+ }
51
+ }
package/src/index.ts CHANGED
@@ -27,6 +27,9 @@ export { TemplateLoader } from './TemplateLoader';
27
27
  export { SplashScreen } from './SplashScreen';
28
28
  export { MobileBottomDrawer } from './MobileBottomDrawer';
29
29
 
30
+ // Presenter ports (one presenter driving several views)
31
+ export { Port } from './Port';
32
+
30
33
  // Island pages (islands mounted from a server-written page spec)
31
34
  export { IslandPage } from './page/IslandPage';
32
35
  export { readSpec, SPEC_ELEMENT_ID } from './page/spec';