respond-framework
Version:
create as fast you think
34 lines (33 loc) • 1.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _cloneDeep = require("./helpers/cloneDeep.js");
var _utils = require("./helpers/utils.js");
const snapshot = function (proxy = this.state, vns = this.versionNotifiers, cache = this.snapCache) {
const vn = vns.get(proxy);
return vn ? createSnapshot(vn, vns, cache) : (0, _cloneDeep.default)(proxy); // because snapshots before proxication otherwise return the entire original object, and need to be separate references -- important for triggerPlugin's creation of prevState, which must not share references with the current state, or prevState will be the same as current state
};
var _default = exports.default = snapshot;
function createSnapshot({
obj: o,
version
}, vns, cache) {
const {
version: v,
snap: s
} = cache.get(o) ?? {};
if (v === version) return s;
const snap = (0, _utils.isArray)(o) ? [] : (0, _utils.create)((0, _utils.getProto)(o)); // computed methods/selectors on prototype
cache.set(o, {
snap,
version
});
(0, _utils.keys)(o).forEach(k => snap[k] = recurse(o[k], vns, cache));
return snap;
}
function recurse(proxy, vns, cache) {
const vn = vns.get(proxy);
return vn ? createSnapshot(vn, vns, cache) : proxy; // proxy : primitive value
}