support notifications in chrome

This commit is contained in:
Ari Lazier 2015-10-26 16:49:46 -07:00
parent 6f57500972
commit bb68e45250
3 changed files with 17 additions and 35 deletions

View File

@ -15,7 +15,6 @@ let propTypes = {};
[
'FUNCTION',
'NOTIFICATION',
'REALM',
'RESULTS',
].forEach(function(type) {

View File

@ -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;
}

View File

@ -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');
}
}
}