@supersoniks/concorde 3.2.8 → 3.3.2
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/build-infos.json +1 -1
- package/concorde-core.bundle.js +229 -229
- package/concorde-core.es.js +2166 -1831
- package/dist/concorde-core.bundle.js +229 -229
- package/dist/concorde-core.es.js +2166 -1831
- package/docs/assets/{index-C0K6xugr.css → index-B669R8JF.css} +1 -1
- package/docs/assets/index-BTo6ly4d.js +4820 -0
- package/docs/index.html +2 -2
- package/docs/src/core/components/functional/fetch/fetch.md +6 -0
- package/docs/src/core/components/ui/menu/menu.md +46 -5
- package/docs/src/core/components/ui/modal/modal.md +0 -4
- package/docs/src/core/components/ui/toast/toast.md +166 -0
- package/docs/src/docs/_misc/ancestor-attribute.md +94 -0
- package/docs/src/docs/_misc/auto-subscribe.md +199 -0
- package/docs/src/docs/_misc/bind.md +362 -0
- package/docs/src/docs/_misc/on-assign.md +336 -0
- package/docs/src/docs/_misc/templates-demo.md +19 -0
- package/docs/src/docs/search/docs-search.json +550 -0
- package/docs/src/tsconfig-model.json +1 -1
- package/docs/src/tsconfig.json +28 -8
- package/package.json +8 -1
- package/src/core/components/functional/queue/queue.demo.ts +8 -11
- package/src/core/components/functional/sdui/sdui.ts +0 -0
- package/src/core/decorators/Subscriber.ts +5 -187
- package/src/core/decorators/subscriber/ancestorAttribute.ts +17 -0
- package/src/core/decorators/subscriber/autoFill.ts +28 -0
- package/src/core/decorators/subscriber/autoSubscribe.ts +54 -0
- package/src/core/decorators/subscriber/bind.ts +305 -0
- package/src/core/decorators/subscriber/common.ts +50 -0
- package/src/core/decorators/subscriber/onAssign.ts +318 -0
- package/src/core/mixins/Fetcher.ts +0 -0
- package/src/core/utils/HTML.ts +0 -0
- package/src/core/utils/PublisherProxy.ts +1 -1
- package/src/core/utils/api.ts +0 -0
- package/src/decorators.ts +9 -2
- package/src/docs/_misc/ancestor-attribute.md +94 -0
- package/src/docs/_misc/auto-subscribe.md +199 -0
- package/src/docs/_misc/bind.md +362 -0
- package/src/docs/_misc/on-assign.md +336 -0
- package/src/docs/_misc/templates-demo.md +19 -0
- package/src/docs/example/decorators-demo.ts +658 -0
- package/src/docs/navigation/navigation.ts +22 -3
- package/src/docs/search/docs-search.json +415 -0
- package/src/docs.ts +4 -0
- package/src/tsconfig-model.json +1 -1
- package/src/tsconfig.json +22 -2
- package/src/tsconfig.tsbuildinfo +1 -1
- package/vite.config.mts +0 -2
- package/docs/assets/index-Dgl1lJQo.js +0 -4861
- package/templates-test.html +0 -32
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
# @bind
|
|
2
|
+
|
|
3
|
+
The `@bind` decorator automatically binds a class property to a path in a publisher. The property will be automatically
|
|
4
|
+
updated when the publisher's data changes.
|
|
5
|
+
|
|
6
|
+
If you need to trigger the rendering lifecycle of a LitElement, please also add the `@state()` decorator to the property.
|
|
7
|
+
|
|
8
|
+
## Principle
|
|
9
|
+
|
|
10
|
+
This decorator subscribes to a publisher via the `PublisherManager` using a path (dot notation) to access a specific property. When this property is modified in the publisher, the decorated property is automatically updated.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
### Import
|
|
15
|
+
|
|
16
|
+
<sonic-code language="typescript">
|
|
17
|
+
<template>
|
|
18
|
+
import { bind } from "@supersoniks/concorde/decorators";
|
|
19
|
+
</template>
|
|
20
|
+
</sonic-code>
|
|
21
|
+
|
|
22
|
+
### Basic example
|
|
23
|
+
|
|
24
|
+
<sonic-code language="typescript">
|
|
25
|
+
<template>
|
|
26
|
+
@customElement("demo-bind")
|
|
27
|
+
export class DemoBind extends LitElement {
|
|
28
|
+
static styles = [tailwind];
|
|
29
|
+
//
|
|
30
|
+
@bind("demoData.firstName")
|
|
31
|
+
@state()
|
|
32
|
+
firstName = "";
|
|
33
|
+
//
|
|
34
|
+
@bind("demoData.lastName")
|
|
35
|
+
@state()
|
|
36
|
+
lastName: string = "";
|
|
37
|
+
//
|
|
38
|
+
@bind("demoData.count")
|
|
39
|
+
@state()
|
|
40
|
+
count: number = 0;
|
|
41
|
+
//
|
|
42
|
+
render() {
|
|
43
|
+
return //......
|
|
44
|
+
}
|
|
45
|
+
//
|
|
46
|
+
updateData() {
|
|
47
|
+
const demoData = PublisherManager.get("demoData");
|
|
48
|
+
const demoUsers = PublisherManager.get("demoUsers");
|
|
49
|
+
const randomIndex = Math.floor(Math.random() * demoUsers.get().length);
|
|
50
|
+
const randomUser = demoUsers.get()[randomIndex];
|
|
51
|
+
demoData.set({
|
|
52
|
+
firstName: randomUser.firstName,
|
|
53
|
+
lastName: randomUser.lastName,
|
|
54
|
+
count: (demoData.count.get() || 0) + 1,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
</template>
|
|
59
|
+
</sonic-code>
|
|
60
|
+
|
|
61
|
+
<sonic-code>
|
|
62
|
+
<template>
|
|
63
|
+
<demo-bind></demo-bind>
|
|
64
|
+
</template>
|
|
65
|
+
</sonic-code>
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
## Reflect (liaison bidirectionnelle)
|
|
69
|
+
|
|
70
|
+
Lorsque vous avez besoin que les modifications locales se propagent également vers le publisher, activez l'option `reflect` :
|
|
71
|
+
|
|
72
|
+
<sonic-code language="typescript">
|
|
73
|
+
<template>
|
|
74
|
+
@bind("userData.profile.avatarUrl", { reflect: true })
|
|
75
|
+
@state()
|
|
76
|
+
avatar: string;
|
|
77
|
+
</template>
|
|
78
|
+
</sonic-code>
|
|
79
|
+
|
|
80
|
+
- `reflect: true` crée un getter/setter qui garde les descripteurs existants et synchronise la valeur avec le publisher.
|
|
81
|
+
- Les mises à jour provenant du publisher sont protégées par un flag interne afin d'éviter les boucles infinies.
|
|
82
|
+
- Toute écriture sur la propriété décorée (ex. saisie utilisateur, mise à jour durant `render`) déclenche un `publisher.set(...)`.
|
|
83
|
+
- Pratique pour implémenter des formulaires pilotés par les publishers sans écrire manuellement la logique de synchronisation.
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
<sonic-code language="typescript">
|
|
87
|
+
<template>
|
|
88
|
+
@customElement("demo-bind-reflect")
|
|
89
|
+
export class DemoBindReflect extends LitElement {
|
|
90
|
+
static styles = [tailwind];
|
|
91
|
+
//
|
|
92
|
+
@bind("bindReflectDemo.count", { reflect: true })
|
|
93
|
+
@state()
|
|
94
|
+
withReflect: number = 0;
|
|
95
|
+
//
|
|
96
|
+
@bind("bindReflectDemo.count")
|
|
97
|
+
@state()
|
|
98
|
+
withoutReflect: number = 0;
|
|
99
|
+
//
|
|
100
|
+
render() {
|
|
101
|
+
return html`
|
|
102
|
+
<div class="mb-3">
|
|
103
|
+
from publisher : ${sub("bindReflectDemo.count") || 0} <br />
|
|
104
|
+
from component with reflect : ${this.withReflect || 0} <br />
|
|
105
|
+
from component without reflect : ${this.withoutReflect || 0}
|
|
106
|
+
</div>
|
|
107
|
+
<sonic-button @click=${() => this.withReflect++}
|
|
108
|
+
>Increment with reflect</sonic-button
|
|
109
|
+
>
|
|
110
|
+
<sonic-button @click=${() => this.withoutReflect++}
|
|
111
|
+
>Increment without reflect</sonic-button
|
|
112
|
+
>
|
|
113
|
+
`;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
</template>
|
|
117
|
+
</sonic-code>
|
|
118
|
+
|
|
119
|
+
<sonic-code toggleCode>
|
|
120
|
+
<template>
|
|
121
|
+
<demo-bind-reflect></demo-bind-reflect>
|
|
122
|
+
</template>
|
|
123
|
+
</sonic-code>
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
### Complex path
|
|
127
|
+
|
|
128
|
+
<sonic-code language="typescript">
|
|
129
|
+
<template>
|
|
130
|
+
@customElement("user-profile")
|
|
131
|
+
export class UserProfile extends LitElement {
|
|
132
|
+
// Access to a simple property
|
|
133
|
+
@bind("cart")
|
|
134
|
+
@state()
|
|
135
|
+
cart: object;
|
|
136
|
+
//
|
|
137
|
+
// Access to a nested property
|
|
138
|
+
@bind("userData.profile.email")
|
|
139
|
+
@state()
|
|
140
|
+
userEmail: string;
|
|
141
|
+
//
|
|
142
|
+
// Access to an array element
|
|
143
|
+
@bind("userData.addresses.0.city")
|
|
144
|
+
@state()
|
|
145
|
+
primaryCity: string;
|
|
146
|
+
//
|
|
147
|
+
render() {
|
|
148
|
+
return html`
|
|
149
|
+
<div>
|
|
150
|
+
<p>Number of items : ${this.cart.items.length}</p>
|
|
151
|
+
<p>Email: ${this.userEmail}</p>
|
|
152
|
+
<p>City: ${this.primaryCity}</p>
|
|
153
|
+
</div>
|
|
154
|
+
`;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
</template>
|
|
158
|
+
</sonic-code>
|
|
159
|
+
|
|
160
|
+
## Path syntax
|
|
161
|
+
|
|
162
|
+
The path uses dot notation to navigate through the publisher structure:
|
|
163
|
+
|
|
164
|
+
- **First segment**: dataProvider identifier (e.g., `"myDataProvider"`)
|
|
165
|
+
- **Following segments**: nested properties (e.g., `"myDataProvider.user.profile"`)
|
|
166
|
+
- **Array access**: use numeric index (e.g., `"myDataProvider.items.0"`)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
### Dynamic path driven by class properties
|
|
171
|
+
|
|
172
|
+
You can now build the path dynamically by referencing the host class properties inside the string passed to `@bind`. Two placeholder syntaxes are supported:
|
|
173
|
+
|
|
174
|
+
- `` ${myProperty} `` or `` ${this.myProperty} ``
|
|
175
|
+
- `` {$myProperty} ``
|
|
176
|
+
|
|
177
|
+
Each placeholder is replaced at runtime with the current value of the corresponding property. `@bind` automatically watches those properties and:
|
|
178
|
+
|
|
179
|
+
- re-evaluates the final path when one of them changes,
|
|
180
|
+
- removes the previous subscription before attaching the new one,
|
|
181
|
+
- keeps working with `reflect: true`.
|
|
182
|
+
- observe the changes inside `willUpdate(changedProperties)` so nothing touche aux getters/setters.
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
<sonic-code language="typescript">
|
|
186
|
+
<template>
|
|
187
|
+
@customElement("demo-bind-dynamic")
|
|
188
|
+
export class DemoBindDynamic extends LitElement {
|
|
189
|
+
static styles = [tailwind];
|
|
190
|
+
//
|
|
191
|
+
@property({ type: String })
|
|
192
|
+
dataProvider: "demoUsers" | "demoUsersAlt" = "demoUsers";
|
|
193
|
+
//
|
|
194
|
+
@property({ type: Number })
|
|
195
|
+
userIndex: number = 0;
|
|
196
|
+
//
|
|
197
|
+
@bind("${dataProvider}.${userIndex}")
|
|
198
|
+
@state()
|
|
199
|
+
user: any = {};
|
|
200
|
+
//
|
|
201
|
+
updateUserIndex(e: Event) {
|
|
202
|
+
this.userIndex = parseInt((e.target as HTMLInputElement).value);
|
|
203
|
+
}
|
|
204
|
+
//
|
|
205
|
+
updateDataProvider(e: Event) {
|
|
206
|
+
this.dataProvider = (e.target as HTMLSelectElement).value as
|
|
207
|
+
| "demoUsers"
|
|
208
|
+
| "demoUsersAlt";
|
|
209
|
+
}
|
|
210
|
+
//
|
|
211
|
+
updateCurrentUserData() {
|
|
212
|
+
const usersPublisher = PublisherManager.get(this.dataProvider);
|
|
213
|
+
const userPublisher = Objects.traverse(
|
|
214
|
+
usersPublisher,
|
|
215
|
+
[String(this.userIndex)]
|
|
216
|
+
) as PublisherProxy;
|
|
217
|
+
|
|
218
|
+
if (userPublisher) {
|
|
219
|
+
// Générer de nouvelles données aléatoires
|
|
220
|
+
const randomNames = [
|
|
221
|
+
{ firstName: "Alice", lastName: "Wonder" },
|
|
222
|
+
{ firstName: "Bob", lastName: "Builder" },
|
|
223
|
+
{ firstName: "Charlie", lastName: "Chaplin" },
|
|
224
|
+
];
|
|
225
|
+
|
|
226
|
+
const randomName =
|
|
227
|
+
randomNames[Math.floor(Math.random() * randomNames.length)];
|
|
228
|
+
const randomEmail = `${randomName.firstName.toLowerCase()}.${randomName.lastName.toLowerCase()}@example.com`;
|
|
229
|
+
|
|
230
|
+
// Mettre à jour l'utilisateur directement
|
|
231
|
+
const currentUser = userPublisher.get() || {};
|
|
232
|
+
userPublisher.set({
|
|
233
|
+
...currentUser,
|
|
234
|
+
firstName: randomName.firstName,
|
|
235
|
+
lastName: randomName.lastName,
|
|
236
|
+
email: randomEmail,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
//
|
|
241
|
+
render() {
|
|
242
|
+
return html`
|
|
243
|
+
<div class="flex flex-col gap-2">
|
|
244
|
+
<sonic-select
|
|
245
|
+
.value=${this.dataProvider}
|
|
246
|
+
label="Users set"
|
|
247
|
+
@change=${this.updateDataProvider}
|
|
248
|
+
>
|
|
249
|
+
<option value="demoUsers">First set of users</option>
|
|
250
|
+
<option value="demoUsersAlt">Second set of users</option>
|
|
251
|
+
</sonic-select>
|
|
252
|
+
<sonic-input
|
|
253
|
+
type="number"
|
|
254
|
+
.value=${this.userIndex}
|
|
255
|
+
@input=${this.updateUserIndex}
|
|
256
|
+
min="0"
|
|
257
|
+
max="9"
|
|
258
|
+
label="Index"
|
|
259
|
+
class="block"
|
|
260
|
+
>
|
|
261
|
+
</sonic-input>
|
|
262
|
+
<sonic-button @click=${this.updateCurrentUserData}
|
|
263
|
+
>Update current user data</sonic-button
|
|
264
|
+
>
|
|
265
|
+
<div class="flex flex-col gap-2 border p-2">
|
|
266
|
+
<div>
|
|
267
|
+
<sonic-icon name="user" library="heroicons"></sonic-icon>
|
|
268
|
+
${this.user?.firstName} ${this.user?.lastName}
|
|
269
|
+
</div>
|
|
270
|
+
<div>
|
|
271
|
+
<sonic-icon name="envelope" library="heroicons"></sonic-icon>
|
|
272
|
+
${this.user?.email}
|
|
273
|
+
</div>
|
|
274
|
+
</div>
|
|
275
|
+
</div>
|
|
276
|
+
`;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
</template>
|
|
280
|
+
</sonic-code>
|
|
281
|
+
|
|
282
|
+
<sonic-code>
|
|
283
|
+
<template>
|
|
284
|
+
<demo-bind-dynamic></demo-bind-dynamic>
|
|
285
|
+
</template>
|
|
286
|
+
</sonic-code>
|
|
287
|
+
|
|
288
|
+
> ⚠️ Use a classic string literal: `@bind("${dataProvider}.${profileId}.info.title")`. Do **not** use a template literal (backticks), otherwise JavaScript would try to interpolate the value immediately.
|
|
289
|
+
>
|
|
290
|
+
|
|
291
|
+
Additional constraints:
|
|
292
|
+
|
|
293
|
+
- The hosting class must expose a `willUpdate(changedProperties?: Map<PropertyKey, unknown>)` method (LitElement already le fournit) so that `@bind` peut écouter les changements de dépendances.
|
|
294
|
+
- Dependencies need to be reactive (e.g. `@property()` on LitElement) or you must call `this.requestUpdate("myProp")` manually after changing them, otherwise `willUpdate` ne sera jamais notifié.
|
|
295
|
+
- If you use nested expressions like `${user.id}`, the first segment (`user`) is the one being observed: you need to reassign `this.user` (e.g. with a new object) so that the binding can detect the change.
|
|
296
|
+
|
|
297
|
+
## Behavior
|
|
298
|
+
|
|
299
|
+
- The property is automatically updated when the publisher's data changes
|
|
300
|
+
- Subscription happens at the time of `connectedCallback`
|
|
301
|
+
- Unsubscription happens automatically at the time of `disconnectedCallback`
|
|
302
|
+
- If the path doesn't exist yet, a publisher is created with the value `null`
|
|
303
|
+
- The value will be updated as soon as the data becomes available
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
## Use cases
|
|
307
|
+
|
|
308
|
+
This decorator is particularly useful for:
|
|
309
|
+
|
|
310
|
+
- **Binding properties** to specific data in a publisher
|
|
311
|
+
- **Accessing sub-properties** without having to manually write subscription logic
|
|
312
|
+
- **Simplifying code** by avoiding repetitive calls to `PublisherManager.get()` and `onAssign()`
|
|
313
|
+
|
|
314
|
+
## Complete example
|
|
315
|
+
|
|
316
|
+
<sonic-code language="typescript">
|
|
317
|
+
<template>
|
|
318
|
+
import { html, LitElement } from "lit";
|
|
319
|
+
import { customElement } from "lit/decorators.js";
|
|
320
|
+
import { bind } from "@supersoniks/concorde/decorators";
|
|
321
|
+
import { PublisherManager } from "@supersoniks/concorde/core/utils/PublisherProxy";
|
|
322
|
+
//
|
|
323
|
+
@customElement("product-card")
|
|
324
|
+
export class ProductCard extends LitElement {
|
|
325
|
+
@bind("productData.name")
|
|
326
|
+
@state()
|
|
327
|
+
productName: string;
|
|
328
|
+
//
|
|
329
|
+
@bind("productData.price")
|
|
330
|
+
@state()
|
|
331
|
+
price: number;
|
|
332
|
+
//
|
|
333
|
+
@bind("productData.description")
|
|
334
|
+
@state()
|
|
335
|
+
description: string;
|
|
336
|
+
//
|
|
337
|
+
render() {
|
|
338
|
+
return html`
|
|
339
|
+
<div class="product-card">
|
|
340
|
+
<h2>${this.productName}</h2>
|
|
341
|
+
<p class="price">${this.price}€</p>
|
|
342
|
+
<p>${this.description}</p>
|
|
343
|
+
</div>
|
|
344
|
+
`;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
// Somewhere in your code, update the data:
|
|
348
|
+
const productPublisher = PublisherManager.get("productData");
|
|
349
|
+
productPublisher.set({
|
|
350
|
+
name: "Example product",
|
|
351
|
+
price: 29.99,
|
|
352
|
+
description: "A product description"
|
|
353
|
+
});
|
|
354
|
+
// The component properties will be automatically updated
|
|
355
|
+
</template>
|
|
356
|
+
</sonic-code>
|
|
357
|
+
|
|
358
|
+
## Notes
|
|
359
|
+
|
|
360
|
+
- This decorator works with any component that has `connectedCallback` and `disconnectedCallback` methods (such as `LitElement` or components extending `Subscriber`)
|
|
361
|
+
- Updates are reactive: any modification to the publisher triggers a property update
|
|
362
|
+
- For more information about publishers, see the documentation on [Sharing data](#docs/_getting-started/pubsub.md/pubsub)
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
# @onAssign
|
|
2
|
+
|
|
3
|
+
The `@onAssign` decorator allows you to execute a method when one or more publishers are updated. The method is called only when all specified publishers have been assigned values.
|
|
4
|
+
|
|
5
|
+
## Principle
|
|
6
|
+
|
|
7
|
+
This decorator subscribes to one or more publishers via the `PublisherManager`. When all specified publishers have been assigned values (via `set`), the decorated method is called with all the values as arguments.
|
|
8
|
+
|
|
9
|
+
This is particularly useful when you need to wait for multiple data sources to be ready before executing logic.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Import
|
|
14
|
+
|
|
15
|
+
<sonic-code language="typescript">
|
|
16
|
+
<template>
|
|
17
|
+
import { onAssign } from "@supersoniks/concorde/decorators";
|
|
18
|
+
</template>
|
|
19
|
+
</sonic-code>
|
|
20
|
+
|
|
21
|
+
### Basic example
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
<sonic-code language="typescript">
|
|
25
|
+
<template>
|
|
26
|
+
//...
|
|
27
|
+
@customElement("demo-on-assign")
|
|
28
|
+
export class DemoOnAssign extends LitElement {
|
|
29
|
+
static styles = [tailwind];
|
|
30
|
+
//
|
|
31
|
+
@state() userWithSettings: any = null;
|
|
32
|
+
@state() isReady: boolean = false;
|
|
33
|
+
@state() lastUpdate: string = "";
|
|
34
|
+
//
|
|
35
|
+
@onAssign("demoUser", "demoUserSettings")
|
|
36
|
+
handleDataReady(user: any, settings: any) {
|
|
37
|
+
this.isReady = Object.keys(user).length > 0 && Object.keys(settings).length > 0;
|
|
38
|
+
this.userWithSettings = { ...user, ...settings };
|
|
39
|
+
this.lastUpdate = new Date().toLocaleTimeString();
|
|
40
|
+
this.requestUpdate();
|
|
41
|
+
}
|
|
42
|
+
//
|
|
43
|
+
render() {
|
|
44
|
+
const { name, email, theme, language } = this.userWithSettings;
|
|
45
|
+
return //...
|
|
46
|
+
}
|
|
47
|
+
//
|
|
48
|
+
updateData() {
|
|
49
|
+
const user = PublisherManager.get("demoUser");
|
|
50
|
+
const userSettings = PublisherManager.get("demoUserSettings");
|
|
51
|
+
const userNumber = Math.floor(Math.random() * 100);
|
|
52
|
+
user.set({
|
|
53
|
+
name: `User n°${userNumber}`,
|
|
54
|
+
email: `user-${userNumber}@example.com`,
|
|
55
|
+
});
|
|
56
|
+
//
|
|
57
|
+
userSettings.set({
|
|
58
|
+
theme: ["light", "dark", "auto"][Math.floor(Math.random() * 3)],
|
|
59
|
+
language: ["en", "fr", "es"][Math.floor(Math.random() * 3)],
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
</template>
|
|
64
|
+
</sonic-code>
|
|
65
|
+
|
|
66
|
+
<sonic-code>
|
|
67
|
+
<template>
|
|
68
|
+
<demo-on-assign></demo-on-assign>
|
|
69
|
+
</template>
|
|
70
|
+
</sonic-code>
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
### Example with nested paths
|
|
74
|
+
|
|
75
|
+
<sonic-code language="typescript">
|
|
76
|
+
<template>
|
|
77
|
+
@customElement("product-view")
|
|
78
|
+
export class ProductView extends LitElement {
|
|
79
|
+
product: any = null;
|
|
80
|
+
inventory: any = null;
|
|
81
|
+
//
|
|
82
|
+
@onAssign("store.product", "store.inventory")
|
|
83
|
+
handleProductData(product: any, inventory: any) {
|
|
84
|
+
this.product = product;
|
|
85
|
+
this.inventory = inventory;
|
|
86
|
+
this.requestUpdate();
|
|
87
|
+
}
|
|
88
|
+
//
|
|
89
|
+
render() {
|
|
90
|
+
if (!this.product) return html`<div>Loading...</div>`;
|
|
91
|
+
//
|
|
92
|
+
const stock = this.inventory[this.product.id] || 0;
|
|
93
|
+
return html`
|
|
94
|
+
<div>
|
|
95
|
+
<h2>${this.product.name}</h2>
|
|
96
|
+
<p>Price: ${this.product.price}€</p>
|
|
97
|
+
<p>Stock: ${stock}</p>
|
|
98
|
+
</div>
|
|
99
|
+
`;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
</template>
|
|
103
|
+
</sonic-code>
|
|
104
|
+
|
|
105
|
+
## Path syntax
|
|
106
|
+
|
|
107
|
+
The path uses dot notation to navigate through the publisher structure:
|
|
108
|
+
|
|
109
|
+
- **First segment**: dataProvider identifier (e.g., `"userData"`)
|
|
110
|
+
- **Following segments**: nested properties (e.g., `"store.product"`)
|
|
111
|
+
- **Array access**: use numeric index (e.g., `"data.items.0"`)
|
|
112
|
+
|
|
113
|
+
### Dynamic path driven by class properties
|
|
114
|
+
|
|
115
|
+
You can now build the paths dynamically by referencing the host class properties inside the strings passed to `@onAssign`. Two placeholder syntaxes are supported:
|
|
116
|
+
|
|
117
|
+
- `` ${myProperty} `` or `` ${this.myProperty} ``
|
|
118
|
+
- `` {$myProperty} ``
|
|
119
|
+
|
|
120
|
+
Each placeholder is replaced at runtime with the current value of the corresponding property. `@onAssign` automatically watches those properties and:
|
|
121
|
+
|
|
122
|
+
- re-evaluates the final paths when one of them changes,
|
|
123
|
+
- removes the previous subscriptions before attaching the new ones,
|
|
124
|
+
- observe the changes inside `willUpdate(changedProperties)` so nothing touches the getters/setters.
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
<sonic-code language="typescript">
|
|
128
|
+
<template>
|
|
129
|
+
@customElement("demo-on-assign-dynamic")
|
|
130
|
+
export class DemoOnAssignDynamic extends LitElement {
|
|
131
|
+
static styles = [tailwind];
|
|
132
|
+
//
|
|
133
|
+
@property({ type: String })
|
|
134
|
+
dataProvider: "demoUsers" | "demoUsersAlt" = "demoUsers";
|
|
135
|
+
//
|
|
136
|
+
@property({ type: Number })
|
|
137
|
+
userIndex: number = 0;
|
|
138
|
+
//
|
|
139
|
+
@state() user: any = null;
|
|
140
|
+
@state() userSettings: any = null;
|
|
141
|
+
|
|
142
|
+
@onAssign("${dataProvider}.${userIndex}", "${dataProvider}Settings.${userIndex}")
|
|
143
|
+
handleUserDataReady(user: any, settings: any) {
|
|
144
|
+
this.user = user;
|
|
145
|
+
this.userSettings = settings;
|
|
146
|
+
}
|
|
147
|
+
//
|
|
148
|
+
updateUserIndex(e: Event) {
|
|
149
|
+
this.userIndex = parseInt((e.target as HTMLInputElement).value);
|
|
150
|
+
}
|
|
151
|
+
//
|
|
152
|
+
updateDataProvider(e: Event) {
|
|
153
|
+
this.dataProvider = (e.target as HTMLSelectElement).value as
|
|
154
|
+
| "demoUsers"
|
|
155
|
+
| "demoUsersAlt";
|
|
156
|
+
}
|
|
157
|
+
//
|
|
158
|
+
updateCurrentUserData() {
|
|
159
|
+
const usersPublisher = PublisherManager.get(this.dataProvider);
|
|
160
|
+
const settingsPublisher = PublisherManager.get(
|
|
161
|
+
`${this.dataProvider}Settings`
|
|
162
|
+
);
|
|
163
|
+
const userPublisher = Objects.traverse(
|
|
164
|
+
usersPublisher,
|
|
165
|
+
[String(this.userIndex)]
|
|
166
|
+
) as PublisherProxy;
|
|
167
|
+
const settingPublisher = Objects.traverse(
|
|
168
|
+
settingsPublisher,
|
|
169
|
+
[String(this.userIndex)]
|
|
170
|
+
) as PublisherProxy;
|
|
171
|
+
|
|
172
|
+
if (userPublisher && settingPublisher) {
|
|
173
|
+
// Générer de nouvelles données aléatoires
|
|
174
|
+
const randomNames = [
|
|
175
|
+
{ firstName: "Alice", lastName: "Wonder" },
|
|
176
|
+
{ firstName: "Bob", lastName: "Builder" },
|
|
177
|
+
{ firstName: "Charlie", lastName: "Chaplin" },
|
|
178
|
+
];
|
|
179
|
+
const randomThemes = ["light", "dark", "auto"];
|
|
180
|
+
const randomLanguages = ["en", "fr", "es"];
|
|
181
|
+
|
|
182
|
+
const randomName =
|
|
183
|
+
randomNames[Math.floor(Math.random() * randomNames.length)];
|
|
184
|
+
const randomEmail = `${randomName.firstName.toLowerCase()}.${randomName.lastName.toLowerCase()}@example.com`;
|
|
185
|
+
const randomTheme =
|
|
186
|
+
randomThemes[Math.floor(Math.random() * randomThemes.length)];
|
|
187
|
+
const randomLanguage =
|
|
188
|
+
randomLanguages[Math.floor(Math.random() * randomLanguages.length)];
|
|
189
|
+
|
|
190
|
+
// Mettre à jour l'utilisateur directement
|
|
191
|
+
const currentUser = userPublisher.get() || {};
|
|
192
|
+
userPublisher.set({
|
|
193
|
+
...currentUser,
|
|
194
|
+
firstName: randomName.firstName,
|
|
195
|
+
lastName: randomName.lastName,
|
|
196
|
+
email: randomEmail,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// Mettre à jour les settings directement
|
|
200
|
+
settingPublisher.set({
|
|
201
|
+
theme: randomTheme,
|
|
202
|
+
language: randomLanguage,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
//
|
|
207
|
+
render() {
|
|
208
|
+
return html`
|
|
209
|
+
<div class="flex flex-col gap-2">
|
|
210
|
+
<sonic-select label="Users set" @change=${this.updateDataProvider}>
|
|
211
|
+
<option value="demoUsers">First set of users</option>
|
|
212
|
+
<option value="demoUsersAlt">Second set of users</option>
|
|
213
|
+
</sonic-select>
|
|
214
|
+
<sonic-input
|
|
215
|
+
type="number"
|
|
216
|
+
.value=${this.userIndex}
|
|
217
|
+
@input=${this.updateUserIndex}
|
|
218
|
+
min="0"
|
|
219
|
+
max="9"
|
|
220
|
+
label="Index"
|
|
221
|
+
class="block"
|
|
222
|
+
>
|
|
223
|
+
</sonic-input>
|
|
224
|
+
<sonic-button @click=${this.updateCurrentUserData}
|
|
225
|
+
>Update current user data</sonic-button
|
|
226
|
+
>
|
|
227
|
+
<div class="flex flex-col gap-2 border p-2">
|
|
228
|
+
<div>
|
|
229
|
+
<sonic-icon name="user" library="heroicons"></sonic-icon>
|
|
230
|
+
${this.user?.firstName} ${this.user?.lastName}
|
|
231
|
+
</div>
|
|
232
|
+
<div>
|
|
233
|
+
<sonic-icon name="envelope" library="heroicons"></sonic-icon>
|
|
234
|
+
${this.user?.email}
|
|
235
|
+
</div>
|
|
236
|
+
<div>
|
|
237
|
+
Theme: ${this.userSettings?.theme} | Language:
|
|
238
|
+
${this.userSettings?.language}
|
|
239
|
+
</div>
|
|
240
|
+
</div>
|
|
241
|
+
</div>
|
|
242
|
+
`;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
</template>
|
|
246
|
+
</sonic-code>
|
|
247
|
+
|
|
248
|
+
<sonic-code>
|
|
249
|
+
<template>
|
|
250
|
+
<demo-on-assign-dynamic></demo-on-assign-dynamic>
|
|
251
|
+
</template>
|
|
252
|
+
</sonic-code>
|
|
253
|
+
|
|
254
|
+
> ⚠️ Use classic string literals: `@onAssign("${dataProvider}.${profileId}", "settings.${profileId}")`. Do **not** use template literals (backticks), otherwise JavaScript would try to interpolate the value immediately.
|
|
255
|
+
|
|
256
|
+
Additional constraints:
|
|
257
|
+
|
|
258
|
+
- The hosting class must expose a `willUpdate(changedProperties?: Map<PropertyKey, unknown>)` method (LitElement already provides it) so that `@onAssign` can listen to dependency changes.
|
|
259
|
+
- Dependencies need to be reactive (e.g. `@property()` on LitElement) or you must call `this.requestUpdate("myProp")` manually after changing them, otherwise `willUpdate` will never be notified.
|
|
260
|
+
- If you use nested expressions like `${user.id}`, the first segment (`user`) is the one being observed: you need to reassign `this.user` (e.g. with a new object) so that the binding can detect the change.
|
|
261
|
+
|
|
262
|
+
## Behavior
|
|
263
|
+
|
|
264
|
+
- The method is called only when **all** specified publishers have been assigned values
|
|
265
|
+
- The method receives the values of all publishers as arguments, in the same order as specified
|
|
266
|
+
- Subscription happens at the time of `connectedCallback`
|
|
267
|
+
- Unsubscription happens automatically at the time of `disconnectedCallback`
|
|
268
|
+
- If a publisher doesn't exist yet, it will be created with the value `null`
|
|
269
|
+
- The method is called with `this` bound to the component instance
|
|
270
|
+
|
|
271
|
+
## Use cases
|
|
272
|
+
|
|
273
|
+
This decorator is particularly useful for:
|
|
274
|
+
|
|
275
|
+
- **Waiting for multiple data sources** before rendering or executing logic
|
|
276
|
+
- **Synchronizing data** from different publishers
|
|
277
|
+
- **Initializing components** that depend on multiple data providers
|
|
278
|
+
- **Handling complex data dependencies** where you need all data to be ready
|
|
279
|
+
|
|
280
|
+
## Complete example
|
|
281
|
+
|
|
282
|
+
<sonic-code language="typescript">
|
|
283
|
+
<template>
|
|
284
|
+
import { html, LitElement } from "lit";
|
|
285
|
+
import { customElement } from "lit/decorators.js";
|
|
286
|
+
import { onAssign } from "@supersoniks/concorde/decorators";
|
|
287
|
+
import { PublisherManager } from "@supersoniks/concorde/core/utils/PublisherProxy";
|
|
288
|
+
//
|
|
289
|
+
@customElement("order-summary")
|
|
290
|
+
export class OrderSummary extends LitElement {
|
|
291
|
+
order: any = null;
|
|
292
|
+
customer: any = null;
|
|
293
|
+
shipping: any = null;
|
|
294
|
+
//
|
|
295
|
+
@onAssign("orderData", "customerData", "shippingData")
|
|
296
|
+
handleOrderReady(order: any, customer: any, shipping: any) {
|
|
297
|
+
this.order = order;
|
|
298
|
+
this.customer = customer;
|
|
299
|
+
this.shipping = shipping;
|
|
300
|
+
this.requestUpdate();
|
|
301
|
+
}
|
|
302
|
+
//
|
|
303
|
+
render() {
|
|
304
|
+
if (!this.order || !this.customer || !this.shipping) {
|
|
305
|
+
return html`<div>Loading order details...</div>`;
|
|
306
|
+
}
|
|
307
|
+
//
|
|
308
|
+
return html`
|
|
309
|
+
<div class="order-summary">
|
|
310
|
+
<h2>Order #${this.order.id}</h2>
|
|
311
|
+
<p>Customer: ${this.customer.name}</p>
|
|
312
|
+
<p>Shipping to: ${this.shipping.address}</p>
|
|
313
|
+
<p>Total: ${this.order.total}€</p>
|
|
314
|
+
</div>
|
|
315
|
+
`;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
// Somewhere in your code, update the publishers:
|
|
319
|
+
const orderPub = PublisherManager.get("orderData");
|
|
320
|
+
const customerPub = PublisherManager.get("customerData");
|
|
321
|
+
const shippingPub = PublisherManager.get("shippingData");
|
|
322
|
+
// The method will be called only when all three are set:
|
|
323
|
+
orderPub.set({ id: "123", total: 99.99 });
|
|
324
|
+
customerPub.set({ name: "John Doe", email: "john@example.com" });
|
|
325
|
+
shippingPub.set({ address: "123 Main St" });
|
|
326
|
+
// handleOrderReady will be called with all three values
|
|
327
|
+
</template>
|
|
328
|
+
</sonic-code>
|
|
329
|
+
|
|
330
|
+
## Notes
|
|
331
|
+
|
|
332
|
+
- This decorator works with any component that has `connectedCallback` and `disconnectedCallback` methods (such as `LitElement` or components extending `Subscriber`)
|
|
333
|
+
- The method is called synchronously when all publishers are ready
|
|
334
|
+
- If you need to update the UI, remember to call `this.requestUpdate()` inside the method
|
|
335
|
+
- For more information about publishers, see the documentation on [Sharing data](#docs/_getting-started/pubsub.md/pubsub)
|
|
336
|
+
|