From 36bd4eed0da87ec82e5eab42dae428063b2aba5f Mon Sep 17 00:00:00 2001 From: brokun Date: Fri, 2 Feb 2024 11:09:03 +0800 Subject: [PATCH] fix(observable): readonly and non-configurable property should return the actual value --- .changeset/spotty-boxes-applaud.md | 12 ++++++++ packages/mana-observable/src/tracker.spec.ts | 32 ++++++++++++++++++++ packages/mana-observable/src/tracker.ts | 4 +++ 3 files changed, 48 insertions(+) create mode 100644 .changeset/spotty-boxes-applaud.md diff --git a/.changeset/spotty-boxes-applaud.md b/.changeset/spotty-boxes-applaud.md new file mode 100644 index 0000000..b58c813 --- /dev/null +++ b/.changeset/spotty-boxes-applaud.md @@ -0,0 +1,12 @@ +--- +'@difizen/mana-observable': patch +'@difizen/mana-syringe': patch +'@difizen/mana-common': patch +'@difizen/mana-react': patch +'@difizen/mana-core': patch +'@difizen/mana-l10n': patch +'@difizen/mana-app': patch +'@difizen/mana-docs': patch +--- + +Readonly & non-configurable property should return the actual value diff --git a/packages/mana-observable/src/tracker.spec.ts b/packages/mana-observable/src/tracker.spec.ts index 40b18cc..c06130f 100644 --- a/packages/mana-observable/src/tracker.spec.ts +++ b/packages/mana-observable/src/tracker.spec.ts @@ -596,4 +596,36 @@ describe('Tracker', () => { assert(!!tryInvokeGetter(proxyBaz.bar, proxyBaz, baz)); }); + + it('#get readonly & non-configurable property', () => { + class Foo {} + + class Bar { + name = 'Bar'; + } + const foo = new Foo(); + const bar = new Bar(); + + Object.defineProperty(foo, 'prop', { + value: bar, + writable: false, + configurable: false, + }); + + try { + let changeCount = 0; + const reaction = () => { + const trackable = Tracker.track(foo, reaction); + if ('prop' in trackable) { + const b = trackable['prop'] as Bar; + b.name; + } + changeCount += 1; + }; + reaction(); // 1 + assert(changeCount === 1); + } catch (e) { + assert(!e); + } + }); }); diff --git a/packages/mana-observable/src/tracker.ts b/packages/mana-observable/src/tracker.ts index 044fbc5..5625d03 100644 --- a/packages/mana-observable/src/tracker.ts +++ b/packages/mana-observable/src/tracker.ts @@ -153,6 +153,10 @@ export namespace Tracker { return track(value, act, true); } const descriptor = getPropertyDescriptor(target, property); + if (descriptor?.configurable === false && descriptor?.writable === false) { + // non-configurable and non-writable property should return the actual value + return target[property]; + } if (descriptor?.get) { return tryInvokeGetter(descriptor.get, proxy, target); }