Skip to content

Commit

Permalink
Merge pull request #312 from streamich/json-crdt-patch-demos
Browse files Browse the repository at this point in the history
JSON CRDT Patch demos
  • Loading branch information
streamich authored Jul 28, 2023
2 parents 79d5048 + 862566c commit 76a9348
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 18 deletions.
21 changes: 21 additions & 0 deletions src/json-crdt-patch/__demos__/Patch.ts
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());
29 changes: 19 additions & 10 deletions src/json-crdt-patch/__demos__/PatchBuilder-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,33 @@ import {LogicalClock} from '../clock';
const clock = new LogicalClock(123, 456);
const builder = new PatchBuilder(clock);

builder.root(
builder.json({
foo: 'bar',
baz: 123,
}),
);
const json = {
foo: 'bar',
baz: 123,
bools: [true, false],
};

builder.root(builder.json(json));

const patch = builder.flush();
console.log(patch.toString());
// Patch 123.456!8

// Patch 123.456!15
// ├─ "obj" 123.456
// ├─ "str" 123.457
// ├─ "ins_str" 123.458!3, obj = 123.457 { 123.457 ← "bar" }
// ├─ "con" 123.461 { 123 }
// ├─ "ins_obj" 123.462!1, obj = 123.456
// ├─ "arr" 123.462
// ├─ "con" 123.463 { true }
// ├─ "val" 123.464 { 123.463 }
// ├─ "con" 123.465 { false }
// ├─ "val" 123.466 { 123.465 }
// ├─ "ins_arr" 123.467!2, obj = 123.462 { 123.462 ← 123.464, 123.466 }
// ├─ "ins_obj" 123.469!1, obj = 123.456
// │ ├─ "foo": 123.457
// │ └─ "baz": 123.461
// └─ "ins_val" 123.463!1, obj = 0.0, val = 123.456
// │ ├─ "baz": 123.461
// │ └─ "bools": 123.462
// └─ "ins_val" 123.470!1, obj = 0.0, val = 123.456

const buf = patch.toBinary();
console.log(buf);
Expand Down
24 changes: 24 additions & 0 deletions src/json-crdt-patch/__demos__/PatchBuilder-konst.ts
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
28 changes: 20 additions & 8 deletions src/json-crdt-patch/__demos__/PatchBuilder-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@ import {LogicalClock} from '../clock';
const clock = new LogicalClock(123, 456);
const builder = new PatchBuilder(clock);

const strId = builder.str();
builder.insStr(strId, strId, 'Hello, world!');
// Create a "str" RGA-String.
const stringId = builder.str();

builder.root(strId);
// Insert "bar" text into the string at the starting position.
builder.insStr(stringId, stringId, 'bar');

const patch = builder.flush();
console.log(patch.toString());
// Patch 123.456!15
// Create an "obj" LWW-Object.
const objectId = builder.obj();

// Set "foo" property of the object to "bar".
builder.setKeys(objectId, [['foo', stringId]]);

// Set the document root LWW-Register value to the object.
builder.root(objectId);

console.log(builder.patch + '');
// Patch 123.456!7
// ├─ "str" 123.456
// ├─ "ins_str" 123.457!13, obj = 123.456 { 123.456 ← "Hello, world!" }
// └─ "ins_val" 123.470!1, obj = 0.0, val = 123.456
// ├─ "ins_str" 123.457!3, obj = 123.456 { 123.456 ← "bar" }
// ├─ "obj" 123.460
// ├─ "ins_obj" 123.461!1, obj = 123.460
// │ └─ "foo": 123.456
// └─ "ins_val" 123.462!1, obj = 0.0, val = 123.460
24 changes: 24 additions & 0 deletions src/json-crdt-patch/__demos__/codec-binary.ts
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
// ]
26 changes: 26 additions & 0 deletions src/json-crdt-patch/__demos__/codec-compact.ts
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"],
// ]
27 changes: 27 additions & 0 deletions src/json-crdt-patch/__demos__/codec-json.ts
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" }
// ]
// }

0 comments on commit 76a9348

Please sign in to comment.