Add support for Results methods in Chrome

Which only consists of sortByProperty
This commit is contained in:
Scott Kyle 2015-10-14 18:53:37 -07:00
parent 78ec67cd6c
commit 5f52d07154
5 changed files with 59 additions and 21 deletions

View File

@ -6,11 +6,29 @@ let util = require('./util');
let idKey = util.idKey; let idKey = util.idKey;
let realmKey = util.realmKey; let realmKey = util.realmKey;
let resizeListKey = util.resizeListKey; let resizeListKey = util.resizeListKey;
let prototype = {};
exports.create = create; exports.create = create;
[
'sortByProperty',
].forEach(function(name) {
Object.defineProperty(prototype, name, {
value: function() {
let resultsId = this[idKey];
let realmId = this[realmKey];
if (!resultsId || !realmId) {
throw new TypeError(name + ' method was not called on Results!');
}
return rpc.callListMethod(realmId, resultsId, name, Array.from(arguments));
}
});
});
function create(realmId, info) { function create(realmId, info) {
let results = util.createList(null, getterForLength, getterForIndex); let results = util.createList(prototype, getterForLength, getterForIndex);
let size = info.size; let size = info.size;
results[realmKey] = realmId; results[realmKey] = realmId;

View File

@ -17,27 +17,30 @@ if (XMLHttpRequest.__proto__ != window.XMLHttpRequestEventTarget) {
window.XMLHttpRequest = override; window.XMLHttpRequest = override;
} }
exports.registerTypeConverter = registerTypeConverter; module.exports = {
registerTypeConverter,
exports.createRealm = createRealm; createRealm,
exports.callRealmMethod = callRealmMethod; callRealmMethod,
exports.getObjectProperty = getObjectProperty; getObjectProperty,
exports.setObjectProperty = setObjectProperty; setObjectProperty,
exports.getListItem = getListItem; getListItem,
exports.setListItem = setListItem; setListItem,
exports.getListSize = getListSize; getListSize,
exports.callListMethod = callListMethod; callListMethod,
exports.getResultsItem = getResultsItem; getResultsItem,
exports.getResultsSize = getResultsSize; getResultsSize,
callResultsMethod,
exports.beginTransaction = beginTransaction; beginTransaction,
exports.cancelTransaction = cancelTransaction; cancelTransaction,
exports.commitTransaction = commitTransaction; commitTransaction,
exports.clearTestState = clearTestState; clearTestState,
};
function registerTypeConverter(type, handler) { function registerTypeConverter(type, handler) {
typeConverters[type] = handler; typeConverters[type] = handler;
@ -101,6 +104,15 @@ function getResultsSize(realmId, resultsId) {
return sendRequest('get_results_size', {realmId, resultsId}); return sendRequest('get_results_size', {realmId, resultsId});
} }
function callResultsMethod(realmId, resultsId, name, args) {
if (args) {
args = args.map((arg) => serialize(realmId, arg));
}
let result = sendRequest('call_results_method', {realmId, resultsId, name, arguments: args});
return deserialize(realmId, result);
}
function beginTransaction(realmId) { function beginTransaction(realmId) {
sendRequest('begin_transaction', {realmId}); sendRequest('begin_transaction', {realmId});
} }

View File

@ -23,6 +23,7 @@ namespace realm {
typedef std::shared_ptr<Realm> SharedRealm; typedef std::shared_ptr<Realm> SharedRealm;
} }
extern const JSStaticFunction RJSResultsFuncs[];
JSClassRef RJSResultsClass(); JSClassRef RJSResultsClass();
JSObjectRef RJSResultsCreate(JSContextRef ctx, realm::SharedRealm realm, std::string className); JSObjectRef RJSResultsCreate(JSContextRef ctx, realm::SharedRealm realm, std::string className);
JSObjectRef RJSResultsCreate(JSContextRef ctx, realm::SharedRealm realm, std::string className, std::string query); JSObjectRef RJSResultsCreate(JSContextRef ctx, realm::SharedRealm realm, std::string className, std::string query);

View File

@ -118,12 +118,12 @@ JSObjectRef RJSResultsCreate(JSContextRef ctx, SharedRealm realm, std::string cl
return RJSWrapObject<Results *>(ctx, RJSResultsClass(), new Results(realm, *object_schema, std::move(query))); return RJSWrapObject<Results *>(ctx, RJSResultsClass(), new Results(realm, *object_schema, std::move(query)));
} }
const JSStaticFunction RJSResultsFuncs[] = {
{"sortByProperty", SortByProperty, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{NULL, NULL},
};
JSClassRef RJSResultsClass() { JSClassRef RJSResultsClass() {
const JSStaticFunction resultsFuncs[] = { static JSClassRef s_objectClass = RJSCreateWrapperClass<Results *>("Results", ResultsGetProperty, NULL, RJSResultsFuncs, ResultsPropertyNames);
{"sortByProperty", SortByProperty, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{NULL, NULL},
};
static JSClassRef s_objectClass = RJSCreateWrapperClass<Results *>("Results", ResultsGetProperty, NULL, resultsFuncs, ResultsPropertyNames);
return s_objectClass; return s_objectClass;
} }

View File

@ -169,6 +169,13 @@ using RPCRequest = std::function<NSDictionary *(NSDictionary *dictionary)>;
} }
return @{@"result": @(length)}; return @{@"result": @(length)};
}; };
_requests["/call_results_method"] = [=](NSDictionary *dict) {
NSString *name = dict[@"name"];
return [self performObjectMethod:name.UTF8String
classMethods:RJSResultsFuncs
args:dict[@"arguments"]
objectId:[dict[@"resultsId"] unsignedLongValue]];
};
_requests["/get_list_item"] = [=](NSDictionary *dict) { _requests["/get_list_item"] = [=](NSDictionary *dict) {
RPCObjectID listId = [dict[@"listId"] unsignedLongValue]; RPCObjectID listId = [dict[@"listId"] unsignedLongValue];
long index = [dict[@"index"] longValue]; long index = [dict[@"index"] longValue];