respond-framework
Version:
create as fast you think
77 lines (73 loc) • 3.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _createSnapProxy = require("../createSnapProxy.js");
var _recordUsage = require("./recordUsage.js");
var _sliceBranch = require("../../createModule/helpers/sliceBranch.js");
var _useSnapshotPrevState = require("../useSnapshotPrevState.js");
var _utils = require("./utils.js");
var _reserved = require("../../createModule/reserved.js");
const createSnapHandler = (snap, state) => {
const handler = {
has(snap, k) {
(0, _recordUsage.default)(state, snap, k, has);
return Reflect.has(snap, k);
},
getOwnPropertyDescriptor(snap, k) {
(0, _recordUsage.default)(state, snap, k, gopd);
return Reflect.getOwnPropertyDescriptor(snap, k);
},
ownKeys(snap) {
(0, _recordUsage.default)(state, snap, ownKeys, ownKeys);
return Reflect.ownKeys(snap);
},
get(snap, k) {
(0, _recordUsage.default)(state, snap, k);
const v = Reflect.get(snap, k);
return (0, _utils.canProxy)(v) ? (0, _createSnapProxy.default)(v, state) : v;
}
};
if (snap[_reserved._module]) handler.get = getModule(state, (0, _utils.getOpd)((0, _utils.getProto)(snap)));
if (snap.__type) handler.get = getModel(state, (0, _utils.getOpd)((0, _utils.getProto)(snap)));
return handler;
};
var _default = exports.default = createSnapHandler;
const ownKeys = 'ownKeys';
const has = 'has';
const gopd = 'getOwnPropertyDescriptor';
// variants for get handler:
const getModule = (state, protoDescriptors) => (snap, k, proxy) => {
if (k === _reserved._parent) return state.parentProxy;
if (k === 'prevState') {
const topSnapPrevState = (0, _useSnapshotPrevState.default)(snap.respond.topState); // need to treverse from top to facilitate props reactivity from parents which possibly gets passed from top
return (0, _sliceBranch.default)(topSnapPrevState, snap.respond.branch); // slice to current module prevState
}
if (protoDescriptors[k]) {
const {
get,
value
} = protoDescriptors[k];
return get ? get.call(proxy) : value; // getter -- needs proxy assigned to `this` and called now (as expected by getters), as getter function is otherwise not bound to proxy
}
(0, _recordUsage.default)(state, snap, k);
const v = Reflect.get(snap, k);
return (0, _utils.canProxy)(v) ? (0, _createSnapProxy.default)(v, state) : v;
};
const getModel = (state, protoDescriptors) => (snap, k, proxy) => {
if (protoDescriptors[k] && !snap.hasOwnProperty(k)) {
// check hasOwnProperty to allow model protos to supply default overriable values on instances
const {
get,
value
} = protoDescriptors[k];
if (get) return get.call(proxy); // getter -- needs proxy assigned to `this` and called now (as expected by getters), as getter function is otherwise not bound to proxy
if (typeof value === 'function') return value; // will automatically be called as method with proxy as `this` by virtue of being called on an object in userland, eg: foo.bar()
(0, _recordUsage.default)(state, snap, k); // record usage, as value may be assigned to state, overriding proto, in the future -- could also be proto.prevState, which is made into a snapshot separately and benefits from the same immutable isChanged/affected reactivity
return value;
}
(0, _recordUsage.default)(state, snap, k);
const v = Reflect.get(snap, k);
return (0, _utils.canProxy)(v) ? (0, _createSnapProxy.default)(v, state) : v;
};