This is an implementation of the Erlang's DVV on TypeScript.
It is used in distributed systems, where UTC timestamp is unreliable value for object's version control.
- Creating a new version
const dvv = new Dvv();
const modified_utc = Date.now().toString(); // timestamp value is used for example only. It can be anything.
const new_dvv: Clock = dvv.new_dvv(modified_utc);
const dot: Clock = dvv.create(new_dvv, 'user_id_1');- Incrementing version
const context: Vector = dvv.join(dot);
const modified_utc2 = Date.now().toString();
const new_with_history: Clock = dvv.new_with_history(context, modified_utc2);
const new_dot: Clock = dvv.update(new_with_history, dot, 'user_id_2');
dvv.sync([dot, new_dot]);- Detecting conflicts
Conflict is situation when two branches of the history exist. It could happen when someone updates old version ( casual history ).
const merged_history: Clock = dvv.sync([OldVersion as Clock, NewVersion as Clock]);
const values = dvv.values(merged_history);
values.length > 1
? console.log('Conflict detected')
: console.log('Ok');- User 1 uploads file to the server, specifying version vector:
const dvv = new Dvv();
const modified_utc = Date.now().toString();
const new_dvv: Clock = dvv.new_dvv(modified_utc);
const dot: Clock = dvv.create(new_dvv, 'user_id_1');- Server checks version on a subject of conflict. Then it stores file with version information and provides it to User 2.
const merged_history: Clock = dvv.sync([ExistingVersion as Clock, UploadedVersion as Clock]);
const values = dvv.values(merged_history);
values.length > 1
? console.log('409 Conflict detected')
: console.log('200 OK') // Casual history is linear- User 2 downloads file, edits it and increments its version, before uploading back to server.
const context: Vector = dvv.join(dot as Clock); // dot is a downloaded version
const modified_utc = Date.now().toString();
const new_with_history: Clock = dvv.new_with_history(context, modified_utc);
const new_dot: Clock = dvv.update(new_with_history, dot, 'user_id_2');
dvv.sync([dot, new_dot]);