Skip to content

Commit

Permalink
fix(observable): readonly and non-configurable property should return…
Browse files Browse the repository at this point in the history
… the actual value
  • Loading branch information
BroKun authored and sunshinesmilelk committed Feb 2, 2024
1 parent 5c2f9f3 commit 36bd4ee
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .changeset/spotty-boxes-applaud.md
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions packages/mana-observable/src/tracker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
});
4 changes: 4 additions & 0 deletions packages/mana-observable/src/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 36bd4ee

Please sign in to comment.