@grafana/scenes 5.20.4 → 5.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # v5.21.0 (Tue Oct 29 2024)
2
+
3
+ #### 🚀 Enhancement
4
+
5
+ - SceneObject: Improve cloning logic [#944](https://github.com/grafana/scenes/pull/944) ([@torkelo](https://github.com/torkelo))
6
+
7
+ #### Authors: 1
8
+
9
+ - Torkel Ödegaard ([@torkelo](https://github.com/torkelo))
10
+
11
+ ---
12
+
1
13
  # v5.20.4 (Wed Oct 23 2024)
2
14
 
3
15
  #### 🐛 Bug Fix
@@ -23,13 +23,15 @@ function cloneSceneObject(sceneObject, withState) {
23
23
  }
24
24
  function cloneSceneObjectState(sceneState, withState) {
25
25
  const clonedState = __spreadValues({}, sceneState);
26
+ Object.assign(clonedState, withState);
26
27
  for (const key in clonedState) {
27
28
  const propValue = clonedState[key];
28
29
  if (propValue instanceof SceneObjectBase) {
29
30
  clonedState[key] = propValue.clone();
30
31
  }
31
32
  if (propValue instanceof SceneObjectRef) {
32
- throw new Error("Cannot clone a SceneObject with a SceneObjectRef in state");
33
+ console.warn("Cloning object with SceneObjectRef");
34
+ continue;
33
35
  }
34
36
  if (Array.isArray(propValue)) {
35
37
  const newArray = [];
@@ -43,7 +45,6 @@ function cloneSceneObjectState(sceneState, withState) {
43
45
  clonedState[key] = newArray;
44
46
  }
45
47
  }
46
- Object.assign(clonedState, withState);
47
48
  return clonedState;
48
49
  }
49
50
  function getClosest(sceneObject, extract) {
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sources":["../../../../src/core/sceneGraph/utils.ts"],"sourcesContent":["import { SceneObject, SceneObjectState } from '../types';\n\nimport { SceneObjectBase } from '../SceneObjectBase';\nimport { SceneObjectRef } from '../SceneObjectRef';\n\n/**\n * Will create new SceneItem with shalled cloned state, but all states items of type SceneObject are deep cloned\n */\nexport function cloneSceneObject<T extends SceneObjectBase<TState>, TState extends SceneObjectState>(\n sceneObject: SceneObjectBase<TState>,\n withState?: Partial<TState>\n): T {\n const clonedState = cloneSceneObjectState(sceneObject.state, withState);\n return new (sceneObject.constructor as any)(clonedState);\n}\n\nexport function cloneSceneObjectState<TState extends SceneObjectState>(\n sceneState: TState,\n withState?: Partial<TState>\n): TState {\n const clonedState = { ...sceneState };\n\n // Clone any SceneItems in state\n for (const key in clonedState) {\n const propValue = clonedState[key];\n if (propValue instanceof SceneObjectBase) {\n clonedState[key] = propValue.clone();\n }\n\n if (propValue instanceof SceneObjectRef) {\n throw new Error('Cannot clone a SceneObject with a SceneObjectRef in state');\n }\n\n // Clone scene objects in arrays\n if (Array.isArray(propValue)) {\n const newArray: any = [];\n for (const child of propValue) {\n if (child instanceof SceneObjectBase) {\n newArray.push(child.clone());\n } else {\n newArray.push(child);\n }\n }\n clonedState[key] = newArray;\n }\n }\n\n Object.assign(clonedState, withState);\n\n return clonedState;\n}\n\n/** Walks up the scene graph, returning the first non-undefined result of `extract` */\nexport function getClosest<T>(sceneObject: SceneObject, extract: (s: SceneObject) => T | undefined): T | undefined {\n let curSceneObject: SceneObject | undefined = sceneObject;\n let extracted: T | undefined = undefined;\n\n while (curSceneObject && !extracted) {\n extracted = extract(curSceneObject);\n curSceneObject = curSceneObject.parent;\n }\n\n return extracted;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAQgB,SAAA,gBAAA,CACd,aACA,SACG,EAAA;AACH,EAAA,MAAM,WAAc,GAAA,qBAAA,CAAsB,WAAY,CAAA,KAAA,EAAO,SAAS,CAAA,CAAA;AACtE,EAAO,OAAA,IAAK,WAAY,CAAA,WAAA,CAAoB,WAAW,CAAA,CAAA;AACzD,CAAA;AAEgB,SAAA,qBAAA,CACd,YACA,SACQ,EAAA;AACR,EAAA,MAAM,cAAc,cAAK,CAAA,EAAA,EAAA,UAAA,CAAA,CAAA;AAGzB,EAAA,KAAA,MAAW,OAAO,WAA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State } from '../types';\n\nimport { SceneObjectBase } from '../SceneObjectBase';\nimport { SceneObjectRef } from '../SceneObjectRef';\n\n/**\n * Will create new SceneItem with shalled cloned state, but all states items of type SceneObject are deep cloned\n */\nexport function cloneSceneObject<T extends SceneObjectBase<TState>, TState extends SceneObjectState>(\n sceneObject: SceneObjectBase<TState>,\n withState?: Partial<TState>\n): T {\n const clonedState = cloneSceneObjectState(sceneObject.state, withState);\n return new (sceneObject.constructor as any)(clonedState);\n}\n\nexport function cloneSceneObjectState<TState extends SceneObjectState>(\n sceneState: TState,\n withState?: Partial<TState>\n): TState {\n const clonedState = { ...sceneState };\n\n Object.assign(clonedState, withState);\n\n // Clone any SceneItems in state\n for (const key in clonedState) {\n const propValue = clonedState[key];\n if (propValue instanceof SceneObjectBase) {\n clonedState[key] = propValue.clone();\n }\n\n if (propValue instanceof SceneObjectRef) {\n console.warn('Cloning object with SceneObjectRef');\n continue;\n }\n\n // Clone scene objects in arrays\n if (Array.isArray(propValue)) {\n const newArray: any = [];\n for (const child of propValue) {\n if (child instanceof SceneObjectBase) {\n newArray.push(child.clone());\n } else {\n newArray.push(child);\n }\n }\n clonedState[key] = newArray;\n }\n }\n\n return clonedState;\n}\n\n/** Walks up the scene graph, returning the first non-undefined result of `extract` */\nexport function getClosest<T>(sceneObject: SceneObject, extract: (s: SceneObject) => T | undefined): T | undefined {\n let curSceneObject: SceneObject | undefined = sceneObject;\n let extracted: T | undefined = undefined;\n\n while (curSceneObject && !extracted) {\n extracted = extract(curSceneObject);\n curSceneObject = curSceneObject.parent;\n }\n\n return extracted;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAQgB,SAAA,gBAAA,CACd,aACA,SACG,EAAA;AACH,EAAA,MAAM,WAAc,GAAA,qBAAA,CAAsB,WAAY,CAAA,KAAA,EAAO,SAAS,CAAA,CAAA;AACtE,EAAO,OAAA,IAAK,WAAY,CAAA,WAAA,CAAoB,WAAW,CAAA,CAAA;AACzD,CAAA;AAEgB,S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A,MAAA,CAAA;AAAA,GAClC;AAEA,EAAO,OAAA,SAAA,CAAA;AACT;;;;"}
package/dist/index.js CHANGED
@@ -511,13 +511,15 @@ function cloneSceneObject(sceneObject, withState) {
511
511
  }
512
512
  function cloneSceneObjectState(sceneState, withState) {
513
513
  const clonedState = __spreadValues$K({}, sceneState);
514
+ Object.assign(clonedState, withState);
514
515
  for (const key in clonedState) {
515
516
  const propValue = clonedState[key];
516
517
  if (propValue instanceof SceneObjectBase) {
517
518
  clonedState[key] = propValue.clone();
518
519
  }
519
520
  if (propValue instanceof SceneObjectRef) {
520
- throw new Error("Cannot clone a SceneObject with a SceneObjectRef in state");
521
+ console.warn("Cloning object with SceneObjectRef");
522
+ continue;
521
523
  }
522
524
  if (Array.isArray(propValue)) {
523
525
  const newArray = [];
@@ -531,7 +533,6 @@ function cloneSceneObjectState(sceneState, withState) {
531
533
  clonedState[key] = newArray;
532
534
  }
533
535
  }
534
- Object.assign(clonedState, withState);
535
536
  return clonedState;
536
537
  }
537
538
  function getClosest(sceneObject, extract) {