News for hackers. Stuff that matters.
Re:A proposal to add signals to JavaScript by _the_inflator on Sunday March 31, 2024 @03:18PM (#39887183)
Historically, this example is the reason why the Web evolved into jQuery and from there forked into the world of Angular and React mainly.

Event handling is getting messy very easily. If you want to get deeper into it, have a look at event bubbling and propagation.

Large applications need a robust event handling. This is the nowadays hidden benefit of frameworks like Angular, Vue etc.

Believe me, you don’t want to use the standard event handling API without a framework. Adding, deleting, cloning, firing, removing, fire once etc on many elements can have serious unwanted side effects.

Re:A proposal to add signals to JavaScript by beezlewax on Sunday March 31, 2024 @03:49PM (#39887444)
What kind of side effects specifically?
Re:A proposal to add signals to JavaScript by doomroot on Sunday March 31, 2024 @03:54PM (#39887490)
I believe memory leaks to start
Re:A proposal to add signals to JavaScript by falcor84 on Sunday March 31, 2024 @04:15PM (#39887639)
Are you implying that there are memory leaks in browsers' internal implementation of events? Because my take is that the problem is with "user space" scripts not cleaning up after themselves, and I don't see how that would get better by adding yet another API to be mindful of.
Re:A proposal to add signals to JavaScript by gibbitz on Sunday March 31, 2024 @05:59PM (#39888446)
I believe this would be more related to something like memoizing a DOM structure in a "Live" listener that is later removed from the DOM but not garbage collected due to the reference in the event listener. As the poster mentioned, developer error -- not a fundamental language or browser implementation flaw.
Re:A proposal to add signals to JavaScript by naasking on Tuesday April 02, 2024 @12:24AM (#39902371)
If a language or browser implementation can't reclaim this unused memory thus creating a memory leak, that arguably is a flaw. It's literally a DoS attack vector that can be exploited by the untrusted scripts that run in the browser sandbox.
Re:A proposal to add signals to JavaScript by Klonoar on Sunday March 31, 2024 @05:42PM (#39888306)
This is one of those points where you need to link to some kind of data or conclusive result actually showcasing this, because you're arguing against a pattern that's been in browser ecosystems for decades. I've done this for larger applications and haven't experienced issues.

Being charitable, the best I can imagine right now that'd cause memory leaks is someone running into the old school JS scoping issues and capturing something in handlers that they shouldn't. That's not the handler itself that's the problem, though - that's the developer.

(Yes, we could rant on and on about the poor design decisions that JS has built in, but that's been beaten to death)

Re:A proposal to add signals to JavaScript by unemployable on Sunday March 31, 2024 @06:50PM (#39888906)
Memory leaks can occur when a component adds events to an element outside of the component (such as the window) and then gets removed from the DOM without removing the event handler from the window. This is solved in native Web Components by the mount/unmount methods where you can run code to remove event listeners when the component has been unmounted.

For other event listeners, they get removed when the DOM element is removed.

The frameworks do not solve this to any greater degree. They also just make everything invisible and behind-the-scenes and hard to debug due to their declarative nature, but that is another topic.

Re:A proposal to add signals to JavaScript by pcthrowaway on Monday April 01, 2024 @01:07AM (#39891090)
Removing event listeners upon some component being removed actually sounds like a great use case for the new FinalizationRegistry API[1]

[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re:A proposal to add signals to JavaScript by ngc6677 on Monday April 01, 2024 @02:29AM (#39891387)
Example with web components in all frameworks https://webcomponents.dev/blog/all-the-ways-to-make-a-web-co... and an other version to experiment with https://jsbin.com/yiviragiba/8/edit?html,css,js,console,outp...
Re:A proposal to add signals to JavaScript by andrewstuart on Sunday March 31, 2024 @03:53PM (#39887481)
I use events extensively in large applications and its never been a problem. In fact they solve complexity.
Re:A proposal to add signals to JavaScript by unemployable on Sunday March 31, 2024 @06:45PM (#39888838)
>If you want to get deeper into it, have a look at event bubbling and propagation.

In this example, the event is on the Window. There is no bubbling. It is already at the top level.

>Believe me, you don’t want to use the standard event handling API without a framework. Adding, deleting, cloning, firing, removing, fire once etc on many elements can have serious unwanted side effects.

I don't know what this means. The frameworks do not have much to do with this topic.

Re:A proposal to add signals to JavaScript by pquki4 on Sunday March 31, 2024 @07:15PM (#39889110)
I think the previous comment discusses events in a general sense.

Frameworks batch changes so that updates are efficient, and in many cases figures out the "correct" order of doing things. If you do all of those yourself in a large and complex UI, very likely you are updating the DOM less efficiently than what frameworks are doing, and very likely you introduced some subtle bugs. Speaking of that from my first-hand experience.