@crdt-sync/core 0.1.0 → 0.1.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 +64 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# @crdt-sync/core
|
|
2
|
+
|
|
3
|
+
It acts as a TypeScript proxy wrapper around the powerful, Rust-compiled WebAssembly (Wasm) `StateStore` engine. It handles initializing the Wasm binaries, orchestrating the state sync via CRDTs, and managing the WebSocket network connections under the hood.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @crdt-sync/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**Note:** If you are using React, we highly recommend using the magic hook adapter instead:
|
|
12
|
+
```bash
|
|
13
|
+
npm install @crdt-sync/core @crdt-sync/react
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- **Wasm-powered CRDT Engine**: Lightning fast, robust conflict resolution written in Rust.
|
|
19
|
+
- **JavaScript State Proxying**: Wraps your regular JS objects into observed CRDT proxies. Mutations (e.g., `state.x = 10`) are instantly and invisibly converted into CRDT operations and queued.
|
|
20
|
+
- **WebSocket Synchronization**: Comes with a built-in `WebSocketManager` to easily broadcast the encoded Wasm envelopes between your client and your backend server.
|
|
21
|
+
|
|
22
|
+
## Basic Usage (Vanilla JS/TS)
|
|
23
|
+
|
|
24
|
+
While using framework adapters (like React's) is recommended for DOM-based apps, you can use `@crdt-sync/core` directly in any plain JavaScript or Node.js environment:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { CrdtStateProxy, WebSocketManager } from '@crdt-sync/core';
|
|
28
|
+
// Import the Wasm initializer
|
|
29
|
+
import init, { WasmStateStore } from '@crdt-sync/core/pkg/web/crdt_sync.js';
|
|
30
|
+
|
|
31
|
+
async function main() {
|
|
32
|
+
// 1. Initialize the Wasm Module
|
|
33
|
+
await init();
|
|
34
|
+
|
|
35
|
+
// 2. Create a unique ID for this client
|
|
36
|
+
const clientId = 'client-' + Math.random().toString(36).substring(2, 11);
|
|
37
|
+
const store = new WasmStateStore(clientId);
|
|
38
|
+
|
|
39
|
+
// 3. Create the State Proxy
|
|
40
|
+
const proxy = new CrdtStateProxy(store);
|
|
41
|
+
|
|
42
|
+
// Set initial state
|
|
43
|
+
proxy.state.robot = {
|
|
44
|
+
speed: 0,
|
|
45
|
+
active: false
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// 4. Hook up to the network
|
|
49
|
+
const ws = new WebSocket('wss://api.example.com/sync');
|
|
50
|
+
const manager = new WebSocketManager(store, proxy, ws);
|
|
51
|
+
|
|
52
|
+
// 5. To mutate the state and broadcast to peers, simply mutate the proxy!
|
|
53
|
+
proxy.state.robot.speed = 42;
|
|
54
|
+
|
|
55
|
+
// Listen for incoming changes from the server
|
|
56
|
+
proxy.onUpdate(() => {
|
|
57
|
+
console.log("State updated remotely!", proxy.state);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
MIT
|