2015-10-02 05:56:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-06 19:36:56 +00:00
|
|
|
let rpc = require('./rpc');
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-06 19:36:56 +00:00
|
|
|
let idKey = Symbol();
|
|
|
|
let realmKey = Symbol();
|
|
|
|
let schemaKey = Symbol();
|
|
|
|
let registeredconstructors = {};
|
2015-10-02 05:56:47 +00:00
|
|
|
|
|
|
|
exports.create = create;
|
2015-10-06 19:36:56 +00:00
|
|
|
exports.registerconstructors = registerconstructors;
|
2015-10-02 05:56:47 +00:00
|
|
|
|
|
|
|
function create(realmId, info) {
|
|
|
|
let schema = info.schema;
|
2015-10-06 19:36:56 +00:00
|
|
|
let constructor = (registeredconstructors[realmId] || {})[schema.name];
|
2015-10-02 05:56:47 +00:00
|
|
|
let object = constructor ? Object.create(constructor.prototype) : {};
|
|
|
|
let props = {};
|
|
|
|
|
|
|
|
object[realmKey] = realmId;
|
|
|
|
object[idKey] = info.id;
|
|
|
|
object[schemaKey] = schema;
|
|
|
|
|
|
|
|
for (let prop of schema.properties) {
|
|
|
|
let name = prop.name;
|
|
|
|
|
|
|
|
props[name] = {
|
|
|
|
get: getterForProperty(name),
|
|
|
|
set: setterForProperty(name),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperties(object, props);
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:36:56 +00:00
|
|
|
function registerconstructors(realmId, constructors) {
|
|
|
|
registeredconstructors[realmId] = constructors;
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getterForProperty(name) {
|
|
|
|
return function() {
|
|
|
|
let realmId = this[realmKey];
|
|
|
|
return rpc.getObjectProperty(realmId, this[idKey], name);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function setterForProperty(name) {
|
|
|
|
return function(value) {
|
|
|
|
rpc.setObjectProperty(this[realmKey], this[idKey], name, value);
|
|
|
|
};
|
|
|
|
}
|