Skip to content

Commit

Permalink
fix(proxy-observation): prevent proxies from being wrapped in proxies…
Browse files Browse the repository at this point in the history
… again (#1716)

If you pass in a property that's part of a computed observation
structure into another class, the value is already wrapped in a proxy
before that property is observed in the new class. If that new class
then also uses computed observation, that property will be wrapped in a
proxy again, causing a "proxy within a proxy". This can repeat itself
endlessly, causing all sorts of issues.

Simply adding the proxy itself as the key to the `proxyMap` will solve
that problem at least as far as the issue in our app is concerned.
  • Loading branch information
fkleuver committed Apr 3, 2023
1 parent cf75f38 commit 7792e9c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/__tests__/2-runtime/proxy-observable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ describe('2-runtime/proxy-observable.spec.ts', function () {
});
}

it('does not rewrap a wrapped object', function () {
const proxied = ProxyObservable.wrap({});
assert.strictEqual(ProxyObservable.wrap(proxied), proxied);
});

it('does not rewrap a wrapped array', function () {
const proxied = ProxyObservable.wrap([]);
assert.strictEqual(ProxyObservable.wrap(proxied), proxied);
});

it('does not rewrap a wrapped map', function () {
const proxied = ProxyObservable.wrap(new Map());
assert.strictEqual(ProxyObservable.wrap(proxied), proxied);
});

it('does not rewrap a wrapped set', function () {
const proxied = ProxyObservable.wrap(new Set());
assert.strictEqual(ProxyObservable.wrap(proxied), proxied);
});

it('does not wrap object that has been marked as "nowrap"', function () {
@nowrap
class MyModel { }
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/observation/proxy-observation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function createProxy<T extends object>(obj: T): T {

const proxiedObj = new Proxy(obj, handler);
proxyMap.set(obj, proxiedObj);
proxyMap.set(proxiedObj, proxiedObj);

return proxiedObj as T;
}
Expand Down

0 comments on commit 7792e9c

Please sign in to comment.