grafana / scenes

Build Grafana dashboards directly in your Grafana app plugins.
https://grafana.com/developers/scenes
Apache License 2.0
133 stars 20 forks source link

SafeSerializingSceneObject: Fix issue with using a private field #873

Closed torkelo closed 1 month ago

torkelo commented 1 month ago

The use of private field here does not actually work when transpiled to a js that does not use native private fields.

since it transforms into this:

var __accessCheck = (obj, member, msg) => {
  if (!member.has(obj))
    throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
  __accessCheck(obj, member, "read from private field");
  return getter ? getter.call(obj) : member.get(obj);
};
var __privateAdd = (obj, member, value) => {
  if (member.has(obj))
    throw TypeError("Cannot add the same private member more than once");
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
var __privateSet = (obj, member, value, setter) => {
  __accessCheck(obj, member, "write to private field");
  setter ? setter.call(obj, value) : member.set(obj, value);
  return value;
};
var _value;
class SafeSerializableSceneObject {
  constructor(value) {
    __privateAdd(this, _value, void 0);
    this.text = "__sceneObject";
    __privateSet(this, _value, value);
  }
  toString() {
    return void 0;
  }
  valueOf() {
    return __privateGet(this, _value);
  }
  get value() {
    return this;
  }
}
_value = new WeakMap();

export { SafeSerializableSceneObject };
//# sourceMappingURL=SafeSerializableSceneObject.js.map

and the privateSet function call is not "preserved" when cloning / spreading leading to this error:

Screenshot 2024-08-17 at 09 41 30
📦 Published PR as canary version: 5.9.1--canary.873.10430729232.0
:sparkles: Test out this PR locally via: ```bash npm install @grafana/scenes-react@5.9.1--canary.873.10430729232.0 npm install @grafana/scenes@5.9.1--canary.873.10430729232.0 # or yarn add @grafana/scenes-react@5.9.1--canary.873.10430729232.0 yarn add @grafana/scenes@5.9.1--canary.873.10430729232.0 ```