@@ -84,11 +84,11 @@ const encodedPatch2 = ppServer.encodePatch(patch2)
84
84
### Benchmark
85
85
86
86
Benchmark for encoded object size (byte):
87
- | | PatchPack | [ MessagePack] ( https://msgpack.org/ ) | JSON.stringify |
88
- | ------ | --------- | ----------- | ------------- |
89
- | state | 60 | 107 (+78%) | 165 (+175%) |
90
- | patch1 | 22 | 53 (+140%) | 72 (+227%) |
91
- | patch2 | 5 | 33 (+560%) | 47 (+840%) |
87
+ | | PatchPack | [ MessagePack] ( https://msgpack.org/ ) | JSON.stringify |
88
+ | ------ | --------- | ----------------------------------- | - ------------- |
89
+ | state | 60 | 107 (+78%) | 165 (+175%) |
90
+ | patch1 | 22 | 53 (+140%) | 72 (+227%) |
91
+ | patch2 | 5 | 33 (+560%) | 47 (+840%) |
92
92
93
93
Send ` encodedStateWithTypes ` , ` encodedPatch1 ` and ` encodedPatch2 ` to Client and decode them:
94
94
@@ -125,9 +125,121 @@ console.log(decodedPatch2)
125
125
// { op: 'replace', path: '/foo/baz', value: true }
126
126
```
127
127
128
- ## Documentation
128
+ # Documentation
129
129
130
- Documentation and specification will be soon...
130
+ ## Patchpack
131
+
132
+ ### constructor
133
+ Return instance of PatchPack with defined schema types
134
+ ``` ts
135
+ constructor (types ?: { [type : string ]: string [] | Type < any > })
136
+ ```
137
+
138
+ Types can be defined in 2 ways:
139
+ - array of properties
140
+ - Class name
141
+
142
+ Example:
143
+ ``` ts
144
+ class User {
145
+ constructor (public name : string ) {}
146
+ }
147
+
148
+ class Item {
149
+ constructor (public id : number ) {}
150
+ }
151
+
152
+ const state = {
153
+ users: [ new User (" John" ), new User (" Santa" ) ]
154
+ item : new Item (123 )
155
+ }
156
+
157
+ const pp = new PatchPack ({
158
+ State: [" users" , " item" ],
159
+ User ,
160
+ Item ,
161
+ })
162
+ ```
163
+ ### encodeState
164
+ Encode state and return in binary format
165
+ ``` ts
166
+ encodeState (state : any , includeTypes = true , updateSchema = true ): Buffer
167
+ ```
168
+
169
+ If parameter ` includeTypes = false ` used, decoder instance of PatchPack must be created with the same types.
170
+
171
+ First time state encoding must be with ` updateSchema = true ` . If you need to encode state with the same schema second time ` updateSchema ` can be set as ` false ` .
172
+
173
+ ### decodeState
174
+ Decode state from binary format to object
175
+ ``` ts
176
+ decodeState (buffer : Buffer , updateSchema = true ): any
177
+ ```
178
+
179
+ First time state decoding must be with ` updateSchema = true ` . If you need to decode the same state second time ` updateSchema ` must be set as ` false ` .
180
+
181
+ Example:
182
+ ``` ts
183
+ const pp = new PatchPack ()
184
+ const state = pp .decodeState (encodedStatewWithTypes )
185
+ ```
186
+
187
+ ### encodePatch
188
+ Encode JsonPatch and return in binary format
189
+ ``` ts
190
+ encodePatch (patch : IReversibleJsonPatch , updateSchema = true ): Buffer
191
+ ```
192
+
193
+ First time patch encoding must be with ` updateSchema = true ` . If you need to encode the same patch second time ` updateSchema ` must be set as ` false ` .
194
+
195
+ The following JsonPatch operation are supported:
196
+ - add
197
+ - replace
198
+ - remove
199
+
200
+ ReversibleJsonPatch with ` oldValue ` is supported
201
+
202
+ Example:
203
+ ``` ts
204
+ // JsonPatch
205
+ const p1 = pp .encodePatch ({
206
+ op: " replace" ,
207
+ path: " /a/b/c" ,
208
+ value: " 100" ,
209
+ })
210
+
211
+ // ReversibleJsonPatch
212
+ const p1 = pp .encodePatch ({
213
+ op: " replace" ,
214
+ path: " /a/b/c" ,
215
+ value: " 100" ,
216
+ oldValue: " 99" ,
217
+ })
218
+ ```
219
+
220
+ ### decodePatch
221
+ Decode patch from binary format to JsonPatch (or ReversibleJsonPatch).
222
+ ``` ts
223
+ decodePatch (buffer : Buffer , updateSchema = true ): IReversibleJsonPatch
224
+ ```
225
+
226
+ First time patch decoding must be with ` updateSchema = true ` . If you need to decode the same patch second time ` updateSchema ` must be set as ` false ` .
227
+
228
+ ### PatchPack.encode
229
+ Encode object to binary with last MessagePack specification.
230
+ ``` ts
231
+ static encode (value : any ): Buffer
232
+ ```
233
+
234
+ ### PatchPack.decode
235
+ Decode binary to object with last MessagePack specification.
236
+ ``` ts
237
+ static decode (buffer : Buffer ): any
238
+ ```
239
+
240
+ ## Specification
241
+
242
+ will be soon...
131
243
132
244
## License
133
245
0 commit comments