From bb68e45250fdd01b57e2140f6942e2cf0f0ea8b6 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Mon, 26 Oct 2015 16:49:46 -0700 Subject: [PATCH] support notifications in chrome --- lib/constants.js | 1 - lib/notifications.js | 21 --------------------- lib/realm.js | 30 +++++++++++++++++------------- 3 files changed, 17 insertions(+), 35 deletions(-) delete mode 100644 lib/notifications.js diff --git a/lib/constants.js b/lib/constants.js index 813db4aa..37ec3dcd 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -15,7 +15,6 @@ let propTypes = {}; [ 'FUNCTION', - 'NOTIFICATION', 'REALM', 'RESULTS', ].forEach(function(type) { diff --git a/lib/notifications.js b/lib/notifications.js deleted file mode 100644 index e6da21bd..00000000 --- a/lib/notifications.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict'; - -const constants = require('./constants'); - -const {keys} = constants; - -module.exports = { - create, -}; - -class Notification {} - -function create(realmId, info) { - let notification = new Notification(); - - notification[keys.realm] = realmId; - notification[keys.id] = info.id; - notification[keys.type] = info.type; - - return notification; -} diff --git a/lib/realm.js b/lib/realm.js index e951d316..6a22be88 100644 --- a/lib/realm.js +++ b/lib/realm.js @@ -3,21 +3,18 @@ const constants = require('./constants'); const lists = require('./lists'); const objects = require('./objects'); -const notifications = require('./notifications'); const results = require('./results'); const rpc = require('./rpc'); const util = require('./util'); const {keys, propTypes, objectTypes} = constants; const notificationsKey = Symbol(); -const notificationCallbackKey = Symbol(); const resultsKey = Symbol(); // TODO: DATA rpc.registerTypeConverter(propTypes.DATE, (_, info) => new Date(info.value)); rpc.registerTypeConverter(propTypes.LIST, lists.create); rpc.registerTypeConverter(propTypes.OBJECT, objects.create); -rpc.registerTypeConverter(objectTypes.NOTIFICATION, notifications.create); rpc.registerTypeConverter(objectTypes.RESULTS, results.create); class Realm { @@ -53,18 +50,26 @@ class Realm { }); } - addNotification(callback) { + addListener(callback) { if (typeof callback != 'function') { - throw new Error('Realm.addNotification must be passed a function!'); + throw new Error('Realm.addListener must be passed a function!'); } + this[notificationsKey].push(callback); + } - let method = util.createMethod(objectTypes.REALM, 'addNotification'); - let notification = method.apply(this, arguments); - notification[notificationCallbackKey] = callback; + removeListener(callback) { + if (typeof callback != 'function') { + throw new Error('Realm.addListener must be passed a function!'); + } + var index = 0; + while((index = this[notificationsKey].indexOf(callback, index)) != -1) { + this[notificationsKey].splice(index, 1); + }; + } - this[notificationsKey].push(notification); - return notification; + removeAllListeners() { + this[notificationsKey] = []; } objects() { @@ -100,9 +105,8 @@ class Realm { results[keys.resize](); } - for (let notification of this[notificationsKey]) { - let callback = notification[notificationCallbackKey]; - callback(this, 'DidChangeNotification'); + for (let callback of this[notificationsKey]) { + callback(this, 'change'); } } }