shared-state-bridge 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -70
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,14 +1,41 @@
|
|
|
1
|
-
# shared-state-bridge
|
|
1
|
+
# Share React State Across Packages in Turborepo & Nx Monorepos | shared-state-bridge
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Share state between packages in a React monorepo without prop drilling, context threading, or Redux boilerplate. `shared-state-bridge` is a cross-package state store for Turborepo, Nx, and Lerna monorepos -- with React 18 hooks, WebSocket real-time sync, and optional localStorage/AsyncStorage persistence.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
- **
|
|
8
|
-
- **
|
|
9
|
-
- **
|
|
10
|
-
- **
|
|
11
|
-
-
|
|
5
|
+
> Monorepo state management for React -- create a named store in one package, access it by name from any other. ~1.2 KB core, zero dependencies, full TypeScript inference.
|
|
6
|
+
|
|
7
|
+
- **Share React state across packages** -- named bridges resolve via a global registry, accessible from any package in your monorepo
|
|
8
|
+
- **React 18 hooks with selector optimization** -- `useSyncExternalStore` with fine-grained re-render control
|
|
9
|
+
- **Persist state to localStorage or AsyncStorage** -- built-in adapters for web and React Native, or write your own
|
|
10
|
+
- **Real-time WebSocket state sync between apps** -- sync state across Next.js, React Native, and any other connected client
|
|
11
|
+
- **~1.2 KB gzipped core, zero dependencies** -- tree-shakeable entry points for React hooks, persistence, and sync
|
|
12
|
+
- **Full TypeScript inference and safety** -- typed `setState`, typed selectors, typed plugins
|
|
13
|
+
- **Extensible plugin system** -- persistence, sync, logging, validation, or your own custom plugins
|
|
14
|
+
|
|
15
|
+
## Why Use a Cross-Package State Store in a Monorepo?
|
|
16
|
+
|
|
17
|
+
In Turborepo, Nx, and Lerna monorepos, multiple packages often need to share runtime state -- authentication tokens, user preferences, feature flags, real-time collaboration data. The typical workarounds all have costs: lifting state into a shared package creates tight coupling, duplicating stores across packages leads to drift, and wiring up Redux or Zustand across package boundaries requires manual plumbing that breaks as the monorepo scales.
|
|
18
|
+
|
|
19
|
+
`shared-state-bridge` solves this with a named global registry. Call `createBridge('auth', initialState)` in one package, call `getBridge('auth')` from any other -- same store instance, same state, no imports needed beyond the type. The registry uses `Symbol.for` internally, so it works even when multiple copies of the library exist in `node_modules` (a common monorepo pitfall).
|
|
20
|
+
|
|
21
|
+
### How shared-state-bridge Compares
|
|
22
|
+
|
|
23
|
+
| Feature | shared-state-bridge | Zustand | Redux Toolkit | Jotai |
|
|
24
|
+
|---|---|---|---|---|
|
|
25
|
+
| Cross-package registry | Yes (named bridges) | No | No | No |
|
|
26
|
+
| Monorepo / Turborepo / Nx | First-class | Manual wiring | Manual wiring | Manual wiring |
|
|
27
|
+
| React 18 `useSyncExternalStore` | Yes | Yes | Yes | Yes |
|
|
28
|
+
| Real-time WebSocket sync | Built-in plugin | No | No | No |
|
|
29
|
+
| Persistence plugin | Built-in | Middleware | Redux Persist | No |
|
|
30
|
+
| Bundle size (full) | ~5.1 KB gzipped | ~1 KB | ~11 KB | ~3 KB |
|
|
31
|
+
| TypeScript-first | Yes | Yes | Yes | Yes |
|
|
32
|
+
| Dependencies | Zero | Zero | Several | Zero |
|
|
33
|
+
|
|
34
|
+
**vs Zustand:** Zustand is a general-purpose state manager for a single package. `shared-state-bridge` adds a named global registry so any package in the monorepo can call `getBridge('app')` and get the same store instance — no prop drilling, no manual exports. If you only need state within one package, Zustand is a great choice.
|
|
35
|
+
|
|
36
|
+
**vs Redux / Redux Toolkit:** Redux works well for large apps with complex update logic. `shared-state-bridge` trades Redux's reducer boilerplate for a simpler `setState` API, and adds cross-package and WebSocket sync out of the box at a fraction of the bundle cost.
|
|
37
|
+
|
|
38
|
+
**vs Jotai:** Jotai's atom model is elegant for fine-grained reactivity within a package. `shared-state-bridge` targets a different problem: sharing a named, structured state object across package boundaries in a monorepo, with optional real-time sync.
|
|
12
39
|
|
|
13
40
|
---
|
|
14
41
|
|
|
@@ -66,7 +93,6 @@ Lightweight shared state bridge for monorepo apps. Sync state across packages in
|
|
|
66
93
|
- [Sync Flow](#sync-flow)
|
|
67
94
|
- [Gotchas and Common Pitfalls](#gotchas-and-common-pitfalls)
|
|
68
95
|
- [FAQ](#faq)
|
|
69
|
-
- [Contributing](#contributing)
|
|
70
96
|
- [Support](#support)
|
|
71
97
|
- [License](#license)
|
|
72
98
|
|
|
@@ -1895,66 +1921,6 @@ State persists locally AND syncs in real-time with other connected apps.
|
|
|
1895
1921
|
|
|
1896
1922
|
---
|
|
1897
1923
|
|
|
1898
|
-
## Contributing
|
|
1899
|
-
|
|
1900
|
-
Contributions are welcome! Here's how to get started:
|
|
1901
|
-
|
|
1902
|
-
```bash
|
|
1903
|
-
# Clone the repo
|
|
1904
|
-
git clone https://github.com/your-username/shared-state-bridge.git
|
|
1905
|
-
cd shared-state-bridge
|
|
1906
|
-
|
|
1907
|
-
# Install dependencies
|
|
1908
|
-
npm install
|
|
1909
|
-
|
|
1910
|
-
# Run tests
|
|
1911
|
-
npm test
|
|
1912
|
-
|
|
1913
|
-
# Run tests in watch mode
|
|
1914
|
-
npm run test:watch
|
|
1915
|
-
|
|
1916
|
-
# Type check
|
|
1917
|
-
npm run typecheck
|
|
1918
|
-
|
|
1919
|
-
# Build
|
|
1920
|
-
npm run build
|
|
1921
|
-
```
|
|
1922
|
-
|
|
1923
|
-
### Project Structure
|
|
1924
|
-
|
|
1925
|
-
```
|
|
1926
|
-
src/
|
|
1927
|
-
├── index.ts # Core entry point
|
|
1928
|
-
├── core/
|
|
1929
|
-
│ ├── types.ts # All TypeScript type definitions
|
|
1930
|
-
│ ├── utils.ts # shallowEqual, throttle, isFunction
|
|
1931
|
-
│ ├── registry.ts # Global bridge registry
|
|
1932
|
-
│ └── bridge.ts # createBridge + Bridge store logic
|
|
1933
|
-
├── react/
|
|
1934
|
-
│ ├── index.ts # React entry point
|
|
1935
|
-
│ ├── context.ts # React context
|
|
1936
|
-
│ ├── provider.tsx # BridgeProvider component
|
|
1937
|
-
│ └── hooks.ts # useBridgeState, useBridge
|
|
1938
|
-
├── persist/
|
|
1939
|
-
│ ├── index.ts # Persist entry point
|
|
1940
|
-
│ ├── plugin.ts # Persistence plugin
|
|
1941
|
-
│ └── adapters.ts # Storage adapters
|
|
1942
|
-
└── sync/
|
|
1943
|
-
├── index.ts # Sync entry point
|
|
1944
|
-
├── types.ts # SyncOptions, wire protocol types
|
|
1945
|
-
├── connection.ts # WebSocket manager (reconnect, buffer)
|
|
1946
|
-
└── plugin.ts # sync() plugin factory
|
|
1947
|
-
```
|
|
1948
|
-
|
|
1949
|
-
### Guidelines
|
|
1950
|
-
|
|
1951
|
-
- Keep the core dependency-free
|
|
1952
|
-
- Maintain 100% TypeScript strict mode compliance
|
|
1953
|
-
- Write tests for all new features
|
|
1954
|
-
- Keep bundle sizes minimal — every byte counts
|
|
1955
|
-
|
|
1956
|
-
---
|
|
1957
|
-
|
|
1958
1924
|
## Support
|
|
1959
1925
|
|
|
1960
1926
|
If you find this package useful, consider buying me a coffee!
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "shared-state-bridge",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Lightweight shared state bridge for monorepo apps — sync state across packages with TypeScript, React hooks, and optional persistence",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|