mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 16:45:43 -06:00
* Component that can cache and extract variable dependencies * Component that can cache and extract variable dependencies * Updates * Refactoring * Lots of refactoring and iterations of supporting both re-rendering and query re-execution * Updated SceneCanvasText * Updated name of file * Updated * Refactoring a bit * Added back getName * Added comment * minor fix * Minor fix * Merge fixes * Merge fixes * Some review fixes * Updated comment * Added forceRender function * Add back fail on console log
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { SceneObjectBase } from './SceneObjectBase';
|
|
import { SceneObjectState, SceneObjectStatePlain } from './types';
|
|
|
|
/**
|
|
* Will call callback for all first level child scene objects and scene objects inside arrays
|
|
*/
|
|
export function forEachSceneObjectInState(state: SceneObjectStatePlain, callback: (scene: SceneObjectBase) => void) {
|
|
for (const propValue of Object.values(state)) {
|
|
if (propValue instanceof SceneObjectBase) {
|
|
callback(propValue);
|
|
}
|
|
|
|
if (Array.isArray(propValue)) {
|
|
for (const child of propValue) {
|
|
if (child instanceof SceneObjectBase) {
|
|
callback(child);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Will create new SceneItem with shalled cloned state, but all states items of type SceneObject are deep cloned
|
|
*/
|
|
export function cloneSceneObject<T extends SceneObjectBase<TState>, TState extends SceneObjectState>(
|
|
sceneObject: SceneObjectBase<TState>,
|
|
withState?: Partial<TState>
|
|
): T {
|
|
const clonedState = { ...sceneObject.state };
|
|
|
|
// Clone any SceneItems in state
|
|
for (const key in clonedState) {
|
|
const propValue = clonedState[key];
|
|
if (propValue instanceof SceneObjectBase) {
|
|
clonedState[key] = propValue.clone();
|
|
}
|
|
|
|
// Clone scene objects in arrays
|
|
if (Array.isArray(propValue)) {
|
|
const newArray: any = [];
|
|
for (const child of propValue) {
|
|
if (child instanceof SceneObjectBase) {
|
|
newArray.push(child.clone());
|
|
} else {
|
|
newArray.push(child);
|
|
}
|
|
}
|
|
clonedState[key] = newArray;
|
|
}
|
|
}
|
|
|
|
Object.assign(clonedState, withState);
|
|
|
|
return new (sceneObject.constructor as any)(clonedState);
|
|
}
|