realm-js/lib/realm.js

136 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-10-28 17:37:17 +00:00
/* Copyright 2015 Realm Inc - All Rights Reserved
* Proprietary and Confidential
*/
'use strict';
const constants = require('./constants');
const lists = require('./lists');
const objects = require('./objects');
const results = require('./results');
const rpc = require('./rpc');
const util = require('./util');
const {keys, propTypes, objectTypes} = constants;
2015-10-27 14:48:11 +00:00
const listenersKey = Symbol();
rpc.registerTypeConverter(propTypes.LIST, lists.create);
rpc.registerTypeConverter(propTypes.OBJECT, objects.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[listenersKey] = new Set();
[
'path',
'schemaVersion',
].forEach((name) => {
Object.defineProperty(this, name, {get: util.getterForProperty(name)});
});
}
2015-10-27 02:18:24 +00:00
addListener(name, callback) {
if (typeof callback != 'function') {
2015-10-26 23:49:46 +00:00
throw new Error('Realm.addListener must be passed a function!');
}
2015-10-27 02:18:24 +00:00
if (name != 'change') {
throw new Error("Only 'change' notification is supported.");
}
this[listenersKey].add(callback);
2015-10-26 23:49:46 +00:00
}
2015-10-27 02:18:24 +00:00
removeListener(name, callback) {
2015-10-26 23:49:46 +00:00
if (typeof callback != 'function') {
throw new Error('Realm.removeListener must be passed a function!');
2015-10-26 23:49:46 +00:00
}
2015-10-27 02:18:24 +00:00
if (name != 'change') {
throw new Error("Only 'change' notification is supported.");
}
this[listenersKey].delete(callback);
2015-10-26 23:49:46 +00:00
}
2015-10-27 02:18:24 +00:00
removeAllListeners(name) {
if (name != undefined && name != 'change') {
2015-10-27 02:18:24 +00:00
throw new Error("Only 'change' notification is supported.");
}
this[listenersKey].clear();
2015-10-20 00:28:20 +00:00
}
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);
util.fireMutationListeners(realmId);
throw e;
}
2015-10-08 22:32:14 +00:00
rpc.commitTransaction(realmId);
this[listenersKey].forEach((cb) => cb(this, 'change'));
}
}
// Non-mutating methods:
util.createMethods(Realm.prototype, objectTypes.REALM, [
'close',
'objects',
]);
// Mutating methods:
util.createMethods(Realm.prototype, objectTypes.REALM, [
'create',
'delete',
'deleteAll',
], true);
Object.defineProperties(Realm, {
Types: {
value: Object.freeze(propTypes),
},
defaultPath: {
get: util.getterForProperty('defaultPath'),
set: util.setterForProperty('defaultPath'),
},
clearTestState: {
value: rpc.clearTestState,
},
});
// The session ID refers to the Realm constructor object in the RPC server.
Realm[keys.id] = rpc.createSession();
module.exports = Realm;