[FIX] hooks: useSubEnv supports arbitrary descriptors in env

Before this commit, when defining a getter in the env passed to useSubEnv,
the value was read, losing the definition of the property.

After this commit, declaring a getter in the env works as expected:
the property stays a getter.
This commit is contained in:
Lucas Perais (lpe)
2021-12-02 13:28:53 +01:00
committed by Aaron Bohy
parent b66d5231d3
commit 463eb4bb86
3 changed files with 63 additions and 1 deletions
+3 -1
View File
@@ -38,7 +38,9 @@ export function useEnv<E extends Env>(): E {
*/
export function useSubEnv(envExtension: Env) {
const node = getCurrent()!;
node.childEnv = Object.freeze(Object.assign({}, node.childEnv, envExtension));
const env = Object.create(node.childEnv);
const descrs = Object.getOwnPropertyDescriptors(envExtension);
node.childEnv = Object.freeze(Object.defineProperties(env, descrs));
}
// -----------------------------------------------------------------------------