2015-10-02 05:56:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
let internal = require('./internal-types');
|
|
|
|
let keys = require('./keys');
|
2015-10-06 19:36:56 +00:00
|
|
|
let lists = require('./lists');
|
|
|
|
let objects = require('./objects');
|
2015-10-15 01:00:21 +00:00
|
|
|
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 types = require('./types');
|
2015-10-08 08:52:37 +00:00
|
|
|
let util = require('./util');
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-15 01:00:21 +00:00
|
|
|
let notificationsKey = Symbol();
|
|
|
|
let notificationCallbackKey = Symbol();
|
2015-10-02 05:56:47 +00:00
|
|
|
|
|
|
|
// TODO: DATA
|
|
|
|
rpc.registerTypeConverter(types.DATE, (_, info) => new Date(info.value));
|
|
|
|
rpc.registerTypeConverter(types.LIST, lists.create);
|
|
|
|
rpc.registerTypeConverter(types.OBJECT, objects.create);
|
2015-10-19 19:06:47 +00:00
|
|
|
rpc.registerTypeConverter(internal.NOTIFICATION, notifications.create);
|
|
|
|
rpc.registerTypeConverter(internal.RESULTS, results.create);
|
2015-10-02 05:56:47 +00:00
|
|
|
|
|
|
|
class Realm {
|
|
|
|
constructor(config) {
|
|
|
|
let schema = typeof config == 'object' && config.schema;
|
|
|
|
let constructors = {};
|
2015-10-15 01:00:21 +00:00
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
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) {
|
2015-10-02 05:56:47 +00:00
|
|
|
schema.splice(i, 1, proto.schema);
|
|
|
|
constructors[proto.schema.name] = item;
|
|
|
|
}
|
|
|
|
}
|
2015-10-15 01:00:21 +00:00
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
let realmId = rpc.createRealm(Array.from(arguments));
|
2015-10-15 01:00:21 +00:00
|
|
|
|
2015-10-06 07:52:15 +00:00
|
|
|
objects.registerConstructors(realmId, constructors);
|
2015-10-15 01:00:21 +00:00
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
this[keys.id] = realmId;
|
|
|
|
this[keys.realm] = realmId;
|
|
|
|
this[keys.type] = internal.REALM;
|
2015-10-15 01:00:21 +00:00
|
|
|
this[notificationsKey] = [];
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addNotification(callback) {
|
2015-10-19 19:06:47 +00:00
|
|
|
let realmId = this[keys.realm];
|
2015-10-15 01:00:21 +00:00
|
|
|
|
|
|
|
if (!realmId) {
|
|
|
|
throw new TypeError('addNotification method was not called on a Realm object!');
|
|
|
|
}
|
|
|
|
if (typeof callback != 'function') {
|
2015-10-19 19:05:41 +00:00
|
|
|
throw new Error('Realm.addNotification must be passed a function!');
|
2015-10-15 01:00:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
let notification = rpc.callMethod(realmId, realmId, internal.REALM, 'addNotification', [callback]);
|
2015-10-15 01:00:21 +00:00
|
|
|
notification[notificationCallbackKey] = callback;
|
|
|
|
|
|
|
|
this[notificationsKey].push(notification);
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
write(callback) {
|
2015-10-19 19:06:47 +00:00
|
|
|
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') {
|
2015-10-02 05:56:47 +00:00
|
|
|
throw new TypeError('Realm.write() must be passed a function!');
|
2015-10-08 22:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpc.beginTransaction(realmId);
|
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
try {
|
|
|
|
callback();
|
|
|
|
} catch (e) {
|
2015-10-08 22:32:14 +00:00
|
|
|
rpc.cancelTransaction(realmId);
|
2015-10-02 05:56:47 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2015-10-08 22:32:14 +00:00
|
|
|
|
|
|
|
rpc.commitTransaction(realmId);
|
2015-10-15 01:00:21 +00:00
|
|
|
|
|
|
|
for (let notification of this[notificationsKey]) {
|
|
|
|
let callback = notification[notificationCallbackKey];
|
|
|
|
callback(this, 'DidChangeNotification');
|
|
|
|
}
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
util.createMethods(Realm.prototype, internal.REALM, [
|
2015-10-14 23:06:20 +00:00
|
|
|
'close',
|
2015-10-08 16:43:38 +00:00
|
|
|
'create',
|
|
|
|
'delete',
|
|
|
|
'deleteAll',
|
2015-10-08 17:04:50 +00:00
|
|
|
'objects',
|
2015-10-19 19:06:47 +00:00
|
|
|
]);
|
2015-10-08 16:43:38 +00:00
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
Object.defineProperty(Realm, 'Types', {value: types});
|
|
|
|
|
2015-10-16 01:48:13 +00:00
|
|
|
Object.defineProperty(Realm, 'clearTestState', {
|
2015-10-15 10:12:28 +00:00
|
|
|
value: function() {
|
2015-10-16 01:48:13 +00:00
|
|
|
rpc.clearTestState();
|
2015-10-15 10:12:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
module.exports = Realm;
|