Skip to content

Commit 403693b

Browse files
committed
updateSchema param, docs
1 parent fae8041 commit 403693b

File tree

2 files changed

+124
-12
lines changed

2 files changed

+124
-12
lines changed

readme.md

Lines changed: 119 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ const encodedPatch2 = ppServer.encodePatch(patch2)
8484
### Benchmark
8585

8686
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%) |
9292

9393
Send `encodedStateWithTypes`, `encodedPatch1` and `encodedPatch2` to Client and decode them:
9494

@@ -125,9 +125,121 @@ console.log(decodedPatch2)
125125
// { op: 'replace', path: '/foo/baz', value: true }
126126
```
127127

128-
## Documentation
128+
# Documentation
129129

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...
131243

132244
## License
133245

src/patchpack.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ export class PatchPack {
2828
return notepack.encode(snapshot)
2929
}
3030

31-
public decodeState<T = any>(buffer: Buffer) {
31+
public decodeState(buffer: Buffer, updateSchema = true): any {
3232
const [encodedNode, types] = notepack.decode(buffer)
3333

3434
// apply schema types
3535
this.schema.init(types)
3636

3737
// decode snapshot
38-
return this.decodeNode(encodedNode, { key: "", index: -1 })
38+
return this.decodeNode(encodedNode, { key: "", index: -1, updateSchema })
3939
}
4040

4141
private encodeNode(value: any, meta: IBuildMeta): any {
@@ -215,7 +215,7 @@ export class PatchPack {
215215
return notepack.encode(data)
216216
}
217217

218-
public decodePatch (buffer: Buffer): IReversibleJsonPatch {
218+
public decodePatch (buffer: Buffer, updateSchema = true): IReversibleJsonPatch {
219219

220220
// encode patch
221221
const encodedPatch = notepack.decode<TSchemaPatch>(buffer)
@@ -246,9 +246,9 @@ export class PatchPack {
246246
const value = values.reverse().pop()
247247
if (parent.type === MAP_NODE && patch.op === "add") {
248248
parent.keys?.push(key as string)
249-
patch.value = this.decodeNode(value[1], { parent, key, index: propIndex })
249+
patch.value = this.decodeNode(value[1], { parent, key, index: propIndex, updateSchema })
250250
} else {
251-
patch.value = this.decodeNode(value, { parent, key, index: propIndex })
251+
patch.value = this.decodeNode(value, { parent, key, index: propIndex, updateSchema })
252252
}
253253
}
254254

0 commit comments

Comments
 (0)