diff options
| author | Asko Nõmm <asko@nmm.ee> | 2025-04-16 19:42:31 +0300 |
|---|---|---|
| committer | Asko Nõmm <asko@nmm.ee> | 2025-04-16 19:42:31 +0300 |
| commit | 1da1d5eb9d33db6cc0b59c1259888046aa0c3752 (patch) | |
| tree | 53ac6808310ea886428608f64e0d621e98f2cf55 /README.md | |
| parent | e12ca91f863f9c1fddd50924b2c555a5d528cd44 (diff) | |
Refactors state diffing which is now much improved.
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 10 |
1 files changed, 5 insertions, 5 deletions
@@ -17,7 +17,7 @@ const app = ShapeX<AppState>({ counter: 1, }); -app.subscribe("$counter", (state) => { +app.subscribe("$.counter", (state) => { console.log("counter changed", state); return { @@ -112,21 +112,21 @@ when the event was dispatched. Subscription callbacks must return an `Response` You can also listen to state changes with subscriptions, which will fire when the listened state changes. You can listen to state changes like so: ```typescript -app.subscribe("$counter", (state) => { +app.subscribe("$.counter", (state) => { return { state, }; }); ``` -Notable difference here is the `$` prefix in the subscription listener name, which tells ShapeX what state to look for. Here `$counter` will look for the root-level `counter` key in state. To look for nested state, simply add a dot (`.`) followed by the key name, i.e: `$counter.nestedKey`. Additionally, state change subscriptions do not get any additional data passed to them, only state. +Notable difference here is the `$.` prefix in the subscription listener name, which tells ShapeX what state to look for. Here `$.counter` will look for the root-level `counter` key in state. To look for nested state, simply add a dot (`.`) followed by the key name, i.e: `$.counter.nestedKey`. Additionally, state change subscriptions do not get any additional data passed to them, only state. #### Subscribe only once If you want to subscribe to an event or state change only once, you can use the `subscribeOnce` method. This method works similarly to `subscribe`, but it will automatically unsubscribe after the first event or state change. ```typescript -app.subscribeOnce("$counter", (state) => { +app.subscribeOnce("$.counter", (state) => { return { state, }; @@ -211,7 +211,7 @@ If you want to get the number of subscriptions for a specific event or state cha ```typescript // State change subscriptions -app.subscriptionCount("$counter"); +app.subscriptionCount("$.counter"); // Event subscriptions app.subscriptionCount("some-event-name"); |
