Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MV-Register extension #530

Merged
merged 7 commits into from
Mar 4, 2024
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
Loading