@wc-bindable/composite 0.8.0
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/README.md +379 -0
- package/dist/createCompositeTarget.d.ts +16 -0
- package/dist/createCompositeTarget.d.ts.map +1 -0
- package/dist/createCompositeTarget.js +143 -0
- package/dist/createCompositeTarget.js.map +1 -0
- package/dist/declarative.d.ts +162 -0
- package/dist/declarative.d.ts.map +1 -0
- package/dist/declarative.js +491 -0
- package/dist/declarative.js.map +1 -0
- package/dist/engine.d.ts +118 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +447 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/lit.d.ts +138 -0
- package/dist/lit.d.ts.map +1 -0
- package/dist/lit.js +273 -0
- package/dist/lit.js.map +1 -0
- package/dist/logger.d.ts +29 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +44 -0
- package/dist/logger.js.map +1 -0
- package/dist/reservedNames.d.ts +25 -0
- package/dist/reservedNames.d.ts.map +1 -0
- package/dist/reservedNames.js +77 -0
- package/dist/reservedNames.js.map +1 -0
- package/dist/types.d.ts +91 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +13 -0
- package/dist/types.js.map +1 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
# @wc-bindable/composite
|
|
2
|
+
|
|
3
|
+
Reference implementation of the wc-bindable **Composition profile**
|
|
4
|
+
([SPEC-extensions.md § Extension 4](../../SPEC-extensions.md) /
|
|
5
|
+
[COMPOSITE.md](../../COMPOSITE.md)).
|
|
6
|
+
|
|
7
|
+
Expose **many** wc-bindable source targets as **one** wc-bindable shell target.
|
|
8
|
+
The composed shell is an ordinary wc-bindable target, so every existing consumer
|
|
9
|
+
— `bind()`, the framework adapters, and `@wc-bindable/remote` — works against it
|
|
10
|
+
unchanged.
|
|
11
|
+
|
|
12
|
+
```text
|
|
13
|
+
@wc-bindable/core = observe one target
|
|
14
|
+
@wc-bindable/remote = proxy one target across a transport
|
|
15
|
+
@wc-bindable/composite = expose many targets as one target
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Profile tiers
|
|
19
|
+
|
|
20
|
+
This package implements:
|
|
21
|
+
|
|
22
|
+
- **T1 — Observation** (always): discovery, a synthesized `static wcBindable`
|
|
23
|
+
declaration, name mapping, the three-phase event fan-out, getter / undefined
|
|
24
|
+
preservation, initial-sync `in` semantics, and lifecycle teardown.
|
|
25
|
+
- **T2 — Local facade** (default when inputs/commands are exposed): composed
|
|
26
|
+
inputs become assignable members (`shell["ai.prompt"] = v`) and composed
|
|
27
|
+
commands become callable members (`shell["ai.run"](...)`), delegating to the
|
|
28
|
+
mapped source.
|
|
29
|
+
|
|
30
|
+
Tier **T3** (the Extension-1 `set` / `setWithAck` / `invoke` consumer surface)
|
|
31
|
+
is **not** implemented here; the per-instance tier claim reports
|
|
32
|
+
`extension1: false`.
|
|
33
|
+
|
|
34
|
+
### Do you need T3?
|
|
35
|
+
|
|
36
|
+
T3 is the **consumer-facing Extension 1 method surface** — `shell.set(name, v)`,
|
|
37
|
+
`shell.setWithAck(...)`, `shell.invoke(name, ...)`, plus their `*WithOptions`
|
|
38
|
+
variants and `dispose()`. You only need it when application code (or a downstream
|
|
39
|
+
adapter) calls those **methods by name** and relies on acknowledged delivery,
|
|
40
|
+
`AbortSignal` / `timeoutMs`, or the `WC_BINDABLE_*` error envelope.
|
|
41
|
+
|
|
42
|
+
You very likely do **not** need T3 if you only want to:
|
|
43
|
+
|
|
44
|
+
| Goal | Use |
|
|
45
|
+
|---|---|
|
|
46
|
+
| Read composed properties | **T1** (default) |
|
|
47
|
+
| Write inputs / call commands **locally** | **T2** facade: `shell["ai.prompt"] = v`, `shell["ai.run"](...)` |
|
|
48
|
+
| Expose writable inputs / invokable commands over `@wc-bindable/remote` | **T2** — `RemoteShellProxy` drives the producer through the local facade, not through T3 methods |
|
|
49
|
+
|
|
50
|
+
So `set` / `setWithAck` / `invoke` as **methods** are the only thing missing.
|
|
51
|
+
Until a future version of this package (or a separate package) adds T3, the
|
|
52
|
+
options are:
|
|
53
|
+
|
|
54
|
+
1. **Prefer T2** for local writes and for remote-writable producers — it covers
|
|
55
|
+
the large majority of "I want to set/invoke" cases.
|
|
56
|
+
2. If you specifically need the Extension-1 *method* surface on the composed
|
|
57
|
+
shell, wrap this shell in your own thin Extension-1 layer that resolves the
|
|
58
|
+
composed name and delegates to the source (the routing rules are specified in
|
|
59
|
+
[COMPOSITE.md § 8 / § 9](../../COMPOSITE.md)). Do not advertise it as "T3"
|
|
60
|
+
unless it implements the **full** surface (see COMPOSITE.md § Profile tiers).
|
|
61
|
+
|
|
62
|
+
A T3 implementation will set `extension1: true` on the tier claim; tools should
|
|
63
|
+
gate the set/invoke affordance on that flag, not assume it.
|
|
64
|
+
|
|
65
|
+
## JavaScript API
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { createCompositeTarget } from "@wc-bindable/composite";
|
|
69
|
+
import { bind } from "@wc-bindable/core";
|
|
70
|
+
|
|
71
|
+
const shell = createCompositeTarget({
|
|
72
|
+
sources: { s3: s3Uploader, ai: aiAgent },
|
|
73
|
+
expose: {
|
|
74
|
+
properties: {
|
|
75
|
+
"s3.progress": { source: "s3", name: "progress" },
|
|
76
|
+
"ai.answer": { source: "ai", name: "answer" },
|
|
77
|
+
},
|
|
78
|
+
inputs: { "ai.prompt": { source: "ai", name: "prompt" } },
|
|
79
|
+
commands: { "ai.run": { source: "ai", name: "run" } },
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Observe like any wc-bindable target.
|
|
84
|
+
const unbind = bind(shell, (name, value) => console.log(name, value));
|
|
85
|
+
|
|
86
|
+
// T2 local facade.
|
|
87
|
+
shell["ai.prompt"] = "summarize this";
|
|
88
|
+
await shell["ai.run"]();
|
|
89
|
+
|
|
90
|
+
shell.dispose(); // detaches every source listener (does NOT dispose sources)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Omit `expose` (or pass `"all-prefixed"`) to auto-expose every source member
|
|
94
|
+
under its default `<sourceId>.<sourceName>` name:
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
const shell = createCompositeTarget({
|
|
98
|
+
sources: { s3: s3Uploader, ai: aiAgent },
|
|
99
|
+
expose: "all-prefixed",
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
> **Production tip — pin the public surface with an explicit `expose` map.**
|
|
104
|
+
> `"all-prefixed"` derives the shell's public API from whatever the sources
|
|
105
|
+
> currently declare, so **adding a source, or a source changing its own
|
|
106
|
+
> `wcBindable` declaration, silently changes the composed shell's surface** —
|
|
107
|
+
> potentially adding/removing properties, inputs, or commands that downstream
|
|
108
|
+
> consumers depend on. It is great for prototyping; for a stable contract, list
|
|
109
|
+
> the members you intend to expose explicitly (an unknown member name in the map
|
|
110
|
+
> then fails fast at construction). `"all-prefixed"` is a documented convenience,
|
|
111
|
+
> not a normative requirement (see COMPOSITE.md § Open questions).
|
|
112
|
+
|
|
113
|
+
## Declarative custom-element API (Declarative Shadow DOM)
|
|
114
|
+
|
|
115
|
+
For HTML-first systems (e.g. `@wcstack/state`), declare a composite as a custom
|
|
116
|
+
element. A **definition element** carries `data-wc-composite-definition`; its tag
|
|
117
|
+
name becomes the composed custom element, and its **Declarative Shadow DOM**
|
|
118
|
+
(`<template shadowrootmode="open">`, legacy `shadowroot="open"` also accepted)
|
|
119
|
+
wraps the composed source elements. Each source is tagged with
|
|
120
|
+
`data-wc-source="<id>"` and may carry an optional `data-wc-expose`.
|
|
121
|
+
|
|
122
|
+
```html
|
|
123
|
+
<my-ai-workbench data-wc-composite-definition>
|
|
124
|
+
<template shadowrootmode="open">
|
|
125
|
+
<s3-uploader data-wc-source="s3"
|
|
126
|
+
data-wc-expose="properties: progress, url; inputs: file; commands: upload"></s3-uploader>
|
|
127
|
+
<ai-agent data-wc-source="ai"
|
|
128
|
+
data-wc-expose="properties: loading, answer; inputs: prompt; commands: run"></ai-agent>
|
|
129
|
+
</template>
|
|
130
|
+
</my-ai-workbench>
|
|
131
|
+
|
|
132
|
+
<!-- After registration, plain instances compose their sources: -->
|
|
133
|
+
<my-ai-workbench></my-ai-workbench>
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
import { registerCompositeDefinitions } from "@wc-bindable/composite";
|
|
138
|
+
|
|
139
|
+
// Scans for [data-wc-composite-definition], awaits each source tag's
|
|
140
|
+
// customElements.whenDefined(), then registers each composed custom element.
|
|
141
|
+
await registerCompositeDefinitions(document);
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
The first argument is the **scan root** and defaults to `document`; pass any
|
|
145
|
+
`ParentNode` (or a definition element itself) to scope the scan — important in
|
|
146
|
+
large apps or when multiple frameworks share a page:
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
// Only register definitions inside #app (the root element is itself included
|
|
150
|
+
// if it carries data-wc-composite-definition).
|
|
151
|
+
await registerCompositeDefinitions(document.querySelector("#app")!);
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Each instance builds its **own** shadow root containing one fresh instance of
|
|
155
|
+
every source tag, binds an engine to them, and exposes the composed surface
|
|
156
|
+
(property getters, and the T2 input setters / command members). Omitting
|
|
157
|
+
`data-wc-expose` auto-exposes every source member (subject to the same
|
|
158
|
+
"pin the surface explicitly in production" caveat as the `"all-prefixed"` JS
|
|
159
|
+
default above).
|
|
160
|
+
|
|
161
|
+
#### `data-wc-expose` syntax
|
|
162
|
+
|
|
163
|
+
`data-wc-expose` is a **package-specific** convenience attribute (not part of the
|
|
164
|
+
normative profile; COMPOSITE.md shows it under the non-normative declarative
|
|
165
|
+
candidate API). Per-source grammar, whitespace-insensitive:
|
|
166
|
+
|
|
167
|
+
```text
|
|
168
|
+
properties: <name>, <name>; inputs: <name>; commands: <name>, <name>
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
- Segments are separated by `;`; each segment is `<kind>: <comma-separated names>`.
|
|
172
|
+
- `<kind>` is `properties` / `inputs` / `commands` (singular `property` /
|
|
173
|
+
`input` / `command` also accepted). Segments may appear in any order, and any
|
|
174
|
+
kind may be omitted.
|
|
175
|
+
- Names are the **source-local** member names; the composed public name becomes
|
|
176
|
+
`<sourceId>.<name>`.
|
|
177
|
+
|
|
178
|
+
**Errors fail loudly at registration** — a segment with no `:` (`"progress"`),
|
|
179
|
+
an unknown kind (`"propertis: x"`), a name that does not exist on the source, a
|
|
180
|
+
duplicate composed name, or a reserved name all throw (the
|
|
181
|
+
`registerCompositeDefinitions` / `defineComposite` promise rejects). There is no
|
|
182
|
+
silent "best-effort" partial parse. Build the expose map in JS instead (pass
|
|
183
|
+
`expose` to `defineComposite`) if you prefer not to use the string DSL.
|
|
184
|
+
|
|
185
|
+
You can also register programmatically:
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
import { defineComposite } from "@wc-bindable/composite";
|
|
189
|
+
|
|
190
|
+
const Workbench = await defineComposite({
|
|
191
|
+
tagName: "my-ai-workbench",
|
|
192
|
+
sources: [
|
|
193
|
+
{ id: "s3", tag: "s3-uploader" },
|
|
194
|
+
{ id: "ai", tag: "ai-agent" },
|
|
195
|
+
],
|
|
196
|
+
// expose?: ExposeConfig — defaults to "all-prefixed"
|
|
197
|
+
});
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
> **Source-definition precondition.** Per § Declarative custom element API, the
|
|
201
|
+
> composed element is registered only **after** every source tag is defined
|
|
202
|
+
> (`customElements.whenDefined`), so a shell instance is never observable with a
|
|
203
|
+
> provisional declaration.
|
|
204
|
+
|
|
205
|
+
## Class authoring
|
|
206
|
+
|
|
207
|
+
`defineComposite` returns an opaque, already-registered class. When you want to
|
|
208
|
+
**add your own behavior** (methods, extra rendering, lifecycle work) on top of
|
|
209
|
+
the composed surface, use `defineCompositeClass`: it returns an *unregistered
|
|
210
|
+
base class* you subclass and register yourself.
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
import { defineCompositeClass } from "@wc-bindable/composite";
|
|
214
|
+
|
|
215
|
+
const Base = await defineCompositeClass({
|
|
216
|
+
sources: [
|
|
217
|
+
{ id: "s3", tag: "s3-uploader" },
|
|
218
|
+
{ id: "ai", tag: "ai-agent" },
|
|
219
|
+
],
|
|
220
|
+
// expose?: ExposeConfig — defaults to "all-prefixed"
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
class MyAiWorkbench extends Base {
|
|
224
|
+
reset() {
|
|
225
|
+
this["ai.prompt"] = ""; // your own method on top of the composed T2 facade
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
customElements.define("my-ai-workbench", MyAiWorkbench);
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
The synthesized `static wcBindable` lives on the base and is statically inherited
|
|
232
|
+
by your subclass, so `target.constructor.wcBindable` discovery, `bind()`, the
|
|
233
|
+
framework adapters, and `@wc-bindable/remote` all work against instances
|
|
234
|
+
unchanged. Like `defineComposite`, this awaits every source tag's
|
|
235
|
+
`customElements.whenDefined()` first, so the declaration is fully determined
|
|
236
|
+
before the class is returned.
|
|
237
|
+
|
|
238
|
+
The base owns its shadow root for the source instances and sets them up in
|
|
239
|
+
`connectedCallback`. A subclass that overrides `connectedCallback` MUST call
|
|
240
|
+
`super.connectedCallback()`. The base only manages the source elements it
|
|
241
|
+
creates (`[data-wc-source]`) — it never wipes other shadow content, so a subclass
|
|
242
|
+
may render its own shadow DOM alongside the sources.
|
|
243
|
+
|
|
244
|
+
### Lit
|
|
245
|
+
|
|
246
|
+
`@wc-bindable/composite/lit` builds on the same machinery for
|
|
247
|
+
[Lit](https://lit.dev). `CompositeLitElement(options)` returns a `LitElement`
|
|
248
|
+
base that **is** a composite shell *and* re-renders your `render()` whenever a
|
|
249
|
+
composed value changes (`lit` is an optional peer dependency):
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
import { html } from "lit";
|
|
253
|
+
import { customElement } from "lit/decorators.js";
|
|
254
|
+
import { CompositeLitElement } from "@wc-bindable/composite/lit";
|
|
255
|
+
|
|
256
|
+
const Base = await CompositeLitElement({
|
|
257
|
+
sources: [
|
|
258
|
+
{ id: "s3", tag: "s3-uploader" },
|
|
259
|
+
{ id: "ai", tag: "ai-agent" },
|
|
260
|
+
],
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
@customElement("my-ai-workbench")
|
|
264
|
+
class MyAiWorkbench extends Base {
|
|
265
|
+
render() {
|
|
266
|
+
return html`
|
|
267
|
+
<progress .value=${(this["s3.progress"] as number) ?? 0}></progress>
|
|
268
|
+
<p>${this["ai.answer"]}</p>
|
|
269
|
+
<button @click=${() => { this["ai.prompt"] = "summarize"; (this["ai.run"] as () => unknown)(); }}>
|
|
270
|
+
Run
|
|
271
|
+
</button>
|
|
272
|
+
`;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
Composed names are dotted strings, not Lit reactive properties, so the base
|
|
278
|
+
bridges each composed update to `requestUpdate()` for you. The source elements
|
|
279
|
+
are created once in the element's shadow root; Lit renders your template into the
|
|
280
|
+
**same** shadow root (after the sources), so `static styles` are adopted as
|
|
281
|
+
usual and the two never clash. Call `el.dispose()` for terminal teardown.
|
|
282
|
+
|
|
283
|
+
#### Lit `ReactiveController`
|
|
284
|
+
|
|
285
|
+
When you'd rather **render the sources in your own template** and control their
|
|
286
|
+
placement, use `CompositeController` instead of the base class. It composes the
|
|
287
|
+
instances *you* render (captured via the Lit `ref` directive it hands you),
|
|
288
|
+
exposes the composed values on `.values` (re-rendering the host on each change),
|
|
289
|
+
and exposes the composed shell on `.shell`:
|
|
290
|
+
|
|
291
|
+
```ts
|
|
292
|
+
import { LitElement, html } from "lit";
|
|
293
|
+
import { customElement } from "lit/decorators.js";
|
|
294
|
+
import { CompositeController } from "@wc-bindable/composite/lit";
|
|
295
|
+
|
|
296
|
+
@customElement("my-ai-workbench")
|
|
297
|
+
class MyAiWorkbench extends LitElement {
|
|
298
|
+
#c = new CompositeController(this, {
|
|
299
|
+
sources: [
|
|
300
|
+
{ id: "s3", tag: "s3-uploader" },
|
|
301
|
+
{ id: "ai", tag: "ai-agent" },
|
|
302
|
+
],
|
|
303
|
+
});
|
|
304
|
+
render() {
|
|
305
|
+
return html`
|
|
306
|
+
<s3-uploader ${this.#c.ref("s3")}></s3-uploader>
|
|
307
|
+
<ai-agent ${this.#c.ref("ai")}></ai-agent>
|
|
308
|
+
<p>${(this.#c.values as Record<string, unknown>)["ai.answer"]}</p>
|
|
309
|
+
<button @click=${() => { this.#c.shell!["ai.prompt"] = "hi"; this.#c.shell!["ai.run"](); }}>
|
|
310
|
+
Run
|
|
311
|
+
</button>
|
|
312
|
+
`;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
The difference from `CompositeLitElement`:
|
|
318
|
+
|
|
319
|
+
| | `CompositeLitElement` (base class) | `CompositeController` |
|
|
320
|
+
|---|---|---|
|
|
321
|
+
| Sources | created for you (hidden in shadow) | **you render them**, tagged with `ref(id)` |
|
|
322
|
+
| What is the shell | the **host element itself** | a separate handle on `.shell` |
|
|
323
|
+
| Composed values | property getters on the element (`this["ai.answer"]`) | `.values` snapshot |
|
|
324
|
+
| Shell identity | stable for the element's lifetime | rebuilt on reconnect (controller lifecycle) |
|
|
325
|
+
|
|
326
|
+
`.shell` is a real wc-bindable target (built via `createCompositeTarget`), so you
|
|
327
|
+
can also hand it to `@wc-bindable/remote` or `bind()` it elsewhere. Because the
|
|
328
|
+
controller follows the usual connect/disconnect lifecycle, `.shell` is torn down
|
|
329
|
+
on disconnect and rebuilt on reconnect — reach for `CompositeLitElement` or
|
|
330
|
+
`createCompositeTarget` directly if you need a shell whose identity is stable
|
|
331
|
+
across disconnects.
|
|
332
|
+
|
|
333
|
+
## Tier claim discovery
|
|
334
|
+
|
|
335
|
+
Every shell exposes a frozen, per-instance tier claim under the well-known
|
|
336
|
+
symbol `Symbol.for("wc-bindable.composite.tiers")`:
|
|
337
|
+
|
|
338
|
+
```ts
|
|
339
|
+
import { COMPOSITE_TIERS_SYMBOL } from "@wc-bindable/composite";
|
|
340
|
+
|
|
341
|
+
shell[COMPOSITE_TIERS_SYMBOL];
|
|
342
|
+
// { protocol: "wc-bindable.composite", version: 1,
|
|
343
|
+
// localFacade: true, extension1: false }
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
A tool MUST read this (not the core declaration) to decide whether to render
|
|
347
|
+
set / invoke affordances — a declared input/command is metadata, not a guarantee
|
|
348
|
+
of delegation. The claim is a **hint, not a permission**: composition is not a
|
|
349
|
+
security boundary.
|
|
350
|
+
|
|
351
|
+
## Notable invariants implemented
|
|
352
|
+
|
|
353
|
+
- **Three-phase fan-out** (extract → commit → dispatch) so siblings sharing one
|
|
354
|
+
source event see a coherent committed snapshot, and a getter never observes a
|
|
355
|
+
partially-updated cache (COMPOSITE.md § 6).
|
|
356
|
+
- **Getter-failure isolation**: a throwing source getter is reported (via
|
|
357
|
+
`reportError`, else the injected logger) and its siblings still dispatch
|
|
358
|
+
(§ 12).
|
|
359
|
+
- **undefined / null preservation**: values are carried out-of-band on the shell
|
|
360
|
+
event, not through `CustomEvent.detail` (which would coerce `undefined` →
|
|
361
|
+
`null`) (§ 7).
|
|
362
|
+
- **Reserved-name rejection** mirroring the remote wire profile (the
|
|
363
|
+
`@wc-bindable/` prefix plus `__proto__` / `constructor` / `prototype`), the
|
|
364
|
+
fixed-API members, and the T2 cross-surface collision rule (§ 4).
|
|
365
|
+
- **Immutable synthesized declaration** (frozen for the shell's lifetime, § 11).
|
|
366
|
+
- **Borrow semantics**: `dispose()` detaches the shell's own source listeners
|
|
367
|
+
and never disposes the source targets (§ 10).
|
|
368
|
+
|
|
369
|
+
## Remote interop
|
|
370
|
+
|
|
371
|
+
A composed shell can include `RemoteCoreProxy` sources, and a **T2** shell may be
|
|
372
|
+
exposed through `@wc-bindable/remote` for writable inputs / invokable commands.
|
|
373
|
+
A non-T2 (observation-only) shell exposed remotely must use an observation-only
|
|
374
|
+
wire projection — see COMPOSITE.md § Remote interop. Set `remoteCompatible: true`
|
|
375
|
+
on the config to advertise the claim; reserved names are rejected regardless.
|
|
376
|
+
|
|
377
|
+
## License
|
|
378
|
+
|
|
379
|
+
MIT
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type CompositeShell, type CreateCompositeTargetConfig } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Expose multiple wc-bindable source targets as a single wc-bindable shell
|
|
4
|
+
* target (COMPOSITE.md Extension 4 — Composition). The returned shell is an
|
|
5
|
+
* ordinary `EventTarget` whose `constructor.wcBindable` is the synthesized
|
|
6
|
+
* composed declaration, so `bind()`, every framework adapter, and
|
|
7
|
+
* `@wc-bindable/remote` work against it unchanged.
|
|
8
|
+
*
|
|
9
|
+
* Implements tier **T1** (observation) always, and tier **T2** (local facade —
|
|
10
|
+
* assignable inputs / callable commands) when `localFacade` is enabled (the
|
|
11
|
+
* default whenever inputs / commands are exposed). Tier T3 (the Extension-1
|
|
12
|
+
* `set` / `invoke` surface) is not implemented by this reference target; the
|
|
13
|
+
* tier claim reports `extension1: false`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createCompositeTarget(config: CreateCompositeTargetConfig): CompositeShell;
|
|
16
|
+
//# sourceMappingURL=createCompositeTarget.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createCompositeTarget.d.ts","sourceRoot":"","sources":["../src/createCompositeTarget.ts"],"names":[],"mappings":"AAOA,OAAO,EAGL,KAAK,cAAc,EAEnB,KAAK,2BAA2B,EACjC,MAAM,YAAY,CAAC;AAUpB;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,2BAA2B,GAAG,cAAc,CA4HzF"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { CompositeEngine, declarationsFromSources, planComposition, } from "./engine.js";
|
|
2
|
+
import { consoleLogger } from "./logger.js";
|
|
3
|
+
import { COMPOSITE_PROFILE_VERSION, COMPOSITE_TIERS_SYMBOL, } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Base class for the headless shell instance. A fresh subclass is generated per
|
|
6
|
+
* `createCompositeTarget` call so each shell carries its own `static wcBindable`
|
|
7
|
+
* (the isolated-constructor pattern required by § 1 for per-instance
|
|
8
|
+
* declarations).
|
|
9
|
+
*/
|
|
10
|
+
class CompositeShellBase extends EventTarget {
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Expose multiple wc-bindable source targets as a single wc-bindable shell
|
|
14
|
+
* target (COMPOSITE.md Extension 4 — Composition). The returned shell is an
|
|
15
|
+
* ordinary `EventTarget` whose `constructor.wcBindable` is the synthesized
|
|
16
|
+
* composed declaration, so `bind()`, every framework adapter, and
|
|
17
|
+
* `@wc-bindable/remote` work against it unchanged.
|
|
18
|
+
*
|
|
19
|
+
* Implements tier **T1** (observation) always, and tier **T2** (local facade —
|
|
20
|
+
* assignable inputs / callable commands) when `localFacade` is enabled (the
|
|
21
|
+
* default whenever inputs / commands are exposed). Tier T3 (the Extension-1
|
|
22
|
+
* `set` / `invoke` surface) is not implemented by this reference target; the
|
|
23
|
+
* tier claim reports `extension1: false`.
|
|
24
|
+
*/
|
|
25
|
+
export function createCompositeTarget(config) {
|
|
26
|
+
const expose = config.expose ?? "all-prefixed";
|
|
27
|
+
const logger = config.logger ?? consoleLogger;
|
|
28
|
+
// No cycle check is needed here: `config.sources` are already-constructed
|
|
29
|
+
// instances and the shell is the return value, so a source can never be this
|
|
30
|
+
// shell — a cyclic composition is unconstructable by order (cf. the
|
|
31
|
+
// declarative `defineComposite` self-reference guard).
|
|
32
|
+
const sourceDecls = declarationsFromSources(config.sources);
|
|
33
|
+
// localFacade defaults to true whenever there is any input / command to
|
|
34
|
+
// delegate. Plan once with the resolved flag; the flag also drives the § 4
|
|
35
|
+
// cross-surface collision check inside planComposition.
|
|
36
|
+
let localFacade = config.localFacade;
|
|
37
|
+
if (localFacade === undefined) {
|
|
38
|
+
// Cheap pre-plan as T1 to learn whether inputs/commands exist.
|
|
39
|
+
const probe = planComposition(sourceDecls, expose, { localFacade: false });
|
|
40
|
+
localFacade = probe.inputs.length > 0 || probe.commands.length > 0;
|
|
41
|
+
}
|
|
42
|
+
const plan = planComposition(sourceDecls, expose, { localFacade });
|
|
43
|
+
const declaration = plan.declaration;
|
|
44
|
+
// Generated subclass holding this shell's isolated static declaration (§ 1).
|
|
45
|
+
const ShellClass = class extends CompositeShellBase {
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(ShellClass, "wcBindable", {
|
|
48
|
+
value: declaration,
|
|
49
|
+
enumerable: false,
|
|
50
|
+
writable: false,
|
|
51
|
+
configurable: false,
|
|
52
|
+
});
|
|
53
|
+
const target = new ShellClass();
|
|
54
|
+
const sources = new Map();
|
|
55
|
+
for (const id of Object.keys(config.sources))
|
|
56
|
+
sources.set(id, config.sources[id]);
|
|
57
|
+
const engine = new CompositeEngine(plan, sources, target, logger);
|
|
58
|
+
// § Tier claim: frozen, own, read-only per-instance claim.
|
|
59
|
+
const claim = {
|
|
60
|
+
protocol: "wc-bindable.composite",
|
|
61
|
+
version: COMPOSITE_PROFILE_VERSION,
|
|
62
|
+
localFacade: plan.localFacade,
|
|
63
|
+
extension1: false,
|
|
64
|
+
};
|
|
65
|
+
if (config.remoteCompatible)
|
|
66
|
+
claim.remoteCompatible = true;
|
|
67
|
+
Object.freeze(claim);
|
|
68
|
+
Object.defineProperty(target, COMPOSITE_TIERS_SYMBOL, {
|
|
69
|
+
value: claim,
|
|
70
|
+
enumerable: false,
|
|
71
|
+
writable: false,
|
|
72
|
+
configurable: false,
|
|
73
|
+
});
|
|
74
|
+
// dispose() is materialized as an own method so it is reachable both via the
|
|
75
|
+
// proxy and via direct property access; it is reserved as a composed name.
|
|
76
|
+
Object.defineProperty(target, "dispose", {
|
|
77
|
+
value: () => engine.dispose(),
|
|
78
|
+
enumerable: false,
|
|
79
|
+
writable: false,
|
|
80
|
+
configurable: false,
|
|
81
|
+
});
|
|
82
|
+
engine.install();
|
|
83
|
+
// The EventTarget instance methods must run with the real target as `this`
|
|
84
|
+
// (their internal slots do not resolve through a Proxy receiver). Bind once
|
|
85
|
+
// and cache so repeated reads return a stable reference. Crucially this set
|
|
86
|
+
// does NOT include `constructor` — binding the generated class would strip its
|
|
87
|
+
// static `wcBindable` and break discovery.
|
|
88
|
+
const FORWARDED_METHODS = new Set(["addEventListener", "removeEventListener", "dispatchEvent"]);
|
|
89
|
+
const boundMethods = new Map();
|
|
90
|
+
const bindForwarded = (key, value) => {
|
|
91
|
+
let bound = boundMethods.get(key);
|
|
92
|
+
if (bound === undefined) {
|
|
93
|
+
bound = value.bind(target);
|
|
94
|
+
boundMethods.set(key, bound);
|
|
95
|
+
}
|
|
96
|
+
return bound;
|
|
97
|
+
};
|
|
98
|
+
const handler = {
|
|
99
|
+
has(t, key) {
|
|
100
|
+
// § 5: a composed property reports presence from the source. Input /
|
|
101
|
+
// command facade members report present so tooling can see the surface.
|
|
102
|
+
if (typeof key === "string") {
|
|
103
|
+
if (engine.isProperty(key))
|
|
104
|
+
return engine.has(key);
|
|
105
|
+
if (engine.localFacade && (engine.isInput(key) || engine.isCommand(key)))
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
return Reflect.has(t, key);
|
|
109
|
+
},
|
|
110
|
+
get(t, key) {
|
|
111
|
+
if (typeof key === "string") {
|
|
112
|
+
if (engine.isProperty(key))
|
|
113
|
+
return engine.getValue(key);
|
|
114
|
+
if (engine.localFacade && engine.isCommand(key))
|
|
115
|
+
return engine.getCommand(key);
|
|
116
|
+
// A composed input has no readable value of its own; fall through so a
|
|
117
|
+
// declared input that is not also a property reads as undefined.
|
|
118
|
+
}
|
|
119
|
+
// Forward everything else (addEventListener, dispatchEvent, dispose,
|
|
120
|
+
// constructor, the tier symbol, ...) to the real target, using `t` as the
|
|
121
|
+
// receiver. Only the EventTarget instance methods need `this` rebinding.
|
|
122
|
+
const value = Reflect.get(t, key, t);
|
|
123
|
+
if (typeof value === "function" && FORWARDED_METHODS.has(key)) {
|
|
124
|
+
return bindForwarded(key, value);
|
|
125
|
+
}
|
|
126
|
+
return value;
|
|
127
|
+
},
|
|
128
|
+
set(t, key, value) {
|
|
129
|
+
if (typeof key === "string") {
|
|
130
|
+
if (engine.isProperty(key)) {
|
|
131
|
+
throw new TypeError(`@wc-bindable/composite: composed property ${JSON.stringify(key)} is read-only (it is an output)`);
|
|
132
|
+
}
|
|
133
|
+
if (engine.localFacade && engine.isInput(key)) {
|
|
134
|
+
engine.setInput(key, value); // delegates; a throwing setter propagates
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return Reflect.set(t, key, value, t);
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
return new Proxy(target, handler);
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=createCompositeTarget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createCompositeTarget.js","sourceRoot":"","sources":["../src/createCompositeTarget.ts"],"names":[],"mappings":"AACA,OAAO,EACL,eAAe,EACf,uBAAuB,EACvB,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EACL,yBAAyB,EACzB,sBAAsB,GAIvB,MAAM,YAAY,CAAC;AAEpB;;;;;GAKG;AACH,MAAM,kBAAmB,SAAQ,WAAW;CAAG;AAE/C;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,qBAAqB,CAAC,MAAmC;IACvE,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,cAAc,CAAC;IAC/C,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,IAA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