Skip to content

Commit

Permalink
Merge pull request #530 from streamich/extensions-2
Browse files Browse the repository at this point in the history
Extensions 2
  • Loading branch information
streamich committed Mar 4, 2024
2 parents 882cc97 + 9de74c8 commit cd57c74
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/json-crdt-extensions/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const enum ExtensionId {
MvValue = 0,
mval = 0,
Peritext = 1,
QuillDelta = 2,
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ export class ValueMv implements ExtensionJsonNode, Printable {
// ---------------------------------------------------------------- Printable

public toString(tab?: string): string {
return this.constructor.name + printTree(tab, [(tab) => this.data.toString(tab)]);
return this.name() + printTree(tab, [(tab) => this.data.toString(tab)]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class ValueMvApi extends NodeApi<ValueMv> implements ExtensionApi<ValueMv
const rgaApi = new ArrApi(node.data, api);
const length = rgaApi.length();
rgaApi.del(0, length);
rgaApi.ins(0, [builder.constOrJson(json)]);
rgaApi.ins(0, [builder.json(json)]);
rgaApi.node.removeTombstones();
return this;
}
Expand Down
46 changes: 46 additions & 0 deletions src/json-crdt-extensions/mval/__demos__/usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* tslint:disable no-console */

/**
* Run this demo with:
*
* npx nodemon -q -x ts-node src/json-crdt-extensions/mval/__demos__/usage.ts
*/

import {Model, s} from '../../../json-crdt';
import {ValueMvExt} from '..';

console.clear();

const model = Model.withLogicalClock(1234);

model.ext.register(ValueMvExt);

model.api.root({
obj: {
mv: ValueMvExt.new(s.con(1)),
},
});

console.log('');
console.log('Initial value:');
console.log(model + '');

const api = model.api.in(['obj', 'mv']).asExt(ValueMvExt);

api.set(s.con(5));

console.log('');
console.log('After update:');
console.log(model + '');

const model2 = model.fork();

const api2 = model2.api.in(['obj', 'mv']).asExt(ValueMvExt);

api.set(s.con(10));
api2.set(s.con(20));
model.applyPatch(model2.api.flush());

console.log('');
console.log('After two users update concurrently:');
console.log(model + '');
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import {ValueMvExt} from '..';
import {konst} from '../../../json-crdt-patch/builder/Konst';
import {Model} from '../../../json-crdt/model';

test('can specify extension name', () => {
expect(ValueMvExt.name).toBe('mval');
});

test('can create a new multi-value register', () => {
const model = Model.withLogicalClock();
model.ext.register(ValueMvExt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import type {ArrNode} from '../../json-crdt/nodes/arr/ArrNode';
import type {ExtensionDefinition} from '../../json-crdt';

export const ValueMvExt: ExtensionDefinition<ArrNode, ValueMv, ValueMvApi> = {
id: ExtensionId.MvValue,
id: ExtensionId.mval,
name: 'mval',
new: (value: unknown | ITimestampStruct) =>
ext(
ExtensionId.MvValue,
ExtensionId.mval,
delayed((builder) => builder.jsonArr([value])),
),
Node: ValueMv,
Expand Down
2 changes: 1 addition & 1 deletion src/json-crdt/extensions/Extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Extensions implements Printable {
this.constructor.name +
printTree(
tab,
keys.map((k) => (tab) => `${k}: ${this.ext[k].Node.name}`),
keys.map((k) => (tab) => `${k}: ${this.ext[k].name}`),
)
);
}
Expand Down
1 change: 1 addition & 0 deletions src/json-crdt/extensions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface ExtensionDefinition<
EApi extends ExtensionApi<ENode> = ExtensionApi<ENode>,
> {
id: number;
name: string;
new: (...args: any[]) => NodeBuilder;
Node: new (data: Node) => ENode;
Api: new (node: ENode, api: ModelApi) => EApi;
Expand Down

0 comments on commit cd57c74

Please sign in to comment.