rhine-var 0.5.2 → 0.7.0

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.
Files changed (76) hide show
  1. package/README.md +224 -197
  2. package/README_zh.md +243 -216
  3. package/dist/config/config.d.ts +8 -0
  4. package/dist/config/config.d.ts.map +1 -0
  5. package/dist/config/config.js +19 -0
  6. package/dist/core/connector/WebsocketRhineConnector.d.ts +28 -0
  7. package/dist/core/connector/WebsocketRhineConnector.d.ts.map +1 -0
  8. package/dist/core/connector/WebsocketRhineConnector.js +119 -0
  9. package/dist/core/connector/connector-status.enum.d.ts +6 -0
  10. package/dist/core/connector/connector-status.enum.d.ts.map +1 -0
  11. package/dist/core/connector/connector-status.enum.js +9 -0
  12. package/dist/core/connector/connector.abstract.d.ts +27 -0
  13. package/dist/core/connector/connector.abstract.d.ts.map +1 -0
  14. package/dist/core/connector/connector.abstract.js +62 -0
  15. package/dist/core/connector/create-connector.d.ts +5 -0
  16. package/dist/core/connector/create-connector.d.ts.map +1 -0
  17. package/dist/core/connector/create-connector.js +25 -0
  18. package/dist/core/connector/hocuspocus/hocuspocus-connector.class.d.ts +10 -0
  19. package/dist/core/connector/hocuspocus/hocuspocus-connector.class.d.ts.map +1 -0
  20. package/dist/core/connector/hocuspocus/hocuspocus-connector.class.js +78 -0
  21. package/dist/core/connector/websocket/sync-handshake-check.class.d.ts +5 -0
  22. package/dist/core/connector/websocket/sync-handshake-check.class.d.ts.map +1 -0
  23. package/dist/core/connector/websocket/sync-handshake-check.class.js +39 -0
  24. package/dist/core/connector/websocket/websocket-connector.class.d.ts +10 -0
  25. package/dist/core/connector/websocket/websocket-connector.class.d.ts.map +1 -0
  26. package/dist/core/connector/websocket/websocket-connector.class.js +74 -0
  27. package/dist/core/event/Callback.d.ts +4 -4
  28. package/dist/core/event/Callback.d.ts.map +1 -1
  29. package/dist/core/event/change-type.enum.d.ts +7 -0
  30. package/dist/core/event/change-type.enum.d.ts.map +1 -0
  31. package/dist/core/event/change-type.enum.js +10 -0
  32. package/dist/core/native/native.type.d.ts +5 -0
  33. package/dist/core/native/native.type.d.ts.map +1 -0
  34. package/dist/core/native/native.type.js +2 -0
  35. package/dist/core/native/native.utils.d.ts +9 -0
  36. package/dist/core/native/native.utils.d.ts.map +1 -0
  37. package/dist/core/native/native.utils.js +141 -0
  38. package/dist/core/proxy/Proxy.d.ts +7 -9
  39. package/dist/core/proxy/Proxy.d.ts.map +1 -1
  40. package/dist/core/proxy/Proxy.js +44 -66
  41. package/dist/core/proxy/RhineVarItem.d.ts.map +1 -1
  42. package/dist/core/proxy/RhineVarItem.js +0 -1
  43. package/dist/core/proxy/proxied-rhine-var.type.d.ts +13 -0
  44. package/dist/core/proxy/proxied-rhine-var.type.d.ts.map +1 -0
  45. package/dist/core/proxy/proxied-rhine-var.type.js +2 -0
  46. package/dist/core/proxy/rhine-var-item.class.d.ts +47 -0
  47. package/dist/core/proxy/rhine-var-item.class.d.ts.map +1 -0
  48. package/dist/core/proxy/rhine-var-item.class.js +315 -0
  49. package/dist/core/proxy/rhine-var.class.d.ts +8 -0
  50. package/dist/core/proxy/rhine-var.class.d.ts.map +1 -0
  51. package/dist/core/proxy/rhine-var.class.js +19 -0
  52. package/dist/core/support/array/rhine-var-array.class.d.ts +4 -0
  53. package/dist/core/support/array/rhine-var-array.class.d.ts.map +1 -0
  54. package/dist/core/support/array/rhine-var-array.class.js +5 -0
  55. package/dist/core/utils/convert-property.utils.d.ts +21 -0
  56. package/dist/core/utils/convert-property.utils.d.ts.map +1 -0
  57. package/dist/core/utils/convert-property.utils.js +348 -0
  58. package/dist/core/utils/data.utils.d.ts +9 -0
  59. package/dist/core/utils/data.utils.d.ts.map +1 -0
  60. package/dist/core/utils/data.utils.js +45 -0
  61. package/dist/index.d.ts +9 -8
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.js +16 -12
  64. package/dist/react/hooks/use-rhine.hook.d.ts +3 -0
  65. package/dist/react/hooks/use-rhine.hook.d.ts.map +1 -0
  66. package/dist/react/hooks/use-rhine.hook.js +19 -0
  67. package/dist/react/hooks/use-synced.hook.d.ts +4 -0
  68. package/dist/react/hooks/use-synced.hook.d.ts.map +1 -0
  69. package/dist/react/hooks/use-synced.hook.js +32 -0
  70. package/dist/utils/logger.d.ts +3 -0
  71. package/dist/utils/logger.d.ts.map +1 -0
  72. package/dist/utils/logger.js +16 -0
  73. package/package.json +70 -68
  74. package/playground/next-app/next.config.mjs +4 -4
  75. package/playground/next-app/package.json +17 -17
  76. package/playground/next-app/public/favicon.ico +0 -0
