lxsmnsyc / solid-labels

Simple, reactive labels for SolidJS
MIT License
243 stars 8 forks source link

Custom name, and inject code for comment macro? #6

Closed voladelta closed 2 years ago

voladelta commented 2 years ago

Hi Alexis,

First of all, thank you for the package. It makes my dev life with Solid easier.

So here the request: πŸ˜…

The reason: It took me 2 days on and off to figure out why a Select didn't set initial memo value. Turned out i was using createRenderEffect instead of createEffect.

So i'd like to have

/* @effect name */
/* @computed name */

then ability to set custom createEffect like

babel: {
  plugins: [solidLabels({ createEffect: customCreateEffect })],
},

or inject code into effect comment if possible, like

/* @effect name @customCreateEffect */
/* @computed name @customCreateComputed */

The reason: i want to track the executation flow of effect/computation, to know if the my designed flow is correct. So it won't took me 2 days to debug any more. so i wrote little helper.

//@ts-ignore
window.setStartFlow = (name: string) => {
  scope = name;
  flow = [];
};

//@ts-ignore
window.getFlow = () => {
  console.log({ flow });
};

export function customCreateEffect(fn: Function, name: string = "") {
  createEffect(() => {
    if (name && scope) {
      if (name === scope) flow = [name]; // reset the flow
      else flow.push(name); // push the new effect into the stack
    }

    fn();
  });
}

I know you might be busy. So if you point me to the direction, i can learn to implement them.

Thank you for taking your time and reading this. πŸ‘πŸ€Ÿ

lxsmnsyc commented 2 years ago

Hmmm, I could certainly provide named blocks for comment pragma, what I have problems with would be adding support for custom reactive utilities. I could probably add support for auto-block:

/* @block createCustomEffect */ {
  // ...
}

// becomes
createCustomEffect(() => {
 // ...
});

The reason I can only provide this much was because it's a bit of a challenge on where to put things after the pragma (in this case, name).

lxsmnsyc commented 2 years ago

Named effects have now been released in v0.8.0

For third-party labels/pragma, I'll think about the benefits of them being added, meanwhile, I usually encourage 3P code to remain 3P. They can always opt-in to the sugar (that is, for memos and signals).