From 6e5d5c2021a4df7f5b96af3189a03cff28cbb5db Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Sat, 19 Oct 2024 03:45:33 -0400 Subject: [PATCH] dev: clone property descriptors in Context --- src/putility/src/libs/context.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/putility/src/libs/context.js b/src/putility/src/libs/context.js index fb65238b..7201f410 100644 --- a/src/putility/src/libs/context.js +++ b/src/putility/src/libs/context.js @@ -18,8 +18,20 @@ */ class Context { - constructor (values) { - for ( const k in values ) this[k] = values[k]; + constructor (values = {}) { + 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) { 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; - sub[k] = newValues[k]; + Object.defineProperty(sub, k, descs[k]); } return sub;