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
Add reactive localDevice and isActivated to push hooks
usePushActivation now returns a reactive localDevice (LocalDevice | null)
that initialises from localStorage and updates on activate/deactivate.
usePush now exposes isActivated, derived from a shared store, enabling
components to guard subscription calls without extra wiring. Updated
docs and tests accordingly.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The `localDevice` property is reactive — it updates when `activate()` or `deactivate()` is called. It is also initialised from `localStorage` on mount, so if the device was activated in a prior session, `localDevice` will be populated immediately.
72
+
70
73
#### Activation lifecycle
71
74
72
75
Activation registers the device with Ably's push service (on web, this requests browser notification permission and registers a service worker). The device identity is persisted to `localStorage`, so:
@@ -79,19 +82,17 @@ A typical pattern is to call `activate()` once in response to a user action (e.g
{!isActivated &&<p>Push must be activated before subscribing.</p>}
129
135
</div>
130
136
);
131
137
};
132
138
```
133
139
140
+
#### Activation awareness
141
+
142
+
`usePush` is aware of whether push has been activated via `usePushActivation`. The `isActivated` property is reactive — when `usePushActivation` calls `activate()` or `deactivate()`, all `usePush` instances update automatically, even if they are in different components. This works via a shared store without requiring any additional providers.
143
+
134
144
> [!IMPORTANT]
135
-
> The device must be activated (via `usePushActivation`) before calling `subscribeDevice()` or `unsubscribeDevice()`. See [Error Handling](#error-handling) for details on what happens if activation hasn't been completed.
145
+
> The device must be activated (via `usePushActivation`) before calling `subscribeDevice()` or `unsubscribeDevice()`. Use `isActivated` to guard your UI or check before calling. See [Error Handling](#error-handling) for details on what happens if activation hasn't been completed.
136
146
137
147
#### Subscribe by device or by client
138
148
@@ -202,7 +212,18 @@ If you call `subscribeDevice()` or `unsubscribeDevice()` before the device has b
202
212
Error: Cannot subscribe from client without deviceIdentityToken (code: 50000)
203
213
```
204
214
205
-
Ensure `activate()` has completed successfully before subscribing:
215
+
The recommended way to prevent this is to use the `isActivated` flag from `usePush` to guard your UI:
|`activate`|`() => Promise<void>`| Activates the device for push notifications. Persists to `localStorage`. |
370
+
|`deactivate`|`() => Promise<void>`| Deactivates the device and removes the registration from Ably's servers. |
371
+
|`localDevice`|`Ably.LocalDevice\|null`| The current device if activated, `null` otherwise. Reactive — updates on activate/deactivate and is initialised from persisted state. |
0 commit comments