Make RPC Results objects auto-resize

This commit is contained in:
Scott Kyle 2015-10-19 17:28:20 -07:00
parent 3439b4c455
commit 85e2a26b42
2 changed files with 42 additions and 26 deletions

View File

@ -11,6 +11,7 @@ let util = require('./util');
let {keys, propTypes, objectTypes} = constants;
let notificationsKey = Symbol();
let notificationCallbackKey = Symbol();
let resultsKey = Symbol();
// TODO: DATA
rpc.registerTypeConverter(propTypes.DATE, (_, info) => new Date(info.value));
@ -42,6 +43,7 @@ class Realm {
this[keys.realm] = realmId;
this[keys.type] = objectTypes.REALM;
this[notificationsKey] = [];
this[resultsKey] = [];
[
'path',
@ -52,19 +54,25 @@ class Realm {
}
addNotification(callback) {
let realmId = this[keys.realm];
if (!realmId) {
throw new TypeError('addNotification method was not called on a Realm object!');
}
if (typeof callback != 'function') {
throw new Error('Realm.addNotification must be passed a function!');
}
let notification = rpc.callMethod(realmId, realmId, 'addNotification', [callback]);
let method = util.createMethod(objectTypes.REALM, 'addNotification');
let notification = method.apply(this, arguments);
notification[notificationCallbackKey] = callback;
this[notificationsKey].push(notification);
return notification;
}
objects() {
let method = util.createMethod(objectTypes.REALM, 'objects');
let results = method.apply(this, arguments);
this[resultsKey].push(results);
return results;
}
write(callback) {
@ -88,6 +96,10 @@ class Realm {
rpc.commitTransaction(realmId);
for (let results of this[resultsKey]) {
results[keys.resize]();
}
for (let notification of this[notificationsKey]) {
let callback = notification[notificationCallbackKey];
callback(this, 'DidChangeNotification');
@ -100,7 +112,6 @@ util.createMethods(Realm.prototype, objectTypes.REALM, [
'create',
'delete',
'deleteAll',
'objects',
]);
Object.defineProperties(Realm, {

View File

@ -8,6 +8,7 @@ let {keys} = constants;
module.exports = {
createList,
createMethods,
createMethod,
getterForProperty,
setterForProperty,
};
@ -62,31 +63,35 @@ function createMethods(prototype, type, methodNames, resize) {
for (let name of methodNames) {
props[name] = {
value: function() {
let realmId = this[keys.realm];
let id = this[keys.id];
if (!realmId || !id) {
throw new TypeError(name + ' method was not called a Realm object!');
}
if (this[keys.type] !== type) {
throw new TypeError(name + ' method was called on an object of the wrong type!');
}
let result = rpc.callMethod(realmId, id, name, Array.from(arguments));
if (resize) {
this[keys.resize]();
}
return result;
}
value: createMethod(type, name, resize),
};
}
Object.defineProperties(prototype, props);
}
function createMethod(type, name, resize) {
return function() {
let realmId = this[keys.realm];
let id = this[keys.id];
if (!realmId || !id) {
throw new TypeError(name + ' method was not called a Realm object!');
}
if (this[keys.type] !== type) {
throw new TypeError(name + ' method was called on an object of the wrong type!');
}
let result = rpc.callMethod(realmId, id, name, Array.from(arguments));
if (resize) {
this[keys.resize]();
}
return result;
};
}
function getterForProperty(name) {
return function() {
return rpc.getProperty(this[keys.realm], this[keys.id], name);