-
-
Notifications
You must be signed in to change notification settings - Fork 18
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 #540 from streamich/extensions-3
Extensions 3
- Loading branch information
Showing
10 changed files
with
164 additions
and
40 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,34 @@ | ||
/* tslint:disable no-console */ | ||
|
||
/** | ||
* Run this demo with: | ||
* | ||
* npx nodemon -q -x ts-node src/json-crdt-extensions/cnt/__demos__/docs.ts | ||
*/ | ||
|
||
import {Model, s} from '../../../json-crdt'; | ||
import {CntExt} from '..'; | ||
|
||
console.clear(); | ||
|
||
const model = Model.withLogicalClock(1234); | ||
|
||
model.ext.register(CntExt); | ||
|
||
model.api.root({ | ||
counter: CntExt.new(1), | ||
}); | ||
console.log(model + ''); | ||
|
||
// Excess use only ... | ||
// 2-3 days for finding damages ... | ||
// .. | ||
|
||
const api = model.api.in(['counter']).asExt(CntExt); | ||
const values = api.view(); | ||
|
||
console.log(values); | ||
|
||
api.inc(10); | ||
|
||
console.log(model + ''); |
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,2 @@ | ||
export * from './mval'; | ||
export * from './cnt'; |
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,30 @@ | ||
/* tslint:disable no-console */ | ||
|
||
/** | ||
* Run this demo with: | ||
* | ||
* npx nodemon -q -x ts-node src/json-crdt-extensions/mval/__demos__/docs.ts | ||
*/ | ||
|
||
import {Model, s} from '../../../json-crdt'; | ||
import {MvalExt} from '..'; | ||
|
||
console.clear(); | ||
|
||
const model = Model.withLogicalClock(1234); | ||
|
||
model.ext.register(MvalExt); | ||
|
||
model.api.root({ | ||
score: MvalExt.new(1), | ||
}); | ||
console.log(model + ''); | ||
|
||
const api = model.api.in(['score']).asExt(MvalExt); | ||
const values = api.view(); | ||
|
||
console.log(values); | ||
|
||
api.set(s.con(2)); | ||
|
||
console.log(model + ''); |
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,28 @@ | ||
/* tslint:disable no-console */ | ||
|
||
/** | ||
* Run this demo with: | ||
* | ||
* npx nodemon -q -x ts-node src/json-crdt-extensions/mval/__demos__/view.ts | ||
*/ | ||
|
||
import {Model, s} from '../../../json-crdt'; | ||
import {MvalExt} from '..'; | ||
|
||
console.clear(); | ||
|
||
const model = Model.withLogicalClock(1234); | ||
|
||
model.ext.register(MvalExt); | ||
|
||
model.api.root(MvalExt.new(s.con(1))); | ||
|
||
console.log(''); | ||
console.log('Model with extension:'); | ||
console.log(model + ''); | ||
|
||
const model2 = Model.fromBinary(model.toBinary()); | ||
|
||
console.log(''); | ||
console.log('Model not aware of extension:'); | ||
console.log(model2 + ''); |
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
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,29 @@ | ||
# Extensions | ||
|
||
Extensions allow to create new node types out of the existing built-in types: | ||
`con`, `val`, `obj`, `vec`, `str`, `bin`, `arr`. | ||
|
||
Each extension has a globally unique ID, which is an 8-bit unsigned integer. | ||
Thus, only 256 extensions can be defined at the same time. | ||
|
||
Extensions do not modify in any shape the JSON CRDT, nor JSON CRDT Patch | ||
protocols, instead they build on top of the `vec` node type. An extension node | ||
is a `vec` node with a specific structure, and a specific interpretation of the | ||
elements of the `vec` node. | ||
|
||
An extension `vec` node follows the following structure: it is a 2-tuple, where | ||
the first element in the extension *header* and the second element is the | ||
extension *payload*. | ||
|
||
The extension *header* is a `con` node, which holds a 3 byte `Uint8Array` with | ||
the following octets: (1) the extension ID, (2) the session ID modulo 256, and | ||
(3) the time sequence modulo 256. | ||
|
||
The extension *payload* is any JSON CRDT node with any value, which is specific | ||
to the extension. | ||
|
||
``` | ||
vec | ||
├─ 0: con Uin8Array { <ext_id>, <sid_mod_256>, <time_mod_256> } | ||
└─ 1: any | ||
``` |
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