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-04-27 00:08:00 +00:00
|
|
|
import { keys, objectTypes } from './constants';
|
2016-04-26 23:53:50 +00:00
|
|
|
import { getterForProperty, setterForProperty, createMethods } from './util';
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2016-05-06 22:42:17 +00:00
|
|
|
let registeredConstructors = {};
|
|
|
|
let registeredRealmPaths = {};
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2016-04-26 23:30:04 +00:00
|
|
|
export default class RealmObject {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-mutating methods:
|
|
|
|
createMethods(RealmObject.prototype, objectTypes.OBJECT, [
|
|
|
|
'isValid',
|
2017-06-29 09:59:10 +00:00
|
|
|
'objectSchema',
|
2017-11-08 10:22:59 +00:00
|
|
|
'linkingObjects',
|
2018-05-15 12:37:04 +00:00
|
|
|
'linkingObjectsCount',
|
2017-11-08 10:22:59 +00:00
|
|
|
'_objectId',
|
|
|
|
'_isSameObject',
|
2016-04-26 23:30:04 +00:00
|
|
|
]);
|
|
|
|
|
2016-05-06 22:42:17 +00:00
|
|
|
export function clearRegisteredConstructors() {
|
|
|
|
registeredConstructors = {};
|
|
|
|
registeredRealmPaths = {};
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:32:55 +00:00
|
|
|
export function createObject(realmId, info) {
|
2015-10-02 05:56:47 +00:00
|
|
|
let schema = info.schema;
|
2016-05-06 22:42:17 +00:00
|
|
|
let realmPath = registeredRealmPaths[realmId];
|
|
|
|
let constructor = (registeredConstructors[realmPath] || {})[schema.name];
|
2016-04-27 00:32:55 +00:00
|
|
|
let object = Object.create(constructor ? constructor.prototype : RealmObject.prototype);
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
object[keys.realm] = realmId;
|
|
|
|
object[keys.id] = info.id;
|
|
|
|
object[keys.type] = info.type;
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-11-03 05:54:05 +00:00
|
|
|
schema.properties.forEach((name) => {
|
2015-11-25 18:48:51 +00:00
|
|
|
Object.defineProperty(object, name, {
|
|
|
|
enumerable: true,
|
2016-02-28 20:36:30 +00:00
|
|
|
get: getterForProperty(name),
|
|
|
|
set: setterForProperty(name),
|
2015-11-25 18:48:51 +00:00
|
|
|
});
|
2015-11-06 00:10:52 +00:00
|
|
|
});
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2016-02-18 19:09:51 +00:00
|
|
|
if (constructor) {
|
|
|
|
let result = constructor.call(object);
|
|
|
|
if (result != null && result != object) {
|
|
|
|
throw new Error('Realm object constructor must not return another value');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2016-05-06 22:42:17 +00:00
|
|
|
export function registerConstructors(realmId, realmPath, constructors) {
|
|
|
|
registeredRealmPaths[realmId] = realmPath;
|
|
|
|
|
|
|
|
if (constructors) {
|
|
|
|
registeredConstructors[realmPath] = constructors;
|
|
|
|
}
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
2016-02-25 01:34:20 +00:00
|
|
|
|
|
|
|
export function typeForConstructor(realmId, constructor) {
|
2016-05-06 22:42:17 +00:00
|
|
|
let realmPath = registeredRealmPaths[realmId];
|
|
|
|
let constructors = registeredConstructors[realmPath];
|
2016-02-25 01:34:20 +00:00
|
|
|
|
|
|
|
for (let name in constructors) {
|
|
|
|
if (constructors[name] == constructor) {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 20:16:42 +00:00
|
|
|
throw new Error("Constructor was not registered in the schema for this Realm")
|
2016-02-25 01:34:20 +00:00
|
|
|
}
|