mirror of
https://github.com/grafana/grafana.git
synced 2025-08-06 05:30:12 +08:00
Sandbox: Fix react class components stale state (#70572)
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import { ProxyTarget } from '@locker/near-membrane-shared';
|
||||
import { isNearMembraneProxy, ProxyTarget } from '@locker/near-membrane-shared';
|
||||
|
||||
import { forbiddenElements } from './constants';
|
||||
import { isReactClassComponent } from './utils';
|
||||
|
||||
// IMPORTANT: NEVER export this symbol from a public (e.g `@grafana/*`) package
|
||||
const SANDBOX_LIVE_VALUE = Symbol.for('@@SANDBOX_LIVE_VALUE');
|
||||
@ -72,6 +73,31 @@ export function markDomElementStyleAsALiveTarget(el: Element) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Some specific near membrane proxies interfere with plugins
|
||||
* an example of this is React class components state and their fast life cycles
|
||||
* with cached objects.
|
||||
*
|
||||
* This function marks an object as a live target inside the sandbox
|
||||
* but not all objects, only the ones that are allowed to be modified
|
||||
*/
|
||||
export function patchObjectAsLiveTarget(obj: unknown) {
|
||||
if (
|
||||
obj &&
|
||||
// do not define it twice
|
||||
!Object.hasOwn(obj, SANDBOX_LIVE_VALUE) &&
|
||||
// only for proxies
|
||||
isNearMembraneProxy(obj) &&
|
||||
// do not patch functions
|
||||
!(obj instanceof Function) &&
|
||||
// conditions for allowed objects
|
||||
// react class components
|
||||
isReactClassComponent(obj)
|
||||
) {
|
||||
Reflect.defineProperty(obj, SANDBOX_LIVE_VALUE, {});
|
||||
}
|
||||
}
|
||||
|
||||
export function isLiveTarget(el: ProxyTarget) {
|
||||
return Object.hasOwn(el, SANDBOX_LIVE_VALUE);
|
||||
}
|
||||
|
Reference in New Issue
Block a user