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-08 00:08:19 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-09 07:47:08 +00:00
|
|
|
import { fireMutationListeners } from './collections';
|
2016-02-28 20:36:30 +00:00
|
|
|
import { keys } from './constants';
|
|
|
|
import * as rpc from './rpc';
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2016-02-28 20:36:30 +00:00
|
|
|
export function createMethods(prototype, type, methodNames, mutates) {
|
2015-10-19 19:06:47 +00:00
|
|
|
let props = {};
|
|
|
|
|
2015-11-06 00:10:52 +00:00
|
|
|
methodNames.forEach((name) => {
|
2015-10-19 19:06:47 +00:00
|
|
|
props[name] = {
|
2015-10-28 17:21:32 +00:00
|
|
|
value: createMethod(type, name, mutates),
|
2015-10-20 00:28:20 +00:00
|
|
|
};
|
2015-11-06 00:10:52 +00:00
|
|
|
});
|
2015-10-19 19:06:47 +00:00
|
|
|
|
2015-10-20 00:28:20 +00:00
|
|
|
Object.defineProperties(prototype, props);
|
|
|
|
}
|
2015-10-19 19:06:47 +00:00
|
|
|
|
2016-02-28 20:36:30 +00:00
|
|
|
export function createMethod(type, name, mutates) {
|
2015-10-20 00:28:20 +00:00
|
|
|
return function() {
|
|
|
|
let realmId = this[keys.realm];
|
|
|
|
let id = this[keys.id];
|
|
|
|
|
|
|
|
if (!realmId || !id) {
|
2017-09-08 16:55:23 +00:00
|
|
|
throw new TypeError(name + ' method was not called on a Realm object!');
|
2015-10-20 00:28:20 +00:00
|
|
|
}
|
|
|
|
if (this[keys.type] !== type) {
|
|
|
|
throw new TypeError(name + ' method was called on an object of the wrong type!');
|
|
|
|
}
|
2015-10-19 19:06:47 +00:00
|
|
|
|
2016-05-02 19:41:53 +00:00
|
|
|
try {
|
|
|
|
return rpc.callMethod(realmId, id, name, Array.from(arguments));
|
|
|
|
} finally {
|
|
|
|
if (mutates) {
|
|
|
|
fireMutationListeners(realmId);
|
|
|
|
}
|
2015-10-20 00:28:20 +00:00
|
|
|
}
|
|
|
|
};
|
2015-10-19 19:06:47 +00:00
|
|
|
}
|
2015-10-19 22:26:42 +00:00
|
|
|
|
2016-02-28 20:36:30 +00:00
|
|
|
export function getterForProperty(name) {
|
2015-10-19 22:26:42 +00:00
|
|
|
return function() {
|
|
|
|
return rpc.getProperty(this[keys.realm], this[keys.id], name);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-02-28 20:36:30 +00:00
|
|
|
export function setterForProperty(name) {
|
2015-10-19 22:26:42 +00:00
|
|
|
return function(value) {
|
2015-10-28 17:21:32 +00:00
|
|
|
let realmId = this[keys.realm];
|
|
|
|
|
|
|
|
rpc.setProperty(realmId, this[keys.id], name, value);
|
|
|
|
|
|
|
|
// If this isn't a primitive value, then it might create a new object in the Realm.
|
|
|
|
if (value && typeof value == 'object') {
|
|
|
|
fireMutationListeners(realmId);
|
|
|
|
}
|
2015-10-19 22:26:42 +00:00
|
|
|
};
|
|
|
|
}
|