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

Adding presence to ot-json0 #25

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions lib/json0.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,63 @@ function convertToText(c) {
delete c.o;
}

// not checking anything here, we should probably check that u: exists
// (only thing we care about at json0 top level), and then delegate
// to any subtypes if there is already subtype presence data
json.createPresence = function(presence) {
return presence;
};

// this needs more thinking/testing, looking a bit more carefully at
// how this is implemented in ot-rich-text, etc.
json.comparePresence = function(pres1, pres2) {
if (!pres1 || !pres2) {
return false;
}
if (!pres1.p || !pres2.p) {
return false;
}
if (pres1.t !== pres2.t) {
return false;
}
if (pres1.t && subtypes[pres1.t]) {
if (pres1.p[0] === pres2.p[0]) {
return subtypes[pres1.t].comparePresence(pres1, pres2);
}
} else return pres1 === pres2;
};

// this is the key function, always run client-side, both on
// the client that creates a text-change, and on the clients
// that receive text-changes (ops). if there are no ops, just
// return presence, if there are ops, delegate to the subtype
// responsible for those ops (currently only ot-rich-text).
// I am making assumptions many places that all ops will be
// of the same subtype, not sure if this is a given.
// We're only concerned about the first level of object/array,
// not sure if the spec allows nesting of subtypes.
json.transformPresence = function(presence, op, isOwn) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thrilled to see this development!

It would be awesome if these functions were covered by tests.

Copy link

@curran curran Apr 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The next step would be to handle transformation of nested structures recursively.

Also, it would be interesting to implement presence for text0, since it's embedded within json0.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it would be interesting to implement presence for text0, since it's embedded within json0.

if (op.length < 1) {
return presence;
}
const representativeOp = op[0];
const opType = op[0].t;
const path = representativeOp.p && representativeOp.p[0]
if (opType && subtypes[opType] && path) {
if (!presence.p || !presence.p[0] || presence.p[0] !== path) {
return presence
}
// return result of running the subtype's transformPresence,
// but add path and type, which the subtype will not include
presence = {
...subtypes[opType].transformPresence(presence, op, isOwn),
p: op[0].p,
t: op[0].t
};
}
return presence;
};

json.apply = function(snapshot, op) {
json.checkValidOp(op);

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ot-json0",
"version": "1.1.0",
"name": "@houshuang/ot-json0",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems out of place for this PR.

"version": "1.2.0",
"description": "JSON OT type",
"main": "lib/index.js",
"directories": {
Expand All @@ -17,7 +17,7 @@
},
"repository": {
"type": "git",
"url": "git://github.com/ottypes/json0"
"url": "git://github.com/houshuang/json0"
},
"keywords": [
"ot",
Expand Down