realm-js/lib/realm.js

131 lines
3.5 KiB
JavaScript
Raw Normal View History

'use strict';
let constants = require('./constants');
2015-10-06 19:36:56 +00:00
let lists = require('./lists');
let objects = require('./objects');
let notifications = require('./notifications');
2015-10-08 00:08:19 +00:00
let results = require('./results');
2015-10-06 19:36:56 +00:00
let rpc = require('./rpc');
let util = require('./util');
let {keys, propTypes, objectTypes} = constants;
let notificationsKey = Symbol();
let notificationCallbackKey = Symbol();
2015-10-20 00:28:20 +00:00
let 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 {
constructor(config) {
let schema = typeof config == 'object' && config.schema;
let constructors = {};
for (let i = 0, len = schema ? schema.length : 0; i < len; i++) {
let item = schema[i];
let proto = item.prototype;
2015-10-06 19:36:56 +00:00
if (proto && proto.schema) {
schema.splice(i, 1, proto.schema);
constructors[proto.schema.name] = item;
}
}
let realmId = rpc.createRealm(Array.from(arguments));
2015-10-06 07:52:15 +00:00
objects.registerConstructors(realmId, constructors);
this[keys.id] = realmId;
this[keys.realm] = realmId;
this[keys.type] = objectTypes.REALM;
this[notificationsKey] = [];
2015-10-20 00:28:20 +00:00
this[resultsKey] = [];
[
'path',
'schemaVersion',
].forEach((name) => {
Object.defineProperty(this, name, {get: util.getterForProperty(name)});
});
}
addNotification(callback) {
if (typeof callback != 'function') {
2015-10-19 19:05:41 +00:00
throw new Error('Realm.addNotification must be passed a function!');
}
2015-10-20 00:28:20 +00:00
let method = util.createMethod(objectTypes.REALM, 'addNotification');
let notification = method.apply(this, arguments);
notification[notificationCallbackKey] = callback;
this[notificationsKey].push(notification);
2015-10-20 00:28:20 +00:00
return notification;
}
objects() {
let method = util.createMethod(objectTypes.REALM, 'objects');
let results = method.apply(this, arguments);
this[resultsKey].push(results);
return results;
}
write(callback) {
let realmId = this[keys.realm];
2015-10-08 22:32:14 +00:00
if (!realmId) {
throw new TypeError('write method was not called on a Realm object!');
}
if (typeof callback != 'function') {
throw new TypeError('Realm.write() must be passed a function!');
2015-10-08 22:32:14 +00:00
}
rpc.beginTransaction(realmId);
try {
callback();
} catch (e) {
2015-10-08 22:32:14 +00:00
rpc.cancelTransaction(realmId);
throw e;
}
2015-10-08 22:32:14 +00:00
rpc.commitTransaction(realmId);
2015-10-20 00:28:20 +00:00
for (let results of this[resultsKey]) {
results[keys.resize]();
}
for (let notification of this[notificationsKey]) {
let callback = notification[notificationCallbackKey];
callback(this, 'DidChangeNotification');
}
}
}
util.createMethods(Realm.prototype, objectTypes.REALM, [
'close',
'create',
'delete',
'deleteAll',
]);
Object.defineProperties(Realm, {
Types: {
value: Object.freeze(propTypes),
},
defaultPath: {
get: rpc.getDefaultPath,
set: rpc.setDefaultPath,
},
clearTestState: {
value: rpc.clearTestState,
},
});
module.exports = Realm;