Kitware/vtk-js

[Feature] Unfreezing New Instances

Closed this issue · 8 comments

Feature Request

Currently, the publicAPI gets frozen in the macro using Object.freeze.

function newInstance(
  mixins,
  initialValues,
  publicAPI = {},
  model = {},
  skipWidgetState = false
) {
  if (!skipWidgetState) {
    vtkWidgetState.extend(publicAPI, model, initialValues);
  }

  for (let i = 0; i < mixins.length; i++) {
    const mixin = MIXINS[mixins[i]];
    if (mixin) {
      mixin.extend(publicAPI, model, initialValues);
    } else {
      vtkErrorMacro('Invalid mixin name:', mixins[i]);
    }
  }
  macro.safeArrays(model);

  return Object.freeze(publicAPI);
}

It is often desirable to assign metadata to an instance, such as IDs or references. However, this is currently not possible because the instance is frozen.

Motivation and Detailed Description

Would you be willing to remove this constraint? Alternatively, could you suggest a way for me to achieve this without creating a myImageData?

You can add those directly on the model by calling myVtkInstance.set("myId", anything) and myVtkInstance.get("myId").myId

Another option would be to add some kind of mixin into the newInstance arg...

Thanks a lot @jourdain

@jourdain

Your suggested approach doesn't seem to work at the moment.

I've created a CodeSandbox to demonstrate this issue.

https://codesandbox.io/p/sandbox/blending-my897d

what i have done is

imageData.set("stuff", [1, 2, 3]);
console.log("stuff", imageData.get("stuff"));

and here is the log

CleanShot 2024-08-06 at 12 06 47@2x

also typescript didn't liked it either

CleanShot 2024-08-06 at 11 58 31@2x

Sorry I guess my API memory was a bit rusty...

imageData.set({ dataType, id });
const userData = imageData.get("id", "dataType");
console.log(userData.id, userData.dataType)

Worked great thanks!

@jourdain I attempted to override a method in imageData, which resulted in an error. I understand this isn't common practice, but in my case, I need to display a warning when using a method.

I tried this:

imageData.getScalars = () => {
console.warn('');
}

Do you have any suggestions on how I can work around this issue?

Inheritance is the only way, but you can easily inline that.