realm-js/lib/browser/index.js

226 lines
6.8 KiB
JavaScript
Raw Normal View History

2016-02-18 19:59:34 +00:00
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
2015-10-28 17:37:17 +00:00
'use strict';
import { NativeModules } from 'react-native';
import { keys, propTypes, objectTypes } from './constants';
import Collection, * as collections from './collections';
import List, { createList } from './lists';
import Results, { createResults } from './results';
2016-04-27 00:32:55 +00:00
import RealmObject, { createObject, registerConstructors, typeForConstructor } from './objects';
import * as rpc from './rpc';
import * as util from './util';
const {debugHosts, debugPort} = NativeModules.Realm;
2015-10-27 14:48:11 +00:00
const listenersKey = Symbol();
rpc.registerTypeConverter(objectTypes.LIST, createList);
rpc.registerTypeConverter(objectTypes.RESULTS, createResults);
2016-04-27 00:32:55 +00:00
rpc.registerTypeConverter(objectTypes.OBJECT, createObject);
rpc.registerTypeConverter(objectTypes.REALM, createRealm);
function createRealm(_, info) {
let realm = Object.create(Realm.prototype);
setupRealm(realm, info.id);
return realm;
}
function setupRealm(realm, realmId) {
realm[keys.id] = realmId;
realm[keys.realm] = realmId;
realm[keys.type] = objectTypes.REALM;
realm[listenersKey] = new Set();
[
'path',
'readOnly',
'schema',
'schemaVersion',
].forEach((name) => {
Object.defineProperty(realm, name, {get: util.getterForProperty(name)});
});
}
export default class Realm {
constructor(config) {
let schemas = typeof config == 'object' && config.schema;
let constructors = {};
for (let i = 0, len = schemas ? schemas.length : 0; i < len; i++) {
let item = schemas[i];
if (typeof item == 'function') {
let schema = item.schema;
if (!schema || typeof schema != 'object') {
throw new Error("Realm object constructor must have 'schema' property");
}
let {name, properties} = schema;
if (!name || typeof name != 'string') {
throw new Error("Realm object schema must have 'name' property");
} else if (!properties || typeof properties != 'object') {
throw new Error("Realm object schema must have 'properties' property");
}
schemas.splice(i, 1, schema);
constructors[name] = item;
}
}
let realmId = rpc.createRealm(Array.from(arguments));
2016-04-27 00:32:55 +00:00
registerConstructors(realmId, constructors);
setupRealm(this, realmId);
}
create(type, ...args) {
if (typeof type == 'function') {
2016-04-27 00:32:55 +00:00
type = typeForConstructor(this[keys.realm], type);
}
let method = util.createMethod(objectTypes.REALM, 'create', true);
return method.apply(this, [type, ...args]);
}
objects(type, ...args) {
if (typeof type == 'function') {
2016-04-27 00:32:55 +00:00
type = typeForConstructor(this[keys.realm], type);
}
let method = util.createMethod(objectTypes.REALM, 'objects');
return method.apply(this, [type, ...args]);
}
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);
collections.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',
]);
// Mutating methods:
util.createMethods(Realm.prototype, objectTypes.REALM, [
'delete',
'deleteAll',
], true);
Object.defineProperties(Realm, {
Collection: {
value: Collection,
},
List: {
value: List,
},
Results: {
value: Results,
},
2016-04-27 00:32:55 +00:00
Object: {
value: RealmObject,
},
defaultPath: {
get: util.getterForProperty('defaultPath'),
set: util.setterForProperty('defaultPath'),
},
schemaVersion: {
value: function(path, encryptionKey) {
return rpc.callMethod(undefined, Realm[keys.id], 'schemaVersion', Array.from(arguments));
}
},
clearTestState: {
value: function() {
collections.clearMutationListeners();
rpc.clearTestState();
},
},
});
for (let i = 0, len = debugHosts.length; i < len; i++) {
try {
// The session ID refers to the Realm constructor object in the RPC server.
Realm[keys.id] = rpc.createSession(debugHosts[i] + ':' + debugPort);
break;
} catch (e) {
// Only throw exception after all hosts have been tried.
if (i < len - 1) {
continue;
}
// Log the original exception for debugging purposes.
console.error(e);
throw new Error(
'Realm failed to connect to the embedded debug server inside the app. ' +
'If attempting to use Chrome debugging from a device, ensure the device is ' +
'reachable on the same network as this machine.'
);
}
}