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:
parent
36ffc6c77c
commit
15052985f1
|
@ -17,13 +17,18 @@ let propTypes = {};
|
||||||
});
|
});
|
||||||
|
|
||||||
[
|
[
|
||||||
|
'DATA',
|
||||||
|
'DATE',
|
||||||
'DICT',
|
'DICT',
|
||||||
'FUNCTION',
|
'FUNCTION',
|
||||||
|
'LIST',
|
||||||
|
'OBJECT',
|
||||||
'REALM',
|
'REALM',
|
||||||
'RESULTS',
|
'RESULTS',
|
||||||
|
'UNDEFINED',
|
||||||
].forEach(function(type) {
|
].forEach(function(type) {
|
||||||
Object.defineProperty(objectTypes, type, {
|
Object.defineProperty(objectTypes, type, {
|
||||||
value: 'ObjectTypes' + type,
|
value: type.toLowerCase(),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
const constants = require('./constants');
|
const constants = require('./constants');
|
||||||
const util = require('./util');
|
const util = require('./util');
|
||||||
|
|
||||||
|
const {objectTypes} = constants;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
create,
|
create,
|
||||||
};
|
};
|
||||||
|
@ -14,12 +16,12 @@ module.exports = {
|
||||||
class List {}
|
class List {}
|
||||||
|
|
||||||
// Non-mutating methods:
|
// Non-mutating methods:
|
||||||
util.createMethods(List.prototype, constants.propTypes.LIST, [
|
util.createMethods(List.prototype, objectTypes.LIST, [
|
||||||
'snapshot',
|
'snapshot',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Mutating methods:
|
// Mutating methods:
|
||||||
util.createMethods(List.prototype, constants.propTypes.LIST, [
|
util.createMethods(List.prototype, objectTypes.LIST, [
|
||||||
'pop',
|
'pop',
|
||||||
'shift',
|
'shift',
|
||||||
'push',
|
'push',
|
||||||
|
|
|
@ -14,8 +14,8 @@ const util = require('./util');
|
||||||
const {keys, propTypes, objectTypes} = constants;
|
const {keys, propTypes, objectTypes} = constants;
|
||||||
const listenersKey = Symbol();
|
const listenersKey = Symbol();
|
||||||
|
|
||||||
rpc.registerTypeConverter(propTypes.LIST, lists.create);
|
rpc.registerTypeConverter(objectTypes.LIST, lists.create);
|
||||||
rpc.registerTypeConverter(propTypes.OBJECT, objects.create);
|
rpc.registerTypeConverter(objectTypes.OBJECT, objects.create);
|
||||||
rpc.registerTypeConverter(objectTypes.RESULTS, results.create);
|
rpc.registerTypeConverter(objectTypes.RESULTS, results.create);
|
||||||
|
|
||||||
class Realm {
|
class Realm {
|
||||||
|
|
18
lib/rpc.js
18
lib/rpc.js
|
@ -9,7 +9,7 @@ const constants = require('./constants');
|
||||||
|
|
||||||
const DEVICE_HOST = 'localhost:8082';
|
const DEVICE_HOST = 'localhost:8082';
|
||||||
|
|
||||||
const {keys, objectTypes, propTypes} = constants;
|
const {keys, objectTypes} = constants;
|
||||||
const {id: idKey, realm: realmKey} = keys;
|
const {id: idKey, realm: realmKey} = keys;
|
||||||
const typeConverters = {};
|
const typeConverters = {};
|
||||||
|
|
||||||
|
@ -39,8 +39,8 @@ module.exports = {
|
||||||
clearTestState,
|
clearTestState,
|
||||||
};
|
};
|
||||||
|
|
||||||
registerTypeConverter(propTypes.DATA, (_, {value}) => base64.decode(value));
|
registerTypeConverter(objectTypes.DATA, (_, {value}) => base64.decode(value));
|
||||||
registerTypeConverter(propTypes.DATE, (_, {value}) => new Date(value));
|
registerTypeConverter(objectTypes.DATE, (_, {value}) => new Date(value));
|
||||||
registerTypeConverter(objectTypes.DICT, deserializeDict);
|
registerTypeConverter(objectTypes.DICT, deserializeDict);
|
||||||
|
|
||||||
function registerTypeConverter(type, handler) {
|
function registerTypeConverter(type, handler) {
|
||||||
|
@ -96,14 +96,12 @@ function clearTestState() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function serialize(realmId, value) {
|
function serialize(realmId, value) {
|
||||||
|
if (typeof value == 'undefined') {
|
||||||
|
return {type: objectTypes.UNDEFINED};
|
||||||
|
}
|
||||||
if (typeof value == 'function') {
|
if (typeof value == 'function') {
|
||||||
return {type: objectTypes.FUNCTION};
|
return {type: objectTypes.FUNCTION};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof value === 'undefined') {
|
|
||||||
return {type: 'undefined'};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!value || typeof value != 'object') {
|
if (!value || typeof value != 'object') {
|
||||||
return {value: value};
|
return {value: value};
|
||||||
}
|
}
|
||||||
|
@ -118,7 +116,7 @@ function serialize(realmId, value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value instanceof Date) {
|
if (value instanceof Date) {
|
||||||
return {type: propTypes.DATE, value: value.getTime()};
|
return {type: objectTypes.DATE, value: value.getTime()};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
|
@ -127,7 +125,7 @@ function serialize(realmId, value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value instanceof ArrayBuffer || ArrayBuffer.isView(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);
|
let keys = Object.keys(value);
|
||||||
|
|
|
@ -23,21 +23,6 @@ JSValueRef RJSMakeError(JSContextRef ctx, const std::string &message) {
|
||||||
return JSObjectMakeError(ctx, 1, &value, NULL);
|
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 RJSStringForJSString(JSStringRef jsString) {
|
||||||
std::string str;
|
std::string str;
|
||||||
size_t maxSize = JSStringGetMaximumUTF8CStringSize(jsString);
|
size_t maxSize = JSStringGetMaximumUTF8CStringSize(jsString);
|
||||||
|
|
|
@ -41,8 +41,6 @@ JSClassRef RJSCreateWrapperClass(const char * name, JSObjectGetPropertyCallback
|
||||||
return JSClassCreate(&classDefinition);
|
return JSClassCreate(&classDefinition);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string RJSTypeGet(realm::PropertyType propertyType);
|
|
||||||
|
|
||||||
std::string RJSStringForJSString(JSStringRef jsString);
|
std::string RJSStringForJSString(JSStringRef jsString);
|
||||||
std::string RJSStringForValue(JSContextRef ctx, JSValueRef value);
|
std::string RJSStringForValue(JSContextRef ctx, JSValueRef value);
|
||||||
std::string RJSValidatedStringForValue(JSContextRef ctx, JSValueRef value, const char * name = nullptr);
|
std::string RJSValidatedStringForValue(JSContextRef ctx, JSValueRef value, const char * name = nullptr);
|
||||||
|
|
25
src/rpc.cpp
25
src/rpc.cpp
|
@ -22,9 +22,14 @@
|
||||||
using RJSAccessor = realm::NativeAccessor<JSValueRef, JSContextRef>;
|
using RJSAccessor = realm::NativeAccessor<JSValueRef, JSContextRef>;
|
||||||
using namespace realm_js;
|
using namespace realm_js;
|
||||||
|
|
||||||
static const char * const RealmObjectTypesDictionary = "ObjectTypesDICT";
|
static const char * const RealmObjectTypesData = "data";
|
||||||
static const char * const RealmObjectTypesFunction = "ObjectTypesFUNCTION";
|
static const char * const RealmObjectTypesDate = "date";
|
||||||
static const char * const RealmObjectTypesResults = "ObjectTypesRESULTS";
|
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() {
|
RPCServer::RPCServer() {
|
||||||
m_context = JSGlobalContextCreate(NULL);
|
m_context = JSGlobalContextCreate(NULL);
|
||||||
|
@ -219,7 +224,7 @@ json RPCServer::serialize_json_value(JSValueRef value) {
|
||||||
if (JSValueIsObjectOfClass(m_context, value, RJSObjectClass())) {
|
if (JSValueIsObjectOfClass(m_context, value, RJSObjectClass())) {
|
||||||
realm::Object *object = RJSGetInternal<realm::Object *>(js_object);
|
realm::Object *object = RJSGetInternal<realm::Object *>(js_object);
|
||||||
return {
|
return {
|
||||||
{"type", RJSTypeGet(realm::PropertyTypeObject)},
|
{"type", RealmObjectTypesObject},
|
||||||
{"id", store_object(js_object)},
|
{"id", store_object(js_object)},
|
||||||
{"schema", serialize_object_schema(object->get_object_schema())}
|
{"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())) {
|
else if (JSValueIsObjectOfClass(m_context, value, RJSListClass())) {
|
||||||
realm::List *list = RJSGetInternal<realm::List *>(js_object);
|
realm::List *list = RJSGetInternal<realm::List *>(js_object);
|
||||||
return {
|
return {
|
||||||
{"type", RJSTypeGet(realm::PropertyTypeArray)},
|
{"type", RealmObjectTypesList},
|
||||||
{"id", store_object(js_object)},
|
{"id", store_object(js_object)},
|
||||||
{"size", list->size()},
|
{"size", list->size()},
|
||||||
{"schema", serialize_object_schema(list->get_object_schema())}
|
{"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)) {
|
else if (RJSIsValueArrayBuffer(m_context, value)) {
|
||||||
std::string data = RJSAccessor::to_binary(m_context, value);
|
std::string data = RJSAccessor::to_binary(m_context, value);
|
||||||
return {
|
return {
|
||||||
{"type", RJSTypeGet(realm::PropertyTypeData)},
|
{"type", RealmObjectTypesData},
|
||||||
{"value", base64_encode((unsigned char *)data.data(), data.size())},
|
{"value", base64_encode((unsigned char *)data.data(), data.size())},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
else if (RJSIsValueDate(m_context, value)) {
|
else if (RJSIsValueDate(m_context, value)) {
|
||||||
return {
|
return {
|
||||||
{"type", RJSTypeGet(realm::PropertyTypeDate)},
|
{"type", RealmObjectTypesDate},
|
||||||
{"value", RJSValidatedValueToNumber(m_context, value)},
|
{"value", RJSValidatedValueToNumber(m_context, value)},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -336,14 +341,14 @@ JSValueRef RPCServer::deserialize_json_value(const json dict)
|
||||||
|
|
||||||
return js_object;
|
return js_object;
|
||||||
}
|
}
|
||||||
else if (type_string == RJSTypeGet(realm::PropertyTypeData)) {
|
else if (type_string == RealmObjectTypesData) {
|
||||||
std::string bytes;
|
std::string bytes;
|
||||||
if (!base64_decode(value.get<std::string>(), &bytes)) {
|
if (!base64_decode(value.get<std::string>(), &bytes)) {
|
||||||
throw std::runtime_error("Failed to decode base64 encoded data");
|
throw std::runtime_error("Failed to decode base64 encoded data");
|
||||||
}
|
}
|
||||||
return RJSAccessor::from_binary(m_context, realm::BinaryData(bytes));
|
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 exception = NULL;
|
||||||
JSValueRef time = JSValueMakeNumber(m_context, value.get<double>());
|
JSValueRef time = JSValueMakeNumber(m_context, value.get<double>());
|
||||||
JSObjectRef date = JSObjectMakeDate(m_context, 1, &time, &exception);
|
JSObjectRef date = JSObjectMakeDate(m_context, 1, &time, &exception);
|
||||||
|
@ -353,7 +358,7 @@ JSValueRef RPCServer::deserialize_json_value(const json dict)
|
||||||
}
|
}
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
else if (type_string == "undefined") {
|
else if (type_string == RealmObjectTypesUndefined) {
|
||||||
return JSValueMakeUndefined(m_context);
|
return JSValueMakeUndefined(m_context);
|
||||||
}
|
}
|
||||||
assert(0);
|
assert(0);
|
||||||
|
|
Loading…
Reference in New Issue