@grafana/scenes 4.24.3--canary.734.9287358327.0 → 4.24.3--canary.734.9288916719.0

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -21,6 +21,8 @@
21
21
  - URL sync.
22
22
  - ... and more.
23
23
 
24
+ For library documentation go to [https://grafana.com/developers/scenes](https://grafana.com/developers/scenes).
25
+
24
26
  ## Development
25
27
 
26
28
  To work on @grafana/scenes SDK, please follow the guides below.
@@ -5,7 +5,7 @@ class UniqueUrlKeyMapper {
5
5
  getUniqueKey(key, obj) {
6
6
  const objectsWithKey = this.index.get(key);
7
7
  if (!objectsWithKey) {
8
- return key;
8
+ throw new Error("Cannot find any scene object that uses the key '" + key + "'");
9
9
  }
10
10
  const address = objectsWithKey.findIndex((o) => o.sceneObject === obj);
11
11
  if (address > 0) {
@@ -1 +1 @@
1
- {"version":3,"file":"UniqueUrlKeyMapper.js","sources":["../../../src/services/UniqueUrlKeyMapper.ts"],"sourcesContent":["import { SceneObject } from '../core/types';\n\nexport interface SceneObjectWithDepth {\n sceneObject: SceneObject;\n depth: number;\n}\n\nexport class UniqueUrlKeyMapper {\n private index = new Map<string, SceneObjectWithDepth[]>();\n\n public getUniqueKey(key: string, obj: SceneObject) {\n const objectsWithKey = this.index.get(key);\n if (!objectsWithKey) {\n // throw new Error(\"Cannot find any scene object that uses the key '\" + key + \"'\");\n return key;\n }\n\n const address = objectsWithKey.findIndex((o) => o.sceneObject === obj);\n if (address > 0) {\n return `${key}-${address + 1}`;\n }\n\n return key;\n }\n\n public rebuildIndex(root: SceneObject) {\n this.index.clear();\n this.buildIndex(root, 0);\n }\n\n private buildIndex(sceneObject: SceneObject, depth: number) {\n if (sceneObject.urlSync) {\n for (const key of sceneObject.urlSync.getKeys()) {\n const hit = this.index.get(key);\n if (hit) {\n hit.push({ sceneObject, depth });\n hit.sort((a, b) => a.depth - b.depth);\n } else {\n this.index.set(key, [{ sceneObject, depth }]);\n }\n }\n }\n\n sceneObject.forEachChild((child) => this.buildIndex(child, depth + 1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;AAAA,SAC/B,MAAA;AACL,UAAK,IAAA,CAAA,KAAA,CAAM,IAAI,GAAK,EAAA,CAAC,EAAE,WAAa,EAAA,KAAA,EAAO,CAAC,CAAA,CAAA;AAAA,SAC9C;AAAA,OACF;AAAA,KACF;AAEA,IAAY,WAAA,CAAA,YAAA,CAAa,CAAC,KAAU,KAAA,IAAA,CAAK,WAAW,KAAO,EAAA,KAAA,GAAQ,CAAC,CAAC,CAAA,CAAA;AAAA,GACvE;AACF;;;;"}
1
+ {"version":3,"file":"UniqueUrlKeyMapper.js","sources":["../../../src/services/UniqueUrlKeyMapper.ts"],"sourcesContent":["import { SceneObject } from '../core/types';\n\nexport interface SceneObjectWithDepth {\n sceneObject: SceneObject;\n depth: number;\n}\n\nexport class UniqueUrlKeyMapper {\n private index = new Map<string, SceneObjectWithDepth[]>();\n\n public getUniqueKey(key: string, obj: SceneObject) {\n const objectsWithKey = this.index.get(key);\n if (!objectsWithKey) {\n throw new Error(\"Cannot find any scene object that uses the key '\" + key + \"'\");\n }\n\n const address = objectsWithKey.findIndex((o) => o.sceneObject === obj);\n if (address > 0) {\n return `${key}-${address + 1}`;\n }\n\n return key;\n }\n\n public rebuildIndex(root: SceneObject) {\n this.index.clear();\n this.buildIndex(root, 0);\n }\n\n private buildIndex(sceneObject: SceneObject, depth: number) {\n if (sceneObject.urlSync) {\n for (const key of sceneObject.urlSync.getKeys()) {\n const hit = this.index.get(key);\n if (hit) {\n hit.push({ sceneObject, depth });\n hit.sort((a, b) => a.depth - b.depth);\n } else {\n this.index.set(key, [{ sceneObject, depth }]);\n }\n }\n }\n\n sceneObject.forEachChild((child) => this.buildIndex(child, depth + 1));\n }\n}\n"],"names":[],"mappings":"AAOO,MAAM,kBAAmB,CAAA;AAAA,EAAzB,WAAA,GAAA;AACL,IAAQ,IAAA,CAAA,KAAA,uBAAY,GAAoC,EAAA,CAAA;AAAA,GAAA;AAAA,EAEjD,YAAA,CAAa,KAAa,GAAkB,EAAA;AACjD,IAAA,M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AA,KACF;AAEA,IAAY,WAAA,CAAA,YAAA,CAAa,CAAC,KAAU,KAAA,IAAA,CAAK,WAAW,KAAO,EAAA,KAAA,GAAQ,CAAC,CAAC,CAAA,CAAA;AAAA,GACvE;AACF;;;;"}
@@ -86,12 +86,6 @@ class UrlSyncManager {
86
86
  this._urlKeyMapper.rebuildIndex(this._sceneRoot);
87
87
  syncStateFromUrl(sceneObj, urlParams, this._urlKeyMapper);
88
88
  }
89
- syncNewObj(obj) {
90
- if (!this._urlParams) {
91
- throw new Error("UrlSyncManager not initialized");
92
- }
93
- syncStateFromUrl(obj, this._urlParams, this._urlKeyMapper);
94
- }
95
89
  getUrlState(root) {
96
90
  return getUrlState(root);
97
91
  }
@@ -1 +1 @@
1
- {"version":3,"file":"UrlSyncManager.js","sources":["../../../src/services/UrlSyncManager.ts"],"sourcesContent":["import { Location, UnregisterCallback } from 'history';\n\nimport { locationService } from '@grafana/runtime';\n\nimport { SceneObjectStateChangedEvent } from '../core/events';\nimport { SceneObject, SceneObjectUrlValues } from '../core/types';\nimport { writeSceneLog } from '../utils/writeSceneLog';\nimport { Unsubscribable } from 'rxjs';\nimport { UniqueUrlKeyMapper } from './UniqueUrlKeyMapper';\nimport { getUrlState, isUrlValueEqual, syncStateFromUrl } from './utils';\n\nexport interface UrlSyncManagerLike {\n initSync(root: SceneObject): void;\n cleanUp(root: SceneObject): void;\n getUrlState(root: SceneObject): SceneObjectUrlValues;\n syncNewObj(obj: SceneObject): void;\n}\n\nexport class UrlSyncManager implements UrlSyncManagerLike {\n private _urlKeyMapper = new UniqueUrlKeyMapper();\n private _sceneRoot!: SceneObject;\n private _stateSub: Unsubscribable | null = null;\n private _locationSub?: UnregisterCallback | null = null;\n private _lastPath?: string;\n private _ignoreNextLocationUpdate = false;\n private _urlParams: URLSearchParams | undefined;\n\n /**\n * Updates the current scene state to match URL state.\n */\n public initSync(root: SceneObject) {\n if (!this._locationSub) {\n writeSceneLog('UrlSyncManager', 'New location listen');\n this._locationSub = locationService.getHistory().listen(this._onLocationUpdate);\n }\n\n if (this._stateSub) {\n writeSceneLog('UrlSyncManager', 'Unregister previous scene state subscription', this._sceneRoot.state.key);\n this._stateSub.unsubscribe();\n }\n\n const location = locationService.getLocation();\n\n this._sceneRoot = root;\n this._lastPath = location.pathname;\n this._urlParams = new URLSearchParams(location.search);\n this._stateSub = root.subscribeToEvent(SceneObjectStateChangedEvent, this._onStateChanged);\n\n this.syncFrom(this._sceneRoot);\n }\n\n public cleanUp(root: SceneObject) {\n // Ignore this if we have a new or different root\n if (this._sceneRoot !== root) {\n return;\n }\n\n writeSceneLog('UrlSyncManager', 'Clean up');\n\n if (this._locationSub) {\n this._locationSub();\n writeSceneLog('UrlSyncManager', 'Unregister history listen');\n this._locationSub = null;\n }\n\n if (this._stateSub) {\n this._stateSub.unsubscribe();\n this._stateSub = null;\n writeSceneLog(\n 'UrlSyncManager',\n 'Root deactived, unsub to state',\n 'same key',\n this._sceneRoot.state.key === root.state.key\n );\n }\n }\n\n public syncFrom(sceneObj: SceneObject) {\n const urlParams = locationService.getSearch();\n // The index is always from the root\n this._urlKeyMapper.rebuildIndex(this._sceneRoot);\n syncStateFromUrl(sceneObj, urlParams, this._urlKeyMapper);\n }\n\n private _onLocationUpdate = (location: Location) => {\n this._urlParams = new URLSearchParams(location.search);\n\n if (this._ignoreNextLocationUpdate) {\n this._ignoreNextLocationUpdate = false;\n return;\n }\n\n if (this._lastPath !== location.pathname) {\n return;\n }\n\n // Rebuild key mapper index before starting sync\n this._urlKeyMapper.rebuildIndex(this._sceneRoot);\n // Sync scene state tree from url\n syncStateFromUrl(this._sceneRoot, this._urlParams, this._urlKeyMapper);\n this._lastPath = location.pathname;\n };\n\n private _onStateChanged = ({ payload }: SceneObjectStateChangedEvent) => {\n const changedObject = payload.changedObject;\n\n if (changedObject.urlSync) {\n const newUrlState = changedObject.urlSync.getUrlState();\n\n const searchParams = locationService.getSearch();\n const mappedUpdated: SceneObjectUrlValues = {};\n\n this._urlKeyMapper.rebuildIndex(this._sceneRoot);\n\n for (const [key, newUrlValue] of Object.entries(newUrlState)) {\n const uniqueKey = this._urlKeyMapper.getUniqueKey(key, changedObject);\n const currentUrlValue = searchParams.getAll(uniqueKey);\n\n if (!isUrlValueEqual(currentUrlValue, newUrlValue)) {\n mappedUpdated[uniqueKey] = newUrlValue;\n }\n }\n\n if (Object.keys(mappedUpdated).length > 0) {\n this._ignoreNextLocationUpdate = true;\n locationService.partial(mappedUpdated, true);\n }\n }\n };\n\n public syncNewObj(obj: SceneObject) {\n if (!this._urlParams) {\n throw new Error('UrlSyncManager not initialized');\n }\n\n syncStateFromUrl(obj, this._urlParams, this._urlKeyMapper);\n }\n\n public getUrlState(root: SceneObject): SceneObjectUrlValues {\n return getUrlState(root);\n }\n}\n\nlet urlSyncManager: UrlSyncManagerLike | undefined;\n\nexport function getUrlSyncManager(): UrlSyncManagerLike {\n if (!urlSyncManager) {\n urlSyncManager = new UrlSyncManager();\n }\n\n return urlSyncManager;\n}\n"],"names":[],"mappings":";;;;;;AAkBO,MAAM,cAA6C,CAAA;AAAA,EAAnD,WAAA,GAAA;AACL,IAAQ,IAAA,CAAA,aAAA,GAAgB,IAAI,kBAAmB,EAAA,CAAA;AAE/C,IAAA,IAAA,CAAQ,SAAmC,GAAA,IAAA,CAAA;AAC3C,IAAA,IAAA,CAAQ,YAA2C,GAAA,IAAA,CAAA;AAEnD,IAAA,IAAA,CAAQ,yBAA4B,GAAA,KAAA,CAAA;AA4DpC,IAAQ,IAAA,CAAA,iBAAA,GAAoB,CAAC,QAAuB,KAAA;AAClD,MAAA,IAAA,CAAK,UAAa,GAAA,IAAI,eAAgB,CAAA,QAAA,CAAS,MAAM,CAAA,CAAA;AAErD,MAAA,IAAI,KAAK,yBAA2B,EAAA;AAClC,QAAA,IAAA,CAAK,yBAA4B,GAAA,KAAA,CAAA;AACjC,QAAA,OAAA;AAAA,OACF;AAEA,MAAI,IAAA,IAAA,CAAK,SAAc,KAAA,QAAA,CAAS,Q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
+ {"version":3,"file":"UrlSyncManager.js","sources":["../../../src/services/UrlSyncManager.ts"],"sourcesContent":["import { Location, UnregisterCallback } from 'history';\n\nimport { locationService } from '@grafana/runtime';\n\nimport { SceneObjectStateChangedEvent } from '../core/events';\nimport { SceneObject, SceneObjectUrlValues } from '../core/types';\nimport { writeSceneLog } from '../utils/writeSceneLog';\nimport { Unsubscribable } from 'rxjs';\nimport { UniqueUrlKeyMapper } from './UniqueUrlKeyMapper';\nimport { getUrlState, isUrlValueEqual, syncStateFromUrl } from './utils';\n\nexport interface UrlSyncManagerLike {\n initSync(root: SceneObject): void;\n cleanUp(root: SceneObject): void;\n getUrlState(root: SceneObject): SceneObjectUrlValues;\n}\n\nexport class UrlSyncManager implements UrlSyncManagerLike {\n private _urlKeyMapper = new UniqueUrlKeyMapper();\n private _sceneRoot!: SceneObject;\n private _stateSub: Unsubscribable | null = null;\n private _locationSub?: UnregisterCallback | null = null;\n private _lastPath?: string;\n private _ignoreNextLocationUpdate = false;\n private _urlParams: URLSearchParams | undefined;\n\n /**\n * Updates the current scene state to match URL state.\n */\n public initSync(root: SceneObject) {\n if (!this._locationSub) {\n writeSceneLog('UrlSyncManager', 'New location listen');\n this._locationSub = locationService.getHistory().listen(this._onLocationUpdate);\n }\n\n if (this._stateSub) {\n writeSceneLog('UrlSyncManager', 'Unregister previous scene state subscription', this._sceneRoot.state.key);\n this._stateSub.unsubscribe();\n }\n\n const location = locationService.getLocation();\n\n this._sceneRoot = root;\n this._lastPath = location.pathname;\n this._urlParams = new URLSearchParams(location.search);\n this._stateSub = root.subscribeToEvent(SceneObjectStateChangedEvent, this._onStateChanged);\n\n this.syncFrom(this._sceneRoot);\n }\n\n public cleanUp(root: SceneObject) {\n // Ignore this if we have a new or different root\n if (this._sceneRoot !== root) {\n return;\n }\n\n writeSceneLog('UrlSyncManager', 'Clean up');\n\n if (this._locationSub) {\n this._locationSub();\n writeSceneLog('UrlSyncManager', 'Unregister history listen');\n this._locationSub = null;\n }\n\n if (this._stateSub) {\n this._stateSub.unsubscribe();\n this._stateSub = null;\n writeSceneLog(\n 'UrlSyncManager',\n 'Root deactived, unsub to state',\n 'same key',\n this._sceneRoot.state.key === root.state.key\n );\n }\n }\n\n public syncFrom(sceneObj: SceneObject) {\n const urlParams = locationService.getSearch();\n // The index is always from the root\n this._urlKeyMapper.rebuildIndex(this._sceneRoot);\n syncStateFromUrl(sceneObj, urlParams, this._urlKeyMapper);\n }\n\n private _onLocationUpdate = (location: Location) => {\n this._urlParams = new URLSearchParams(location.search);\n\n if (this._ignoreNextLocationUpdate) {\n this._ignoreNextLocationUpdate = false;\n return;\n }\n\n if (this._lastPath !== location.pathname) {\n return;\n }\n\n // Rebuild key mapper index before starting sync\n this._urlKeyMapper.rebuildIndex(this._sceneRoot);\n // Sync scene state tree from url\n syncStateFromUrl(this._sceneRoot, this._urlParams, this._urlKeyMapper);\n this._lastPath = location.pathname;\n };\n\n private _onStateChanged = ({ payload }: SceneObjectStateChangedEvent) => {\n const changedObject = payload.changedObject;\n\n if (changedObject.urlSync) {\n const newUrlState = changedObject.urlSync.getUrlState();\n\n const searchParams = locationService.getSearch();\n const mappedUpdated: SceneObjectUrlValues = {};\n\n this._urlKeyMapper.rebuildIndex(this._sceneRoot);\n\n for (const [key, newUrlValue] of Object.entries(newUrlState)) {\n const uniqueKey = this._urlKeyMapper.getUniqueKey(key, changedObject);\n const currentUrlValue = searchParams.getAll(uniqueKey);\n\n if (!isUrlValueEqual(currentUrlValue, newUrlValue)) {\n mappedUpdated[uniqueKey] = newUrlValue;\n }\n }\n\n if (Object.keys(mappedUpdated).length > 0) {\n this._ignoreNextLocationUpdate = true;\n locationService.partial(mappedUpdated, true);\n }\n }\n };\n\n public getUrlState(root: SceneObject): SceneObjectUrlValues {\n return getUrlState(root);\n }\n}\n\nlet urlSyncManager: UrlSyncManagerLike | undefined;\n\nexport function getUrlSyncManager(): UrlSyncManagerLike {\n if (!urlSyncManager) {\n urlSyncManager = new UrlSyncManager();\n }\n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AAA;AAC/C,IAAiB,gBAAA,CAAA,QAAA,EAAU,SAAW,EAAA,IAAA,CAAK,aAAa,CAAA,CAAA;AAAA,GAC1D;AAAA,EAgDO,YAAY,IAAyC,EAAA;AAC1D,IAAA,OAAO,YAAY,IAAI,CAAA,CAAA;AAAA,GACzB;AACF,CAAA;AAEA,IAAI,cAAA,CAAA;AAEG,SAAS,iBAAwC,GAAA;AACtD,EAAA,IAAI,CAAC,cAAgB,EAAA;AACnB,IAAA,cAAA,GAAiB,IAAI,cAAe,EAAA,CAAA;AAAA,GACtC;AAEA,EAAO,OAAA,cAAA,CAAA;AACT;;;;"}
package/dist/index.d.ts CHANGED
@@ -1531,7 +1531,6 @@ interface UrlSyncManagerLike {
1531
1531
  initSync(root: SceneObject): void;
1532
1532
  cleanUp(root: SceneObject): void;
1533
1533
  getUrlState(root: SceneObject): SceneObjectUrlValues;
1534
- syncNewObj(obj: SceneObject): void;
1535
1534
  }
1536
1535
  declare class UrlSyncManager implements UrlSyncManagerLike {
1537
1536
  private _urlKeyMapper;
@@ -1549,7 +1548,6 @@ declare class UrlSyncManager implements UrlSyncManagerLike {
1549
1548
  syncFrom(sceneObj: SceneObject): void;
1550
1549
  private _onLocationUpdate;
1551
1550
  private _onStateChanged;
1552
- syncNewObj(obj: SceneObject): void;
1553
1551
  getUrlState(root: SceneObject): SceneObjectUrlValues;
1554
1552
  }
1555
1553
  declare function getUrlSyncManager(): UrlSyncManagerLike;
package/dist/index.js CHANGED
@@ -591,7 +591,7 @@ class UniqueUrlKeyMapper {
591
591
  getUniqueKey(key, obj) {
592
592
  const objectsWithKey = this.index.get(key);
593
593
  if (!objectsWithKey) {
594
- return key;
594
+ throw new Error("Cannot find any scene object that uses the key '" + key + "'");
595
595
  }
596
596
  const address = objectsWithKey.findIndex((o) => o.sceneObject === obj);
597
597
  if (address > 0) {
@@ -7831,12 +7831,6 @@ class UrlSyncManager {
7831
7831
  this._urlKeyMapper.rebuildIndex(this._sceneRoot);
7832
7832
  syncStateFromUrl(sceneObj, urlParams, this._urlKeyMapper);
7833
7833
  }
7834
- syncNewObj(obj) {
7835
- if (!this._urlParams) {
7836
- throw new Error("UrlSyncManager not initialized");
7837
- }
7838
- syncStateFromUrl(obj, this._urlParams, this._urlKeyMapper);
7839
- }
7840
7834
  getUrlState(root) {
7841
7835
  return getUrlState(root);
7842
7836
  }