realm-js/lib/extensions.js

198 lines
7.2 KiB
JavaScript
Raw Normal View History

2016-10-10 14:17:05 +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.
//
////////////////////////////////////////////////////////////////////////////
'use strict';
let getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors || function(obj) {
return Object.getOwnPropertyNames(obj).reduce(function (descriptors, name) {
descriptors[name] = Object.getOwnPropertyDescriptor(obj, name);
return descriptors;
}, {});
};
2017-02-02 01:41:11 +00:00
function setConstructorOnPrototype(klass) {
if (klass.prototype.constructor !== klass) {
Object.defineProperty(klass.prototype, 'constructor', { value: klass, configurable: true, writable: true });
}
}
// Return a configuration usable by `Realm.open` when waiting for a download.
// It must have caching disabled, and no schema or schema version specified.
function waitForDownloadConfig(config) {
if (!config) {
return {_cache: false};
}
if (typeof config == 'string') {
return {path: config, _cache: false};
}
if (typeof config == 'object') {
return Object.assign({}, config, {schema: undefined, schemaVersion: undefined, _cache: false});
}
// Unknown type. Pass the config through.
return config;
}
2016-10-10 14:17:05 +00:00
module.exports = function(realmConstructor) {
// Add the specified Array methods to the Collection prototype.
Object.defineProperties(realmConstructor.Collection.prototype, require('./collection-methods'));
2017-02-02 01:41:11 +00:00
setConstructorOnPrototype(realmConstructor.Collection);
setConstructorOnPrototype(realmConstructor.List);
setConstructorOnPrototype(realmConstructor.Results);
setConstructorOnPrototype(realmConstructor.Object);
2017-05-06 23:26:14 +00:00
//Add async open API
Object.defineProperties(realmConstructor, getOwnPropertyDescriptors({
open(config) {
2017-09-01 15:12:06 +00:00
let syncSession;
let promise = new Promise((resolve, reject) => {
let realm = new realmConstructor(waitForDownloadConfig(config));
realm._waitForDownload(
2017-09-01 15:12:06 +00:00
(session) => {
syncSession = session;
},
(error) => {
if (error) {
2017-09-08 16:56:08 +00:00
setTimeout(() => { reject(error); }, 1);
}
2017-09-01 15:12:06 +00:00
else {
try {
let syncedRealm = new realmConstructor(config);
2017-11-13 21:31:33 +00:00
setTimeout(() => { resolve(syncedRealm); }, 1);
2017-09-01 15:12:06 +00:00
} catch (e) {
reject(e);
}
}
});
2017-05-06 23:26:14 +00:00
});
2017-09-01 15:12:06 +00:00
promise.progress = (callback) => {
if (syncSession) {
syncSession.addProgressNotification('download', 'forCurrentlyOutstandingWork', callback);
}
return promise;
};
return promise;
2017-05-06 23:26:14 +00:00
},
2017-09-13 09:37:44 +00:00
openAsync(config, callback, progressCallback) {
2017-08-24 18:01:12 +00:00
const message = "Realm.openAsync is now deprecated in favor of Realm.open. This function will be removed in future versions.";
(console.warn || console.log).call(console, message);
2017-10-02 17:44:24 +00:00
let promise = this.open(config)
if (progressCallback) {
promise.progress(progressCallback)
}
promise.then(realm => {
callback(null, realm)
}).catch(error => {
callback(error);
});
},
2017-05-06 23:26:14 +00:00
}));
2016-10-10 14:17:05 +00:00
// Add sync methods
if (realmConstructor.Sync) {
let userMethods = require('./user-methods');
Object.defineProperties(realmConstructor.Sync.User, getOwnPropertyDescriptors(userMethods.static));
Object.defineProperties(realmConstructor.Sync.User.prototype, getOwnPropertyDescriptors(userMethods.instance));
Object.defineProperty(realmConstructor.Sync.User, '_realmConstructor', { value: realmConstructor });
realmConstructor.Sync.AuthError = require('./errors').AuthError;
2016-10-25 22:04:58 +00:00
if (realmConstructor.Sync.removeAllListeners) {
process.on('exit', realmConstructor.Sync.removeAllListeners);
2016-11-20 18:34:13 +00:00
process.on('SIGINT', function () {
realmConstructor.Sync.removeAllListeners();
2016-11-20 18:34:13 +00:00
process.exit(2);
});
process.on('uncaughtException', function(e) {
realmConstructor.Sync.removeAllListeners();
2016-11-21 23:33:37 +00:00
/* eslint-disable no-console */
2016-11-20 18:34:13 +00:00
console.log(e.stack);
process.exit(99);
});
2016-10-25 22:04:58 +00:00
}
2017-02-02 01:41:11 +00:00
setConstructorOnPrototype(realmConstructor.Sync.User);
setConstructorOnPrototype(realmConstructor.Sync.Session);
if (realmConstructor.Sync._setFeatureToken) {
realmConstructor.Sync.setFeatureToken = function(featureToken) {
2017-09-01 09:53:52 +00:00
if (typeof featureToken !== 'string' && !(featureToken instanceof String)) {
throw new Error("featureToken should be a string");
}
realmConstructor.Sync._setFeatureToken(featureToken.trim());
}
}
2017-10-02 18:29:36 +00:00
realmConstructor.prototype.subscribeToObjects = function(objectType, query) {
const realm = this;
let promise = new Promise((resolve, reject) => {
realm._subscribeToObjects(objectType, query, function(err, results) {
if (err) {
reject(err);
} else {
resolve(results);
}
2017-10-02 18:29:36 +00:00
});
});
return promise;
};
2016-10-10 14:17:05 +00:00
}
// TODO: Remove this now useless object.
var types = Object.freeze({
'BOOL': 'bool',
'INT': 'int',
'FLOAT': 'float',
'DOUBLE': 'double',
'STRING': 'string',
'DATE': 'date',
'DATA': 'data',
'OBJECT': 'object',
'LIST': 'list',
});
Object.defineProperty(realmConstructor, 'Types', {
get: function() {
if (typeof console != 'undefined') {
/* global console */
/* eslint-disable no-console */
var stack = new Error().stack.split("\n").slice(2).join("\n");
var msg = '`Realm.Types` is deprecated! Please specify the type name as lowercase string instead!\n'+stack;
if (console.warn != undefined) {
console.warn(msg);
}
else {
console.log(msg);
}
/* eslint-enable no-console */
}
return types;
},
configurable: true
});
}