2015-10-02 05:56:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
let keys = require('./keys');
|
2015-10-06 19:36:56 +00:00
|
|
|
let rpc = require('./rpc');
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-06 07:52:15 +00:00
|
|
|
let registeredConstructors = {};
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
module.exports = {
|
|
|
|
create,
|
|
|
|
registerConstructors,
|
|
|
|
};
|
2015-10-02 05:56:47 +00:00
|
|
|
|
|
|
|
function create(realmId, info) {
|
|
|
|
let schema = info.schema;
|
2015-10-06 07:52:15 +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 = {};
|
|
|
|
|
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
|
|
|
|
|
|
|
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 07:52:15 +00:00
|
|
|
function registerConstructors(realmId, constructors) {
|
|
|
|
registeredConstructors[realmId] = constructors;
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getterForProperty(name) {
|
|
|
|
return function() {
|
2015-10-19 19:06:47 +00:00
|
|
|
return rpc.getObjectProperty(this[keys.realm], this[keys.id], name);
|
2015-10-02 05:56:47 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function setterForProperty(name) {
|
|
|
|
return function(value) {
|
2015-10-19 19:06:47 +00:00
|
|
|
rpc.setObjectProperty(this[keys.realm], this[keys.id], name, value);
|
2015-10-02 05:56:47 +00:00
|
|
|
};
|
2015-10-08 22:32:14 +00:00
|
|
|
}
|