feat(plugin-bridge): add shared storage #165
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Adds a shared storage API to
@rozenite/plugin-bridgethat enables plugins to synchronize state between the DevTools panel (host) and the React Native app (device). The storage persists data in the browser's localStorage on the host side and automatically synchronizes with connected devices.Context
This feature was implemented using a factory pattern that returns different implementations for the host (DevTools panel) and device (React Native app):
Host implementation: Acts as the source of truth, loads from and saves to
localStoragewith the key formatrozenite-storage-${pluginId}. When a device connects, it either adopts the device's defaults (if no persisted data exists) or sends the persisted data to the device.Device implementation: Starts with provided defaults, sends an
rozenite-storage-initmessage upon connection, and updates its in-memory state when receivingrozenite-storage-syncorrozenite-storage-updatemessages from the host.The communication protocol uses three custom event types:
rozenite-storage-init: Device → Host (includes default values)rozenite-storage-sync: Host → Device (full state synchronization)rozenite-storage-update: Host → Device (individual key updates)The React hook (
useRozeniteSharedStorage) supports two modes:nulluntil first sync completes (prevents UI flickering for critical data)Testing