diff --git a/lib/realm.js b/lib/realm.js index bf6f4fe5..4b0cee12 100644 --- a/lib/realm.js +++ b/lib/realm.js @@ -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, { diff --git a/lib/util.js b/lib/util.js index ba14ede7..699662dc 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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);