-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #312 from streamich/json-crdt-patch-demos
JSON CRDT Patch demos
- Loading branch information
Showing
7 changed files
with
161 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* tslint:disable no-console */ | ||
|
||
/** | ||
* Run this demo with: | ||
* | ||
* npx ts-node src/json-crdt-patch/__demos__/Patch.ts | ||
*/ | ||
|
||
import {InsStrOp, NewStrOp, Patch} from '..'; | ||
import {ts} from '../clock'; | ||
|
||
const patch = new Patch(); | ||
|
||
// Create a new "str" RGA-String with ID [123, 0]. | ||
patch.ops.push(new NewStrOp(ts(123, 0))); | ||
|
||
// Insert "hello" with timespan that starts from [123, 1], into an | ||
// RGA-String with ID [123, 0], at position [123, 0] (the beginning). | ||
patch.ops.push(new InsStrOp(ts(123, 1), ts(123, 0), ts(123, 0), 'hello')); | ||
|
||
console.log(patch.toString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* tslint:disable no-console */ | ||
|
||
/** | ||
* Run this demo with: | ||
* | ||
* npx ts-node src/json-crdt-patch/__demos__/PatchBuilder-konst.ts | ||
*/ | ||
|
||
import {PatchBuilder, Patch, konst} from '..'; | ||
import {LogicalClock} from '../clock'; | ||
|
||
const clock = new LogicalClock(123, 456); | ||
const builder = new PatchBuilder(clock); | ||
|
||
builder.json({ | ||
bools: konst([true, false]), | ||
}), | ||
console.log(builder.patch + ''); | ||
|
||
// Patch 123.456!3 | ||
// ├─ "obj" 123.456 | ||
// ├─ "con" 123.457 { [true,false] } | ||
// └─ "ins_obj" 123.458!1, obj = 123.456 | ||
// └─ "bools": 123.457 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* tslint:disable no-console */ | ||
|
||
/** | ||
* Run this demo with: | ||
* | ||
* npx ts-node src/json-crdt-patch/__demos__/codec-binary.ts | ||
*/ | ||
|
||
import {PatchBuilder} from '..'; | ||
import {encode} from '../codec/binary'; | ||
import {LogicalClock} from '../clock'; | ||
|
||
const clock = new LogicalClock(123, 456); | ||
const builder = new PatchBuilder(clock); | ||
|
||
builder.json('hello'); | ||
const encoded = encode(builder.patch); | ||
|
||
console.log(encoded); | ||
// Uint8Array(16) [ | ||
// 123, 1, 200, 3, 246, 4, | ||
// 172, 1, 0, 1, 0, 104, | ||
// 101, 108, 108, 111 | ||
// ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* tslint:disable no-console */ | ||
|
||
/** | ||
* Run this demo with: | ||
* | ||
* npx ts-node src/json-crdt-patch/__demos__/codec-compact.ts | ||
*/ | ||
|
||
import {PatchBuilder} from '..'; | ||
import {encode} from '../codec/compact'; | ||
import {LogicalClock} from '../clock'; | ||
|
||
const clock = new LogicalClock(123, 456); | ||
const builder = new PatchBuilder(clock); | ||
|
||
builder.json('hello'); | ||
const pojo = encode(builder.patch); | ||
|
||
console.log(JSON.stringify(pojo, null, 2)); | ||
// [ | ||
// [ | ||
// [123, 456] | ||
// ], | ||
// [4], | ||
// [12, -1, -1, "hello"], | ||
// ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* tslint:disable no-console */ | ||
|
||
/** | ||
* Run this demo with: | ||
* | ||
* npx ts-node src/json-crdt-patch/__demos__/codec-json.ts | ||
*/ | ||
|
||
import {PatchBuilder} from '..'; | ||
import {encode} from '../codec/json'; | ||
import {LogicalClock} from '../clock'; | ||
|
||
const clock = new LogicalClock(123, 456); | ||
const builder = new PatchBuilder(clock); | ||
|
||
builder.json('hello'); | ||
const pojo = encode(builder.patch); | ||
|
||
console.log(JSON.stringify(pojo, null, 2)); | ||
// { | ||
// "id": [123, 456], | ||
// "ops": [ | ||
// { "op": "str" }, | ||
// { "op": "ins_str", "obj": [123, 456], | ||
// "after": [123, 456], "value": "hello" } | ||
// ] | ||
// } |