respond-framework
Version:
create as fast you think
31 lines (26 loc) • 1.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = createProxy;
var _createHandler = require("./helpers/createHandler.js");
var _findExistingProxy = require("./helpers/findExistingProxy.js");
var _reserved = require("../createModule/reserved.js");
var _utils = require("./helpers/utils.js");
var _VersionNotifier = require("./VersionNotifier.js");
function createProxy(o, vns, cache, refIds, notify = function () {}) {
const found = (0, _findExistingProxy.default)(o, vns, cache, refIds, notify); // proxy exists elsewhere and can be re-used; parents.add(notify) will be called to re-make the link
if (found) return found;
const obj = (0, _utils.isArray)(o) ? [] : (0, _utils.create)((0, _utils.getProto)(o)); // prepare clone
const VersionNotifier = o[_reserved._module] ? o[_reserved._top] ? _VersionNotifier.VNTop : _VersionNotifier.VNModule : _VersionNotifier.VersionNotifier; // polymorphic version notifier -- based on whether module, top module, or other objects
const vn = new VersionNotifier(obj);
const proxy = new Proxy(obj, (0, _createHandler.default)(vns, cache, refIds, vn.notify)); // vn.notify allows assigned children to notify parents of changes
cache.set(o, proxy); // obj -> proxy
vns.set(proxy, vn); // proxy -> vn
vn.parents.add(notify); // proxy/obj/vn trio now notify parents of changes -- `notify` is vn.notify function of parent
Object.keys(o).forEach(k => {
const v = o[k];
obj[k] = (0, _utils.canProxy)(v) ? createProxy(v, vns, cache, refIds, vn.notify) : v; // copy to clone, making proxies where necessary
});
return proxy;
}