Remove confusion between propTypes and objectTypes

The RPC layer now only speaks in objectTypes, since they don't always equate to propTypes. We were overloading the use of propTypes for no good purpose.
This commit is contained in:
Scott Kyle 2015-11-02 22:16:50 -08:00
parent 36ffc6c77c
commit 15052985f1
7 changed files with 35 additions and 42 deletions

View File

@ -17,13 +17,18 @@ let propTypes = {};
});
[
'DATA',
'DATE',
'DICT',
'FUNCTION',
'LIST',
'OBJECT',
'REALM',
'RESULTS',
'UNDEFINED',
].forEach(function(type) {
Object.defineProperty(objectTypes, type, {
value: 'ObjectTypes' + type,
value: type.toLowerCase(),
});
});

View File

@ -7,6 +7,8 @@
const constants = require('./constants');
const util = require('./util');
const {objectTypes} = constants;
module.exports = {
create,
};
@ -14,12 +16,12 @@ module.exports = {
class List {}
// Non-mutating methods:
util.createMethods(List.prototype, constants.propTypes.LIST, [
util.createMethods(List.prototype, objectTypes.LIST, [
'snapshot',
]);
// Mutating methods:
util.createMethods(List.prototype, constants.propTypes.LIST, [
util.createMethods(List.prototype, objectTypes.LIST, [
'pop',
'shift',
'push',

View File

@ -14,8 +14,8 @@ const util = require('./util');
const {keys, propTypes, objectTypes} = constants;
const listenersKey = Symbol();
rpc.registerTypeConverter(propTypes.LIST, lists.create);
rpc.registerTypeConverter(propTypes.OBJECT, objects.create);
rpc.registerTypeConverter(objectTypes.LIST, lists.create);
rpc.registerTypeConverter(objectTypes.OBJECT, objects.create);
rpc.registerTypeConverter(objectTypes.RESULTS, results.create);
class Realm {

View File

@ -9,7 +9,7 @@ const constants = require('./constants');
const DEVICE_HOST = 'localhost:8082';
const {keys, objectTypes, propTypes} = constants;
const {keys, objectTypes} = constants;
const {id: idKey, realm: realmKey} = keys;
const typeConverters = {};
@ -39,8 +39,8 @@ module.exports = {
clearTestState,
};
registerTypeConverter(propTypes.DATA, (_, {value}) => base64.decode(value));
registerTypeConverter(propTypes.DATE, (_, {value}) => new Date(value));
registerTypeConverter(objectTypes.DATA, (_, {value}) => base64.decode(value));
registerTypeConverter(objectTypes.DATE, (_, {value}) => new Date(value));
registerTypeConverter(objectTypes.DICT, deserializeDict);
function registerTypeConverter(type, handler) {
@ -96,14 +96,12 @@ function clearTestState() {
}
function serialize(realmId, value) {
if (typeof value == 'undefined') {
return {type: objectTypes.UNDEFINED};
}
if (typeof value == 'function') {
return {type: objectTypes.FUNCTION};
}
if (typeof value === 'undefined') {
return {type: 'undefined'};
}
if (!value || typeof value != 'object') {
return {value: value};
}
@ -118,7 +116,7 @@ function serialize(realmId, value) {
}
if (value instanceof Date) {
return {type: propTypes.DATE, value: value.getTime()};
return {type: objectTypes.DATE, value: value.getTime()};
}
if (Array.isArray(value)) {
@ -127,7 +125,7 @@ function serialize(realmId, value) {
}
if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
return {type: propTypes.DATA, value: base64.encode(value)};
return {type: objectTypes.DATA, value: base64.encode(value)};
}
let keys = Object.keys(value);

View File

@ -23,21 +23,6 @@ JSValueRef RJSMakeError(JSContextRef ctx, const std::string &message) {
return JSObjectMakeError(ctx, 1, &value, NULL);
}
std::string RJSTypeGet(PropertyType propertyType) {
switch (propertyType) {
case PropertyTypeBool: return "bool";
case PropertyTypeInt: return "int";
case PropertyTypeFloat: return "float";
case PropertyTypeDouble:return "double";
case PropertyTypeString:return "string";
case PropertyTypeDate: return "date";
case PropertyTypeData: return "data";
case PropertyTypeObject:return "object";
case PropertyTypeArray: return "list";
default: return nullptr;
}
}
std::string RJSStringForJSString(JSStringRef jsString) {
std::string str;
size_t maxSize = JSStringGetMaximumUTF8CStringSize(jsString);

View File

@ -41,8 +41,6 @@ JSClassRef RJSCreateWrapperClass(const char * name, JSObjectGetPropertyCallback
return JSClassCreate(&classDefinition);
}
std::string RJSTypeGet(realm::PropertyType propertyType);
std::string RJSStringForJSString(JSStringRef jsString);
std::string RJSStringForValue(JSContextRef ctx, JSValueRef value);
std::string RJSValidatedStringForValue(JSContextRef ctx, JSValueRef value, const char * name = nullptr);

View File

@ -22,9 +22,14 @@
using RJSAccessor = realm::NativeAccessor<JSValueRef, JSContextRef>;
using namespace realm_js;
static const char * const RealmObjectTypesDictionary = "ObjectTypesDICT";
static const char * const RealmObjectTypesFunction = "ObjectTypesFUNCTION";
static const char * const RealmObjectTypesResults = "ObjectTypesRESULTS";
static const char * const RealmObjectTypesData = "data";
static const char * const RealmObjectTypesDate = "date";
static const char * const RealmObjectTypesDictionary = "dict";
static const char * const RealmObjectTypesFunction = "function";
static const char * const RealmObjectTypesList = "list";
static const char * const RealmObjectTypesObject = "object";
static const char * const RealmObjectTypesResults = "results";
static const char * const RealmObjectTypesUndefined = "undefined";
RPCServer::RPCServer() {
m_context = JSGlobalContextCreate(NULL);
@ -219,7 +224,7 @@ json RPCServer::serialize_json_value(JSValueRef value) {
if (JSValueIsObjectOfClass(m_context, value, RJSObjectClass())) {
realm::Object *object = RJSGetInternal<realm::Object *>(js_object);
return {
{"type", RJSTypeGet(realm::PropertyTypeObject)},
{"type", RealmObjectTypesObject},
{"id", store_object(js_object)},
{"schema", serialize_object_schema(object->get_object_schema())}
};
@ -227,7 +232,7 @@ json RPCServer::serialize_json_value(JSValueRef value) {
else if (JSValueIsObjectOfClass(m_context, value, RJSListClass())) {
realm::List *list = RJSGetInternal<realm::List *>(js_object);
return {
{"type", RJSTypeGet(realm::PropertyTypeArray)},
{"type", RealmObjectTypesList},
{"id", store_object(js_object)},
{"size", list->size()},
{"schema", serialize_object_schema(list->get_object_schema())}
@ -253,13 +258,13 @@ json RPCServer::serialize_json_value(JSValueRef value) {
else if (RJSIsValueArrayBuffer(m_context, value)) {
std::string data = RJSAccessor::to_binary(m_context, value);
return {
{"type", RJSTypeGet(realm::PropertyTypeData)},
{"type", RealmObjectTypesData},
{"value", base64_encode((unsigned char *)data.data(), data.size())},
};
}
else if (RJSIsValueDate(m_context, value)) {
return {
{"type", RJSTypeGet(realm::PropertyTypeDate)},
{"type", RealmObjectTypesDate},
{"value", RJSValidatedValueToNumber(m_context, value)},
};
}
@ -336,14 +341,14 @@ JSValueRef RPCServer::deserialize_json_value(const json dict)
return js_object;
}
else if (type_string == RJSTypeGet(realm::PropertyTypeData)) {
else if (type_string == RealmObjectTypesData) {
std::string bytes;
if (!base64_decode(value.get<std::string>(), &bytes)) {
throw std::runtime_error("Failed to decode base64 encoded data");
}
return RJSAccessor::from_binary(m_context, realm::BinaryData(bytes));
}
else if (type_string == RJSTypeGet(realm::PropertyTypeDate)) {
else if (type_string == RealmObjectTypesDate) {
JSValueRef exception = NULL;
JSValueRef time = JSValueMakeNumber(m_context, value.get<double>());
JSObjectRef date = JSObjectMakeDate(m_context, 1, &time, &exception);
@ -353,7 +358,7 @@ JSValueRef RPCServer::deserialize_json_value(const json dict)
}
return date;
}
else if (type_string == "undefined") {
else if (type_string == RealmObjectTypesUndefined) {
return JSValueMakeUndefined(m_context);
}
assert(0);