typedux
Version:
Slightly adjusted Redux (awesome by default) for TS
62 lines • 2.16 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.VariableProxy = void 0;
const index_1 = require("./index");
const logger_proxy_1 = require("@3fv/logger-proxy");
const dev_1 = require("../dev");
const log = logger_proxy_1.getLogger(__filename);
/**
* A reloadable proxy between a target object
* and the end consumer.
*
* Implemented specifically for hot loading
*/
class VariableProxy {
constructor(target, ignoredProps = []) {
this.ignoredProps = ignoredProps;
this.proxy = {};
this.changeTarget(target);
}
addProxies(o) {
if (!o)
return;
let keys = Object.getOwnPropertyNames(o);
if (log.isInfoEnabled() && dev_1.isDev) {
log.info("Creating proxy for", keys);
}
keys.forEach((prop) => {
if (!/(constructor|prototype|__proto__)/.test(prop) &&
!this.proxy[prop] &&
this.ignoredProps.indexOf(prop) < 0) {
if (index_1.isFunction(this.target[prop])) {
this.proxy[prop] = (...args) => {
return this.target[prop].apply(this.target, args);
};
}
else {
Object.defineProperty(this.proxy, prop, {
get: () => {
return this.target[prop];
},
set: (newVal) => {
return this.target[prop] = newVal;
}
});
}
}
});
}
changeTarget(target) {
this.target = target;
// Update the proxy to ensure all methods are available
// this.addProxies(_.get(this,'target'))
let proto = index_1.getProperty(this, 'target.constructor.prototype');
while (proto) {
this.addProxies(proto);
proto = index_1.getProperty(proto, '__proto__');
}
// this.addProxies(_.get(this,'target.__proto__'))
}
}
exports.VariableProxy = VariableProxy;
//# sourceMappingURL=VariableProxy.js.map
;