Skip to content

Commit

Permalink
Added the native protobuf conversion functions to the upgraded messag…
Browse files Browse the repository at this point in the history
…e versions.

Now they can be naively treated as native protobuf objects
  • Loading branch information
dtracers committed May 30, 2016
1 parent be3a3fb commit e673f9e
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 45 deletions.
39 changes: 1 addition & 38 deletions src/main/js/protobufUtils/classCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports.Inherits = function(Child, Parent) {
}
this.parentClass = Parent;
};
if (!isUndefined(Parent.prototype.superConstructor)) {
if (!module.exports.isUndefined(Parent.prototype.superConstructor)) {
var parentConstructor = Parent.prototype.superConstructor;
var localConstructor = undefined;
/**
Expand All @@ -73,40 +73,3 @@ module.exports.Inherits = function(Child, Parent) {
};
}
};

/**
* Decodes the data and preserves the bytebuffer for later use.
*
* @param {ArrayBuffer} data
* a compiled set of data in the protobuf object.
* @param {Message} proto - The protobuf object that is being decoded.
* @param {Function} [onError] - A callback that is called when an error occurs regarding marking and resetting.
* (optional). This will be called before the result is returned
*
* @return {Message | undefined} decoded protobuf object. (This may return undefined)
*/
module.exports.decode = function(data, proto, onError) {
if (isUndefined(data) || data === null || typeof data !== 'object') {
throw new ProtobufException('Data type is not supported:' + typeof data);
}
try {
data.mark();
} catch (exception) {
if (onError) {
onError(exception);
}
}
var decoded = undefined;

var protoClass = proto;

decoded = protoClass.decode(data);
try {
data.reset();
} catch (exception) {
if (onError) {
onError(exception);
}
}
return decoded;
};
45 changes: 41 additions & 4 deletions src/main/js/protobufUtils/sketchProtoConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,43 @@
var SrlShape = undefined;
var modulesLoaded = false;

/**
* Decodes the data and preserves the bytebuffer for later use.
*
* @param {ArrayBuffer} data
* a compiled set of data in the protobuf object.
* @param {Message} proto - The protobuf object that is being decoded.
* @param {Function} [onError] - A callback that is called when an error occurs regarding marking and resetting.
* (optional). This will be called before the result is returned
*
* @return {Message | undefined} decoded protobuf object. (This may return undefined)
*/
var decode = function(data, proto, onError) {
if (module.exports.isUndefined(data) || data === null || typeof data !== 'object') {
throw 'Data type is not supported:' + typeof data;
}
try {
data.mark();
} catch (exception) {
if (onError) {
onError(exception);
}
}
var decoded = undefined;

var protoClass = proto;

decoded = protoClass.decode(data);
try {
data.reset();
} catch (exception) {
if (onError) {
onError(exception);
}
}
return decoded;
};

function loadModules() {
if (protobufUtils.isUndefined(SrlPoint)) {
SrlPoint = require('./../sketchLibrary/SrlPoint');
Expand All @@ -42,15 +79,14 @@
var objectType = object.type;
switch (objectType) {
case ObjectType.SHAPE:
return SrlShape.createFromProtobuf(protobufUtils.decode(object.object, ShapeMessage));
return SrlShape.createFromProtobuf(decode(object.object, ShapeMessage));
case ObjectType.STROKE:
return SrlStroke.createFromProtobuf(protobufUtils.decode(object.object, StrokeMessage));
return SrlStroke.createFromProtobuf(decode(object.object, StrokeMessage));
case ObjectType.POINT:
return SrlPoint.createFromProtobuf(protobufUtils.decode(object.object, PointMessage));
return SrlPoint.createFromProtobuf(decode(object.object, PointMessage));
}
};


/**
* Used locally to encode an SRL_Object into its protobuf type.
*
Expand Down Expand Up @@ -97,6 +133,7 @@
}
};

module.exports.decode = decode;
module.exports.encodeSrlObject = encodeSrlObject;
module.exports.decodeSrlObject = decodeSrlObject;
module.exports.convertToUpgradedSketchObject = convertToUpgradedSketchObject;
Expand Down
19 changes: 19 additions & 0 deletions src/main/js/sketchLibrary/SrlPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,5 +341,24 @@
return proto;
};

/**
* Converts an array buffer to an upgraded SrlPoint.
*
* @param {ArrayBuffer} data
* @return {SrlPoint}
*/
SrlPoint.decode = function (data) {
return SrlPoint.createFromProtobuf(objectConversionUtils.decode(data, PointMessage));
};

/**
* Creates a byte version of the protobuf data.
*
* @return {ArrayBuffer}
*/
SrlPoint.prototype.toArrayBuffer = function () {
return this.sendToProtobuf().toArrayBuffer();
};

module.exports = SrlPoint;
})(module);
18 changes: 18 additions & 0 deletions src/main/js/sketchLibrary/SrlShape.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,23 @@
return proto;
};

/**
* Converts an array buffer to an upgraded SrlShape
* @param {ArrayBuffer} data
* @return {SrlShape}
*/
SrlShape.decode = function (data) {
return SrlShape.createFromProtobuf(objectConversionUtils.decode(data, ShapeMessage));
};

/**
* Creates a byte version of the protobuf data.
*
* @return {ArrayBuffer}
*/
SrlShape.prototype.toArrayBuffer = function () {
return this.sendToProtobuf().toArrayBuffer();
};

module.exports = SrlShape;
})(module);
18 changes: 18 additions & 0 deletions src/main/js/sketchLibrary/SrlStroke.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,5 +557,23 @@
};


/**
* Converts an array buffer to an upgraded SrlStroke
* @param {ArrayBuffer} data
* @return {SrlStroke}
*/
SrlStroke.decode = function (data) {
return SrlStroke.createFromProtobuf(objectConversionUtils.decode(data, StrokeMessage));
};

/**
* Creates a byte version of the protobuf data.
*
* @return {ArrayBuffer}
*/
SrlStroke.prototype.toArrayBuffer = function () {
return this.sendToProtobuf().toArrayBuffer();
};

module.exports = SrlStroke;
})(module);
14 changes: 14 additions & 0 deletions src/test/js/sketchLibrary/SrlShapeTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var assert = require('chai').assert;

var basePath = '../../../';
var srcPath = 'main/js/';
var SrlShape = require(basePath + srcPath + 'sketchLibrary/SrlShape');

describe('Shape Tests', function () {
describe('initializations', function () {
it('should be able to create an instance of the shape class', function () {
console.log(SrlShape);
var sketch = new SrlShape();
});
});
});
6 changes: 3 additions & 3 deletions src/test/js/sketchLibrary/SrlSketchTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ var assert = require('chai').assert;

var basePath = '../../../';
var srcPath = 'main/js/';
var SketchClass = require(basePath + srcPath + 'sketchLibrary/SrlSketch');
var SrlSketch = require(basePath + srcPath + 'sketchLibrary/SrlSketch');

describe('Sketch Tests', function () {
describe('initializations', function () {
it('should be able to create an instance of the sketch class', function () {
console.log(SketchClass);
var sketch = new SketchClass();
console.log(SrlSketch);
var sketch = new SrlSketch();
});
});
});

0 comments on commit e673f9e

Please sign in to comment.