From 1da1d5eb9d33db6cc0b59c1259888046aa0c3752 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Wed, 16 Apr 2025 19:42:31 +0300 Subject: Refactors state diffing which is now much improved. --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 68f984d..bbc51f8 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ const app = ShapeX({ 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"); -- cgit v1.2.3