Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Commit

Permalink
Refactoring/clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
tnajdek committed Sep 24, 2015
1 parent 95f43f6 commit e9355bf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 101 deletions.
36 changes: 36 additions & 0 deletions src/js/accessors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import utf8 from '../bower_components/utf8/utf8.js';

export function getEnumAccessors(msgkey) {
return {
get: function() {
return this.Cls.reverseEnums[msgkey][this.raw[msgkey]];
},
set: function(newValue) {
this.raw[msgkey] = this.Cls.enums[msgkey][newValue];
}
};
}

export function getStringAccessors(msgkey) {
return {
get: function() {
return utf8.decode(this.raw[msgkey]);
},
set: function(newValue) {
this.binaryLength -= this.raw[msgkey] && this.raw[msgkey].length || 0;
this.raw[msgkey] = utf8.encode(newValue);
this.binaryLength += this.raw[msgkey].length;
}
};
}

export function getRawAccessor(msgkey) {
return {
get: function() {
return this.raw[msgkey];
},
set: function(newValue) {
this.raw[msgkey] = newValue;
}
};
}
129 changes: 28 additions & 101 deletions src/js/message-factory.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// import struct from '../bower_components/jspack-arraybuffer/struct.js';
import MessageBase from './message.js';
// import stringFormat from './string-format.js';
import utf8 from '../bower_components/utf8/utf8.js';
import {
getEnumAccessors,
getStringAccessors,
getRawAccessor
} from './accessors.js';
import * as unpackers from './unpackers.js';
import * as packers from './packers.js';

Expand All @@ -15,22 +17,7 @@ import {

const MAX_SUPPORTED_NUMBER = Number.MAX_SAFE_INTEGER > Math.pow(2, 64) - 1 ? Number.MAX_SAFE_INTEGER : Math.pow(2, 64) - 1; //eslint-disable-line

let binaryTypes = {
'bool': '?',
'byte': 'b',
'ubyte': 'B',
'char': 'c',
'short': 'h',
'ushort': 'H',
'int': 'i',
'uint': 'I',
'int64': 'q',
'uint64': 'Q',
'float': 'f',
'double': 'd',
'string': 's'
},
sizeLookup = {
let sizeLookup = {
'bool': 1,
'byte': 1,
'ubyte': 1,
Expand All @@ -48,8 +35,7 @@ let binaryTypes = {
unpackerLookup = {},
packerLookup = {};

// @TODO: we don't need binaryTypes any more, simplify
Object.keys(binaryTypes).forEach(function(typeName) {
Object.keys(sizeLookup).forEach(function(typeName) {
unpackerLookup[typeName] = unpackers['unpack' + typeName.charAt(0).toUpperCase() + typeName.slice(1)];
packerLookup[typeName] = packers['pack' + typeName.charAt(0).toUpperCase() + typeName.slice(1)];
});
Expand All @@ -74,11 +60,8 @@ class MessageFactory {
baseBinaryLength = this.bytesNeededForId,
msgunpackers = [],
msgpackers = [],
dynamicFieldsIndexes = [],
msgProperties = {};



if(schema[className].enums) {
for(let enumName in schema[className].enums) {
let enumValues = schema[className].enums[enumName];
Expand All @@ -97,48 +80,27 @@ class MessageFactory {
};

msgkeys.forEach(function(msgkey, msgkeyindex) {
let unpacker = unpackerLookup[schema[className].format[msgkey]],
packer = packerLookup[schema[className].format[msgkey]];

if(schema[className].format[msgkey] === 'enum') {
msgunpackers.push(getUnpacker(Object.keys(enums).length));
msgpackers.push(getPacker(Object.keys(enums).length));
baseBinaryLength += getBytesToRepresent(Object.keys(enums).length);
msgProperties[msgkey] = {
get: function() {
return this.Cls.reverseEnums[msgkey][this.raw[msgkey]];
},
set: function(newValue) {
this.raw[msgkey] = this.Cls.enums[msgkey][newValue];
}
};
} else {
msgunpackers.push(unpacker.bind(MessageClass));
msgpackers.push(packer.bind(MessageClass));
baseBinaryLength += sizeLookup[schema[className].format[msgkey]];

if(schema[className].format[msgkey] === 'string') {
dynamicFieldsIndexes.push(msgkeyindex);
msgProperties[msgkey] = {
get: function() {
return utf8.decode(this.raw[msgkey]);
},
set: function(newValue) {
this.binaryLength -= this.raw[msgkey] && this.raw[msgkey].length || 0;
this.raw[msgkey] = utf8.encode(newValue);
this.binaryLength += this.raw[msgkey].length;
}
};
} else {
msgProperties[msgkey] = {
get: function() {
return this.raw[msgkey];
},
set: function(newValue) {
this.raw[msgkey] = newValue;
}
};
}
switch(schema[className].format[msgkey]) {
case 'enum':
msgunpackers.push(getUnpacker(Object.keys(enums).length));
msgpackers.push(getPacker(Object.keys(enums).length));
baseBinaryLength += getBytesToRepresent(Object.keys(enums).length);
msgProperties[msgkey] = getEnumAccessors(msgkey);
break;

case 'string':
msgProperties[msgkey] = getStringAccessors(msgkey);
msgunpackers.push(unpackerLookup[schema[className].format[msgkey]]);
msgpackers.push(packerLookup[schema[className].format[msgkey]]);
baseBinaryLength += sizeLookup[schema[className].format[msgkey]];
break;

default:
msgProperties[msgkey] = getRawAccessor(msgkey);
msgunpackers.push(unpackerLookup[schema[className].format[msgkey]]);
msgpackers.push(packerLookup[schema[className].format[msgkey]]);
baseBinaryLength += sizeLookup[schema[className].format[msgkey]];
break;
}
});

Expand All @@ -147,18 +109,10 @@ class MessageFactory {
value: className,
writable: false
},
// 'binaryFormat': {
// value: this.getBinaryFormat(schema[className]),
// writable: false
// },
'format': {
value: schema[className].format,
writable: false
},
// 'schema': {
// value: schema,
// writable: false
// },
'id': {
value: index + 1,
writable: false
Expand Down Expand Up @@ -206,33 +160,6 @@ class MessageFactory {
}.bind(this));
}

// getBinaryFormat(msgSchema) {
// let fields = Object.keys(msgSchema.format).sort();
// let binaryFormat = '!'; // we always use network (big-endian) byte order
// binaryFormat += this.idBinaryFormat;

// fields.forEach(function(field) {
// if(msgSchema.format[field] === 'string') {
// binaryFormat += 'I{}s';
// }
// else if(msgSchema.format[field] === 'enum') {
// try {
// binaryFormat += getBinaryFormatSymbol(Object.keys(msgSchema.format[field]).length);
// } catch(e) {
// throw `Enum field can contain the maximum number MAX_SUPPORTED_NUMBER possible values.`;
// }
// } else {
// try {
// binaryFormat += binaryTypes[msgSchema.format[field]];
// } catch(e) {
// throw `Unknown field type msgSchema.format[field].`;
// }
// }
// });

// return binaryFormat;
// }

getByName(name) {
return this.msgClassesByName[name];
}
Expand Down

0 comments on commit e9355bf

Please sign in to comment.