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
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-29 11:24:51 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
2016-02-28 20:36:30 +00:00
|
|
|
import { keys, propTypes, objectTypes } from './constants';
|
|
|
|
import * as lists from './lists';
|
|
|
|
import * as objects from './objects';
|
|
|
|
import * as results from './results';
|
|
|
|
import * as rpc from './rpc';
|
|
|
|
import * as util from './util';
|
|
|
|
|
2016-02-29 11:24:51 +00:00
|
|
|
const {debugHosts, debugPort} = NativeModules.Realm;
|
2015-10-27 14:48:11 +00:00
|
|
|
const listenersKey = Symbol();
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-11-03 06:16:50 +00:00
|
|
|
rpc.registerTypeConverter(objectTypes.LIST, lists.create);
|
|
|
|
rpc.registerTypeConverter(objectTypes.OBJECT, objects.create);
|
2015-10-19 23:19:43 +00:00
|
|
|
rpc.registerTypeConverter(objectTypes.RESULTS, results.create);
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2016-02-28 20:36:30 +00:00
|
|
|
export default class Realm {
|
2015-10-02 05:56:47 +00:00
|
|
|
constructor(config) {
|
2016-02-18 19:09:51 +00:00
|
|
|
let schemas = typeof config == 'object' && config.schema;
|
2015-10-02 05:56:47 +00:00
|
|
|
let constructors = {};
|
2015-10-15 01:00:21 +00:00
|
|
|
|
2016-02-18 19:09:51 +00:00
|
|
|
for (let i = 0, len = schemas ? schemas.length : 0; i < len; i++) {
|
|
|
|
let item = schemas[i];
|
2015-11-03 10:41:52 +00:00
|
|
|
|
2016-02-18 19:09:51 +00:00
|
|
|
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;
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
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;
|
2015-10-19 23:19:43 +00:00
|
|
|
this[keys.type] = objectTypes.REALM;
|
2015-11-03 10:41:52 +00:00
|
|
|
this[listenersKey] = new Set();
|
2015-10-19 22:46:28 +00:00
|
|
|
|
|
|
|
[
|
|
|
|
'path',
|
|
|
|
'schemaVersion',
|
|
|
|
].forEach((name) => {
|
|
|
|
Object.defineProperty(this, name, {get: util.getterForProperty(name)});
|
|
|
|
});
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 01:34:20 +00:00
|
|
|
create(type, ...args) {
|
|
|
|
if (typeof type == 'function') {
|
|
|
|
type = objects.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') {
|
|
|
|
type = objects.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) {
|
2015-10-15 01:00:21 +00:00
|
|
|
if (typeof callback != 'function') {
|
2015-10-26 23:49:46 +00:00
|
|
|
throw new Error('Realm.addListener must be passed a function!');
|
2015-10-15 01:00:21 +00:00
|
|
|
}
|
2015-10-27 02:18:24 +00:00
|
|
|
if (name != 'change') {
|
|
|
|
throw new Error("Only 'change' notification is supported.");
|
|
|
|
}
|
2015-11-03 10:41:52 +00:00
|
|
|
this[listenersKey].add(callback);
|
2015-10-26 23:49:46 +00:00
|
|
|
}
|
2015-10-15 01:00:21 +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') {
|
2015-11-03 10:41:52 +00:00
|
|
|
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.");
|
|
|
|
}
|
2015-11-03 10:41:52 +00:00
|
|
|
this[listenersKey].delete(callback);
|
2015-10-26 23:49:46 +00:00
|
|
|
}
|
2015-10-15 01:00:21 +00:00
|
|
|
|
2015-10-27 02:18:24 +00:00
|
|
|
removeAllListeners(name) {
|
2015-11-03 10:41:52 +00:00
|
|
|
if (name != undefined && name != 'change') {
|
2015-10-27 02:18:24 +00:00
|
|
|
throw new Error("Only 'change' notification is supported.");
|
|
|
|
}
|
2015-11-03 10:41:52 +00:00
|
|
|
this[listenersKey].clear();
|
2015-10-20 00:28:20 +00:00
|
|
|
}
|
|
|
|
|
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-28 17:21:32 +00:00
|
|
|
util.fireMutationListeners(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
|
|
|
|
2015-11-06 00:10:52 +00:00
|
|
|
this[listenersKey].forEach((cb) => cb(this, 'change'));
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 17:21:32 +00:00
|
|
|
// Non-mutating methods:
|
2015-10-19 23:19:43 +00:00
|
|
|
util.createMethods(Realm.prototype, objectTypes.REALM, [
|
2015-10-14 23:06:20 +00:00
|
|
|
'close',
|
2015-10-28 17:21:32 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Mutating methods:
|
|
|
|
util.createMethods(Realm.prototype, objectTypes.REALM, [
|
2015-10-08 16:43:38 +00:00
|
|
|
'delete',
|
|
|
|
'deleteAll',
|
2015-10-28 17:21:32 +00:00
|
|
|
], true);
|
2015-10-08 16:43:38 +00:00
|
|
|
|
2015-10-19 23:59:04 +00:00
|
|
|
Object.defineProperties(Realm, {
|
2016-02-28 20:35:54 +00:00
|
|
|
List: {
|
|
|
|
value: lists.List,
|
|
|
|
},
|
|
|
|
Results: {
|
|
|
|
value: results.Results,
|
|
|
|
},
|
2015-10-19 23:59:04 +00:00
|
|
|
Types: {
|
|
|
|
value: Object.freeze(propTypes),
|
|
|
|
},
|
|
|
|
defaultPath: {
|
2015-10-20 22:10:22 +00:00
|
|
|
get: util.getterForProperty('defaultPath'),
|
|
|
|
set: util.setterForProperty('defaultPath'),
|
2015-10-19 23:59:04 +00:00
|
|
|
},
|
|
|
|
clearTestState: {
|
2015-11-03 00:29:13 +00:00
|
|
|
value: function() {
|
|
|
|
util.clearMutationListeners();
|
|
|
|
rpc.clearTestState();
|
|
|
|
},
|
2015-10-19 23:59:04 +00:00
|
|
|
},
|
2015-10-15 10:12:28 +00:00
|
|
|
});
|
|
|
|
|
2016-02-29 11:24:51 +00:00
|
|
|
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.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|