dev: clone property descriptors in Context

This commit is contained in:
KernelDeimos 2024-10-19 03:45:33 -04:00
parent c2a475f3c0
commit 6e5d5c2021

View File

@ -18,8 +18,20 @@
*/ */
class Context { class Context {
constructor (values) { constructor (values = {}) {
for ( const k in values ) this[k] = values[k]; const descs = Object.getOwnPropertyDescriptors(values);
for ( const k in descs ) {
Object.defineProperty(this, k, descs[k]);
}
}
follow (source, keys) {
const values = {};
for ( const k of keys ) {
Object.defineProperty(values, k, {
get: () => source[k]
});
}
return this.sub(values);
} }
sub (newValues) { sub (newValues) {
if ( newValues === undefined ) newValues = {}; if ( newValues === undefined ) newValues = {};
@ -36,9 +48,10 @@ class Context {
} }
} }
for ( const k in newValues ) { const descs = Object.getOwnPropertyDescriptors(newValues);
for ( const k in descs ){
if ( alreadyApplied[k] ) continue; if ( alreadyApplied[k] ) continue;
sub[k] = newValues[k]; Object.defineProperty(sub, k, descs[k]);
} }
return sub; return sub;