2016-05-10 04:40:53 +00:00
|
|
|
"use strict";
|
|
|
|
// Wire protocol representation of events and actions
|
|
|
|
|
2016-05-10 04:57:05 +00:00
|
|
|
var Trie = require('./trie.js');
|
|
|
|
var Patch = require('./patch.js');
|
|
|
|
var Struct = require('./struct.js');
|
2016-05-10 04:40:53 +00:00
|
|
|
|
|
|
|
function _encode(e) {
|
|
|
|
switch (e.type) {
|
|
|
|
case "stateChange":
|
|
|
|
return ["patch", e.patch.toJSON()];
|
|
|
|
case "message":
|
|
|
|
return ["message", e.message];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _decode(what) {
|
|
|
|
return function (j) {
|
|
|
|
switch (j[0]) {
|
|
|
|
case "patch":
|
|
|
|
return Syndicate.stateChange(Patch.fromJSON(j[1]));
|
|
|
|
case "message":
|
|
|
|
return Syndicate.message(j[1]);
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid JSON-encoded " + what + ": " + JSON.stringify(j));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-05-10 04:57:05 +00:00
|
|
|
module.exports.encodeEvent = _encode;
|
|
|
|
module.exports.decodeEvent = _decode("event");
|
|
|
|
module.exports.encodeAction = _encode;
|
|
|
|
module.exports.decodeAction = _decode("action");
|