JS implementation for Results from RPC

This commit is contained in:
Scott Kyle 2015-10-07 17:08:19 -07:00
parent f35d83a07d
commit 41ba3b62b8
5 changed files with 99 additions and 29 deletions

View File

@ -1,10 +1,11 @@
'use strict'; 'use strict';
let rpc = require('./rpc'); let rpc = require('./rpc');
let util = require('./util');
let idKey = Symbol(); let idKey = Symbol();
let realmKey = Symbol(); let realmKey = Symbol();
let prototype = {}; let prototype = util.createListPrototype(getterForLength, getterForIndex, setterForIndex);
exports.create = create; exports.create = create;
@ -30,7 +31,7 @@ exports.create = create;
// Since this method might have grown the list, ensure index properties are defined. // Since this method might have grown the list, ensure index properties are defined.
if (growthMethod) { if (growthMethod) {
defineIndexProperties(this.length); prototype[util.growListPrototypeKey](this.length);
} }
return result; return result;
@ -38,12 +39,6 @@ exports.create = create;
}); });
}); });
Object.defineProperty(prototype, 'length', {
get() {
return rpc.getListSize(this[realmKey], this[idKey]);
}
});
function create(realmId, info) { function create(realmId, info) {
let list = Object.create(prototype); let list = Object.create(prototype);
let size = info.size; let size = info.size;
@ -51,27 +46,13 @@ function create(realmId, info) {
list[realmKey] = realmId; list[realmKey] = realmId;
list[idKey] = info.id; list[idKey] = info.id;
defineIndexProperties(size); list[util.growListPrototypeKey](size);
return list;
} }
let maxSize = 0; function getterForLength() {
function defineIndexProperties(size) { return rpc.getListSize(this[realmKey], this[idKey]);
if (size < maxSize) {
return;
}
let props = {};
for (let i = maxSize; i <= size; i++) {
props[i] = {
get: getterForIndex(i),
set: setterForIndex(i),
};
}
// TODO: Use ES6 Proxy once it's supported on Chrome!
Object.defineProperties(prototype, props);
maxSize = size + 1;
} }
function getterForIndex(index) { function getterForIndex(index) {

View File

@ -2,6 +2,7 @@
let lists = require('./lists'); let lists = require('./lists');
let objects = require('./objects'); let objects = require('./objects');
let results = require('./results');
let rpc = require('./rpc'); let rpc = require('./rpc');
let types = require('./types'); let types = require('./types');
@ -51,8 +52,11 @@ class Realm {
// TODO // TODO
} }
objects(type) { objects(type, predicate) {
// TODO let realmId = this[realmKey];
let info = rpc.getObjects(realmId, type, predicate);
return results.create(realmId, info);
} }
write(callback) { write(callback) {

33
lib/results.js Normal file
View File

@ -0,0 +1,33 @@
'use strict';
let rpc = require('./rpc');
let util = require('./util');
let idKey = Symbol();
let realmKey = Symbol();
let prototype = util.createListPrototype(getterForLength, getterForIndex);
exports.create = create;
function create(realmId, info) {
let results = Object.create(prototype);
let size = info.size;
results[realmKey] = realmId;
results[idKey] = info.resultsId;
results[util.growListPrototypeKey](size);
return results;
}
function getterForLength() {
return rpc.getResultsSize(this[realmKey], this[idKey]);
}
function getterForIndex(index) {
return function() {
let realmId = this[realmKey];
return rpc.getResultsItem(realmId, this[idKey], index);
};
}

View File

@ -17,6 +17,7 @@ exports.registerTypeConverter = registerTypeConverter;
exports.createRealm = createRealm; exports.createRealm = createRealm;
exports.createObject = createObject; exports.createObject = createObject;
exports.getObjects = getObjects;
exports.getObjectProperty = getObjectProperty; exports.getObjectProperty = getObjectProperty;
exports.setObjectProperty = setObjectProperty; exports.setObjectProperty = setObjectProperty;
@ -26,6 +27,9 @@ exports.setListItem = setListItem;
exports.getListSize = getListSize; exports.getListSize = getListSize;
exports.callListMethod = callListMethod; exports.callListMethod = callListMethod;
exports.getResultsItem = getResultsItem;
exports.getResultsSize = getResultsSize;
exports.beginTransaction = beginTransaction; exports.beginTransaction = beginTransaction;
exports.cancelTransaction = cancelTransaction; exports.cancelTransaction = cancelTransaction;
exports.commitTransaction = commitTransaction; exports.commitTransaction = commitTransaction;
@ -42,6 +46,10 @@ function createObject(realmId, type, values) {
return sendRequest('create_object', {realmId, type, values}); return sendRequest('create_object', {realmId, type, values});
} }
function getObjects(realmId, type, predicate) {
return sendRequest('get_objects', {realmId, type, predicate});
}
function getObjectProperty(realmId, objectId, name) { function getObjectProperty(realmId, objectId, name) {
let result = sendRequest('get_property', {realmId, objectId, name}); let result = sendRequest('get_property', {realmId, objectId, name});
return convert(result); return convert(result);
@ -69,6 +77,15 @@ function callListMethod(realmId, listId, name, args) {
return convert(result); return convert(result);
} }
function getResultsItem(realmId, resultsId, index) {
let result = sendRequest('get_results_item', {realmId, resultsId, index});
return convert(result);
}
function getResultsSize(realmId, resultsId) {
return sendRequest('get_results_size', {realmId, resultsId});
}
function beginTransaction(realmId) { function beginTransaction(realmId) {
sendRequest('begin_transaction', {realmId}); sendRequest('begin_transaction', {realmId});
} }

35
lib/util.js Normal file
View File

@ -0,0 +1,35 @@
'use strict';
let growListPrototypeKey = exports.growListPrototypeKey = Symbol();
exports.createListPrototype = createListPrototype;
function createListPrototype(getterForLength, getterForIndex, setterForIndex) {
let prototype = {};
let maxSize = 0;
Object.defineProperty(prototype, 'length', {
get: getterForLength
});
prototype[growListPrototypeKey] = function(size) {
if (size < maxSize) {
return;
}
let props = {};
for (let i = maxSize; i <= size; i++) {
props[i] = {
get: getterForIndex(i),
set: setterForIndex && setterForIndex(i),
};
}
// TODO: Use ES6 Proxy once it's supported on Chrome!
Object.defineProperties(prototype, props);
maxSize = size + 1;
}
return prototype;
}