package/README.md CHANGED
@@ -1,197 +1,224 @@
1
- <div align="center">
2
-
3
- # RHINE-VAR: Simplest and Powerful CRDT Library
4
-
5
- Rhine Variable — A self-synchronizing variable for collaboration.
6
-
7
-
8
- [English](README.md) &nbsp; | &nbsp; [中文](README_zh.md)
9
-
10
- <img src='./assets/images/example_counter.png' style="border-radius: 10px"/>
11
-
12
- </div>
13
-
14
- ```typescript jsx
15
- const state = rhineProxy({count: 0}, 'room-0') // Create
16
- function Counter() {
17
- const snap = useRhine(state) // Hook for React
18
- return <div>
19
- <span>{snap.count}</span> // Automatic Update
20
- <button onClick={() => state.count-- }> -1 </button> // Operate Anyway
21
- <button onClick={() => state.count++ }> +1 </button>
22
- </div>
23
- }
24
- ```
25
-
26
- Try: [https://rv.rhineai.com/examples/counter](https://rv.rhineai.com/examples/counter)
27
-
28
- <br/>
29
-
30
- ## Overview
31
-
32
- **Document:** [LEARN.md](assets/documents/LEARN.md)
33
-
34
- **Github:** [https://github.com/RhineAI-Lab/rhine-var](https://github.com/RhineAI-Lab/rhine-var)
35
-
36
- **Npm:** [https://www.npmjs.com/package/rhine-var](https://www.npmjs.com/package/rhine-var)
37
-
38
- <br/>
39
-
40
- ## Why Choose RHINE-VAR ?
41
-
42
- <div style="height: 6px"></div>
43
-
44
- ### Concise and Efficient Syntax Design
45
- RhineVar draws inspiration from the design philosophy of the Valtio state management library, `significantly reducing the learning curve`. Allowing developers to handle data seamlessly, as if `working with standard variables`.
46
-
47
- ### Comprehensive Type Support
48
- RhineVar offers full type hints and checks, ensuring `Precise Code Completion` and `Static Analysis` during development. This not only improves the development experience but also reduces potential errors, making it especially `suitable for TypeScript projects`, enhancing code safety and reliability.
49
-
50
- ### Distributed Real-time Collaboration Algorithm
51
- The underlying collaboration algorithm of RhineVar is `Powered By the Robust Yjs Library`. Using the CRDT (Conflict-free Replicated Data Type) algorithm, it ensures eventual consistency even in multi-user and offline environments.
52
-
53
- ### Almost No Structural Complexity Limitations
54
- There is almost no upper limit. It can be an `Extremely Complex and Large Data Structure` to accommodate all the data for a large project. But at the very least, it is `a JavaScript Object`.
55
-
56
- <br/>
57
- <details>
58
- <summary><b>More Advantages of RHINE-VAR</b></summary>
59
-
60
- ### High Performance with Low Bandwidth Requirements
61
- The data synchronization and conflict resolution mechanism is highly efficient. Leveraging Yjs's `Incremental Update Mechanism`, only necessary data changes are transmitted rather than the entire document, making it ideal for bandwidth-constrained environments and reducing unnecessary data transfers.
62
-
63
- ### Strong Offline Support
64
- Users can continue to work even while offline. Once reconnected, all changes are automatically synchronized, ensuring `no data is lost or conflicted`. This is crucial for building offline-first applications.
65
-
66
- ### Cross-platform and Framework Agnostic
67
- RhineVar can be used in `All JavaScript Environments`, including browsers, Node.js, and other JavaScript platforms. It integrates with multiple frontend frameworks and libraries such as Next.js, React, Vue.js, ProseMirror, and more.
68
-
69
- ### Lightweight and Extensible
70
- RhineVar is a highly lightweight library, with its core package `only a few KB` in size, making it suitable for various frontend applications. Its modular architecture supports feature extensions, allowing developers to import or develop custom modules as needed.
71
-
72
- ### Decentralized Architecture
73
- With a decentralized architecture, collaborative editing becomes more scalable, efficient, and fault-tolerant. Peer-to-peer data transfer is supported without relying on a central server (currently under development).
74
-
75
- ### Native Yjs Support
76
- RhineVar offers full support for native Yjs object operations, providing lower-level, richer API support. `Direct operations on Yjs objects automatically trigger updates in RhineVar`.
77
-
78
- ### More Friendly and Complete Event System
79
- RhineVar offers an extensive event subscription and listening system with `Intuitive Data Change Events`. It also supports deep data change monitoring within objects, catering to a wide range of use cases.
80
-
81
- ### Fully Open Source
82
- This is a fully open-source project, licensed under the `Apache-2.0 license` on GitHub. You are `Free to use it for both Commercial and Non-commercial Projects`, and it allows modification and distribution, as long as the original copyright notice is retained.
83
-
84
- </details>
85
-
86
- <br/>
87
-
88
- ## Contact Us
89
- Welcome to join our WeChat group for communication. We look forward to having more community members participate in the creation of rhine-var.
90
-
91
- WeChat: [FNA-04]()
92
-
93
- Email: [RhineAILab@gmail.com](rhineailab@gmail.com) & [RhineAI@163.com](RhineAI@163.com)
94
-
95
- <br/>
96
-
97
- ## Install
98
- ```bash
99
- yarn add rhine-var
100
- ```
101
- If you don't have `yarn`, you can install it via `npm i rhine-var`, or install `yarn` first using `npm i -g yarn` and then use the command above to install.
102
-
103
- <br/>
104
-
105
- ## Usage
106
-
107
- ```typescript jsx
108
- const defaultValue = {value: 0}
109
- const url = 'ws://localhost:6600/room-0'
110
-
111
- const state = rhineProxy(defaultValue, url)
112
-
113
- function Counter() {
114
-
115
- const snap = useRhine(state)
116
-
117
- return <div>
118
- <button onClick={() => state.count-- }> -1 </button>
119
- <span>{snap.count}</span>
120
- <button onClick={() => state.count++ }> +1 </button>
121
- </div>
122
- }
123
- ```
124
-
125
- ### Room ID
126
-
127
- A room number corresponds to a state variable, and users who join the room will participate in collaborative activities.
128
- It supports using your own server and even other connection protocols.
129
-
130
- ### Default Value
131
-
132
- When the room does not exist on the server, a default value will be used to create the room. If not connected to the server, data from the default value will also be returned.
133
-
134
- ### rhineProxy
135
-
136
- Create a `RhineVar Object` that anyone in the room can directly modify, and the value will be synchronized to everyone real-time.
137
-
138
- Its data structure `can be quite complex`, but `at least`, it is `an object` in JavaScript.
139
-
140
- ### useRhine
141
-
142
- A hook for use with React. It creates a snapshot of a `RhineVar Object`, and whenever someone modifies this value, the information will be `updated on everyone's screen` in real-time.
143
-
144
- Note: The returned snapshot is read-only. Please do not perform any operations on it! The snapshot is only for reading data within React's XML. For all other operations (such as assignment or subscription), please operate on the original RhineVar object.
145
-
146
- <br/>
147
-
148
- #### [Click to view the full document: LEARN.md](assets/documents/LEARN.md)
149
-
150
-
151
- <br/>
152
-
153
- ## Server
154
-
155
- We provide a public server accessible over the internet for trying out and testing RHINE-VAR. You can connect to it via wss://rwq.rhineai.com/<room-id>.
156
-
157
- Please note that this server does not guarantee security and performance and may impose certain restrictions on users with large-scale usage.
158
-
159
- <br/>
160
-
161
- We provide a simple server as a reference, located at `/test/server` in this project. The server is fully compatible with all Yjs websocket servers.
162
- ```
163
- git clone https://github.com/RhineAI-Lab/rhine-var.git
164
- cd test/server
165
- yarn install
166
- yarn start
167
- ```
168
- It will run on `Port 6600`, and you can connect to it via `ws://localhost:6600/<room-id>`. `<room-id>` can be any text, with each room ID corresponding to a `RhineVariable`.
169
-
170
- More information about server develop: [https://docs.yjs.dev/ecosystem/connection-provider/y-websocket](https://docs.yjs.dev/ecosystem/connection-provider/y-websocket)
171
-
172
- <br/>
173
-
174
- ## Develop
175
-
176
- ### Preparation (Only needed for the first-time setup)
177
- ```bash
178
- # Install dependencies
179
- yarn install
180
- # Install dependencies for the NextJs environment debugging project
181
- yarn run install-next
182
- # Link this library to the NextJs environment debugging project
183
- yarn run link-next
184
- ```
185
-
186
- ### Start Debugging
187
- ```bash
188
- # Enable real-time TypeScript compilation
189
- yarn run watch
190
- # Start the temporary local server, default port is 6600
191
- yarn run server
192
- # Start the NextJs environment debugging project, default port is 6700
193
- yarn run next
194
- # The browser will navigate to http://localhost:6700
195
- ```
196
-
197
- <br/>
1
+ <div align="center">
2
+
3
+ # RHINE-VAR: Simplest and Powerful CRDT Library
4
+
5
+ Rhine Variable — A self-synchronizing variable for collaboration.
6
+
7
+
8
+ [English](README.md) &nbsp; | &nbsp; [中文](README_zh.md)
9
+
10
+ <img src='./assets/images/example_counter.png' style="border-radius: 10px"/>
11
+
12
+ </div>
13
+
14
+ ```typescript jsx
15
+ const state = rhineProxy({count: 0}, 'room-0') // Create
16
+ function Counter() {
17
+ const snap = useRhine(state) // Hook for React
18
+ return <div>
19
+ <span>{snap.count}</span> // Automatic Update
20
+ <button onClick={() => state.count-- }> -1 </button> // Operate Anyway
21
+ <button onClick={() => state.count++ }> +1 </button>
22
+ </div>
23
+ }
24
+ ```
25
+
26
+ Try: [https://rv.rhineai.com/examples/counter](https://rv.rhineai.com/examples/counter)
27
+
28
+ <br/>
29
+
30
+ ## Overview
31
+
32
+ **Document:** [LEARN.md](assets/documents/LEARN.md)
33
+
34
+ **Github:** [https://github.com/RhineAI-Lab/rhine-var](https://github.com/RhineAI-Lab/rhine-var)
35
+
36
+ **Npm:** [https://www.npmjs.com/package/rhine-var](https://www.npmjs.com/package/rhine-var)
37
+
38
+ <br/>
39
+
40
+ ## Why Choose RHINE-VAR ?
41
+
42
+ <div style="height: 6px"></div>
43
+
44
+ ### Concise and Efficient Syntax Design
45
+ RhineVar draws inspiration from the design philosophy of the Valtio state management library, `significantly reducing the learning curve`. Allowing developers to handle data seamlessly, as if `working with standard variables`.
46
+
47
+ ### Comprehensive Type Support
48
+ RhineVar offers full type hints and checks, ensuring `Precise Code Completion` and `Static Analysis` during development. This not only improves the development experience but also reduces potential errors, making it especially `suitable for TypeScript projects`, enhancing code safety and reliability.
49
+
50
+ ### Distributed Real-time Collaboration Algorithm
51
+ The underlying collaboration algorithm of RhineVar is `Powered By the Robust Yjs Library`. Using the CRDT (Conflict-free Replicated Data Type) algorithm, it ensures eventual consistency even in multi-user and offline environments.
52
+
53
+ ### Almost No Structural Complexity Limitations
54
+ There is almost no upper limit. It can be an `Extremely Complex and Large Data Structure` to accommodate all the data for a large project. But at the very least, it is `a JavaScript Object`.
55
+
56
+ <br/>
57
+ <details>
58
+ <summary><b>More Advantages of RHINE-VAR</b></summary>
59
+
60
+ ### High Performance with Low Bandwidth Requirements
61
+ The data synchronization and conflict resolution mechanism is highly efficient. Leveraging Yjs's `Incremental Update Mechanism`, only necessary data changes are transmitted rather than the entire document, making it ideal for bandwidth-constrained environments and reducing unnecessary data transfers.
62
+
63
+ ### Strong Offline Support
64
+ Users can continue to work even while offline. Once reconnected, all changes are automatically synchronized, ensuring `no data is lost or conflicted`. This is crucial for building offline-first applications.
65
+
66
+ ### Cross-platform and Framework Agnostic
67
+ RhineVar can be used in `All JavaScript Environments`, including browsers, Node.js, and other JavaScript platforms. It integrates with multiple frontend frameworks and libraries such as Next.js, React, Vue.js, ProseMirror, and more.
68
+
69
+ ### Lightweight and Extensible
70
+ RhineVar is a highly lightweight library, with its core package `only a few KB` in size, making it suitable for various frontend applications. Its modular architecture supports feature extensions, allowing developers to import or develop custom modules as needed.
71
+
72
+ ### Decentralized Architecture
73
+ With a decentralized architecture, collaborative editing becomes more scalable, efficient, and fault-tolerant. Peer-to-peer data transfer is supported without relying on a central server (currently under development).
74
+
75
+ ### Native Yjs Support
76
+ RhineVar offers full support for native Yjs object operations, providing lower-level, richer API support. `Direct operations on Yjs objects automatically trigger updates in RhineVar`.
77
+
78
+ ### More Friendly and Complete Event System
79
+ RhineVar offers an extensive event subscription and listening system with `Intuitive Data Change Events`. It also supports deep data change monitoring within objects, catering to a wide range of use cases.
80
+
81
+ ### Fully Open Source
82
+ This is a fully open-source project, licensed under the `Apache-2.0 license` on GitHub. You are `Free to use it for both Commercial and Non-commercial Projects`, and it allows modification and distribution, as long as the original copyright notice is retained.
83
+
84
+ </details>
85
+
86
+ <br/>
87
+
88
+ ## Contact Us
89
+ Welcome to join our WeChat group for communication. We look forward to having more community members participate in the creation of rhine-var.
90
+
91
+ WeChat: [FNA-04]()
92
+
93
+ Email: [RhineAILab@gmail.com](rhineailab@gmail.com) & [RhineAI@163.com](RhineAI@163.com)
94
+
95
+ <br/>
96
+
97
+ ## Install
98
+ ```bash
99
+ npm i rhine-var
100
+ ```
101
+ Or using Yarn:
102
+ ```bash
103
+ yarn add rhine-var
104
+ ```
105
+
106
+ <br/>
107
+
108
+ ## Usage
109
+
110
+ ```typescript jsx
111
+ const defaultValue = {value: 0}
112
+ const url = 'ws://localhost:6600/room-0'
113
+
114
+ const state = rhineProxy(defaultValue, url)
115
+
116
+ function Counter() {
117
+
118
+ const snap = useRhine(state)
119
+
120
+ return <div>
121
+ <button onClick={() => state.count-- }> -1 </button>
122
+ <span>{snap.count}</span>
123
+ <button onClick={() => state.count++ }> +1 </button>
124
+ </div>
125
+ }
126
+ ```
127
+
128
+ ### Room ID
129
+
130
+ A room number corresponds to a state variable, and users who join the room will participate in collaborative activities.
131
+ It supports using your own server and even other connection protocols.
132
+
133
+ ### Default Value
134
+
135
+ When the room does not exist on the server, a default value will be used to create the room. If not connected to the server, data from the default value will also be returned.
136
+
137
+ ### rhineProxy
138
+
139
+ Create a `RhineVar Object` that anyone in the room can directly modify, and the value will be synchronized to everyone real-time.
140
+
141
+ Its data structure `can be quite complex`, but `at least`, it is `an object` in JavaScript.
142
+
143
+ ### useRhine
144
+
145
+ A hook for use with React. It creates a snapshot of a `RhineVar Object`, and whenever someone modifies this value, the information will be `updated on everyone's screen` in real-time.
146
+
147
+ Note: The returned snapshot is read-only. Please do not perform any operations on it! The snapshot is only for reading data within React's XML. For all other operations (such as assignment or subscription), please operate on the original RhineVar object.
148
+
149
+ <br/>
150
+
151
+ #### [Click to view the full document: LEARN.md](assets/documents/LEARN.md)
152
+
153
+
154
+ <br/>
155
+
156
+
157
+ ## Server
158
+
159
+ We provide a `free public server` for `RHINE-VAR`, which can be used for trial and testing. You can connect to it via [wss://rvp.rhineai.com/<room-id>](wss://rvp.rhineai.com/<room-id>).
160
+
161
+ Note that this server cannot guarantee security and stability and will limit users and IPs in a large-scale manner.
162
+
163
+ <br/>
164
+
165
+ RHINE-VAR supports custom servers and connection agreements. The server currently fully supports all Yjs and WebSocket servers.
166
+
167
+ We use Tiptap to set up [Hocuspocus](https://tiptap.dev/docs/hocuspocus/introduction) server.
168
+
169
+ He has other servers, but it requires a high-quality Yjs WebSocket server. Based on the broadcasting and performance, you can support up to 1 million users' experience.
170
+
171
+ ```bash
172
+ npm i @hocuspocus/server @hocuspocus/extension-logger @hocuspocus/extension-sqlite y-protocols yjs
173
+ ```
174
+ ```typescript
175
+ import { Hocuspocus } from '@hocuspocus/server'
176
+ import { Logger } from '@hocuspocus/extension-logger'
177
+ import { SQLite } from '@hocuspocus/extension-sqlite'
178
+
179
+ const server = new Hocuspocus({
180
+ name: 'rhine-var-server',
181
+ port: 11600,
182
+ extensions: [
183
+ new Logger(),
184
+ new SQLite({database: 'db.sqlite'}),
185
+ ],
186
+ })
187
+ server.listen()
188
+ ```
189
+
190
+ We also provide a more complete [Hocuspocus](https://tiptap.dev/docs/hocuspocus/introduction) server (link below), which supports pre-authorization for connections and the ability to store data in a database.
191
+
192
+ Best Implementation: [https://github.com/RhineAI-Lab/rhine-var-hocuspocus-server](https://github.com/RhineAI-Lab/rhine-var-hocuspocus-server)
193
+
194
+ <br/>
195
+
196
+ Original server-side development reference: [https://docs.yjs.dev/ecosystem/connection-provider/y-websocket](https://docs.yjs.dev/ecosystem/connection-provider/y-websocket)
197
+
198
+ And we also provide the original Yjs WebSocket server example: [https://github.com/RhineAI-Lab/rhine-var-server](https://github.com/RhineAI-Lab/rhine-var-server)
199
+
200
+
201
+ ## Develop
202
+
203
+ ### Preparation (Only needed for the first-time setup)
204
+ ```bash
205
+ # Install dependencies
206
+ yarn install
207
+ # Install dependencies for the NextJs environment debugging project
208
+ yarn run install-next
209
+ # Link this library to the NextJs environment debugging project
210
+ yarn run link-next
211
+ ```
212
+
213
+ ### Start Debugging
214
+ ```bash
215
+ # Enable real-time TypeScript compilation
216
+ yarn run watch
217
+ # Start the temporary local server, default port is 6600
218
+ yarn run server
219
+ # Start the NextJs environment debugging project, default port is 6700
220
+ yarn run playground
221
+ # The browser will navigate to http://localhost:6700
222
+ ```
223
+
224
+ <br/>