You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/shadow-objects/docs/01-concepts/04-entity-tree-context-events.md
+68-52Lines changed: 68 additions & 52 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,32 +9,35 @@ This section explores the hierarchical nature of the Shadow World, explaining ho
9
9
At the core of the Shadow World is the **Entity Tree**. This tree structure mirrors the hierarchy of your View components (e.g., the DOM structure of `<shae-ent>` elements).
10
10
11
11
### Hierarchy
12
-
***Entities (Puppets):** These are the nodes in the tree. Every `<shae-ent>` in your HTML corresponds to one Entity instance in the Shadow World.
13
-
***Shadow Objects (Logic):** Shadow Objects are **attached** to these Entities.
12
+
13
+
-**Entities (Puppets):** These are the nodes in the tree. Every `<shae-ent>` in your HTML corresponds to one Entity instance in the Shadow World.
14
+
-**Shadow Objects (Logic):** Shadow Objects are **attached** to these Entities.
14
15
15
16
> [!IMPORTANT]
16
17
> **Shadow Objects are NOT nodes in the tree.**
17
18
> They are "components" or "behaviors" attached to an Entity node.
18
19
>
19
-
> * An Entity can have multiple Shadow Objects (via Routing).
20
-
> * All Shadow Objects on the same Entity share the same properties and lifecycle.
20
+
> - An Entity can have multiple Shadow Objects (via Routing).
21
+
> - All Shadow Objects on the same Entity share the same properties and lifecycle.
21
22
22
23
## Context (Dependency Injection)
23
24
24
25
Context allows you to share data deep into the component tree without passing props manually at every level ("prop drilling").
25
26
26
27
### How it Works
28
+
27
29
1.**Provider:** A Shadow Object on an Entity calls `provideContext`.
28
30
2.**Scope:** This value becomes available to:
29
-
***All other Shadow Objects on the same Entity.**
30
-
***All Shadow Objects on descendant (child) Entities.**
31
+
-**All other Shadow Objects on the same Entity.**
32
+
-**All Shadow Objects on descendant (child) Entities.**
31
33
3.**Consumer:** A Shadow Object calls `useContext` to read the value.
32
34
33
35
### Context is Entity-Bound
36
+
34
37
Since Context is attached to the **Entity**, it acts as a shared bus for all logic attached to that node.
35
38
36
-
* If `ShadowObject A` provides a context, `ShadowObject B` (on the same Entity) can immediately consume it.
37
-
* This is the primary way to compose complex logic from smaller, reusable Shadow Objects.
39
+
- If `ShadowObject A` provides a context, `ShadowObject B` (on the same Entity) can immediately consume it.
40
+
- This is the primary way to compose complex logic from smaller, reusable Shadow Objects.
38
41
39
42
```mermaid
40
43
graph TD
@@ -51,70 +54,78 @@ graph TD
51
54
```
52
55
53
56
### Reactivity
57
+
54
58
Context values are **Signals**.
55
-
* If the Provider updates the value, all Consumers (even deep in the tree) update automatically.
56
-
* You don't need to subscribe manually; just reading the value in an effect creates a dependency.
59
+
60
+
- If the Provider updates the value, all Consumers (even deep in the tree) update automatically.
61
+
- You don't need to subscribe manually; just reading the value in an effect creates a dependency.
57
62
58
63
## Events
59
64
60
65
The framework provides a powerful event system based on [@spearwolf/eventize](https://github.com/spearwolf/eventize). This allows decoupled communication between Shadow Objects, Entities, and the View.
61
66
62
67
### The Event Bus: The Entity
68
+
63
69
Every Entity acts as an event emitter. Since multiple Shadow Objects can be attached to the same Entity, the Entity serves as a shared "bus" for them.
64
70
65
-
***Shared Scope:** If one Shadow Object emits an event on its Entity, **all other Shadow Objects on that same Entity** can receive it.
66
-
***Decoupling:** Shadow Objects don't need to know about each other; they just listen to the Entity they are attached to.
71
+
-**Shared Scope:** If one Shadow Object emits an event on its Entity, **all other Shadow Objects on that same Entity** can receive it.
72
+
-**Decoupling:** Shadow Objects don't need to know about each other; they just listen to the Entity they are attached to.
67
73
68
74
### 1. View -> Shadow Events
75
+
69
76
Events dispatched by the View Component are automatically forwarded to the corresponding Entity as _View Events_.
### 3. Broadcasting to Children (Traverse the Entity Tree)
118
+
108
119
You can broadcast events to all descendant Entities using the `traverse` helper. This is useful for "global" updates like a frame tick or a resize event.
109
120
110
121
```typescript
111
-
import {emit} from '@spearwolf/eventize';
122
+
import {emit} from'@spearwolf/eventize';
112
123
113
-
export function StageController({entity, on}: ShadowObjectCreationAPI) {
> **Forwarding to DOM:** If you are using `<shae-ent>`, you can automatically forward these events to the DOM element using the `forward-custom-events` attribute. See the [Web Components API](../03-api/04-web-components.md#forward-custom-events) for details.
0 commit comments