News for hackers. Stuff that matters.
Re:A proposal to add signals to JavaScript by nikitaga on Sunday March 31, 2024 @09:55PM (#39890148)
Why does it need to be a part of the language? This could be a library. There are such libraries. They are small, so including them in your code is no big deal. Adding this to the language should not even be a goal.

Thinking that the current crop of JS UI libraries designed their signals in such a good way that it needs to become a part of the language is hubris. Signals have many possible implementations with different tradeoffs, and none of them deserve to have a special place in the JavaScript spec.

Before these libraries used signals, they or their predecessors used virtual DOM. Luckily, that didn't become part of JS, but how are signals any different? They aren't. The argument for making them standard is even worse than for virtual DOM.

Are we just going pile every fad into a runtime that basically has no way to dispose of no-longer-wanted features without breaking the web? That is quite short sighted.

Re:A proposal to add signals to JavaScript by klabb3 on Monday April 01, 2024 @12:28AM (#39890918)
Good points. We don’t want the wrong thing. But we want the right one!

Reactive UI won. The main thing stopping me from using vanilla JS is the absolute explosion in complexity managing state for even small sized applications. To me, any reactive framework is better than vanilla, so perhaps there is a construct missing? Now that it’s been a decade or so, we should start thinking about possible cut-points for standardization. Like with promises, this could bring down complexity for extremely common use-cases, if done right.

I think a better way to evaluate it would be: “would this proposal be used by existing reactive frameworks?”. If not, why? What’s missing? What’s superfluous? What about lessons from UI annd reactivity from other languages? There’s a lot of fragmented experience to distill, but it’s a worthwhile endeavor imo.

Re:A proposal to add signals to JavaScript by nikitaga on Monday April 01, 2024 @02:02AM (#39891286)
If you want to do "the right thing" – implement your proposal as a library, AND convince people to use it on its own technical merits. Then when everyone uses it (because it's so obviously "the right thing"), you can start asking if anyone wants your library to be built into the language.

But that's not what you're doing. You're gunning for becoming the standard from the start – you are trying to convince people to use your draft implementation based on its status as a proposed standard, instead of them using it on its own technical merits.

Ditch the status, and see if anyone still wants it.

--

Yes, reactive UI won – just fine, without having signals in the JS standard. Because not having signals in the language was never an actual problem holding back reactive UI development in JS.

Your proposal does not "bring down complexity". It simply moves the complexity from UI libraries into the JS standard. In doing that, you forcefully marry the ecosystem to that particular style of complexity. Every browser vendor will need to implement it and support it... for how many decades?

And to what end? Unlike e.g. promises that are useful on their own, your proposal isn't nearly ergonomic enough to allow building reactive UIs in vanilla JS. Users will still need to use libraries for that, just like they do today. You're just moving one piece of such libraries into the standard, without building the case for why it's needed there.

--

Your proposal spends pages selling the readers on signals, but that is not what you need to sell. We already have many implementations of signals. You need to sell why your (or any) signals implementation needs to be in the JavaScript standard.

You have one tiny "Benefits of a standard library" subsection under "Secondary benefits" but it's just ridiculous. You're basically saying that we should add signals to JS because we've added (much simpler or more needed) things to JS before – is that really your best argument?

And... "saving bundle size"? You want to bless one implementation of a complex problem to save what, 5KB of this: https://cdn.jsdelivr.net/npm/s-js

Sorry, just – nothing about this makes sense to me.

Re:A proposal to add signals to JavaScript by klabb3 on Monday April 01, 2024 @02:32AM (#39891399)
I don’t have a dog in the fight. I also don't see yet any mainstream support behind the proposal. So I don’t get why it has to be so heated?

The main benefit is interop. Same with promises. You can implement all of promises with custom callbacks - in fact it’s trivial. But competing implementations don’t typically land on API compatibility simply because they’re solving the same problem. That causes a fractured ecosystem. Maybe interop could be important with signals? I think they should argue that, if so!

> Users will still need to use libraries for that, just like they do today.

Yes? But you reduce the lifting by the libs - ideally enabling a class of vanilla use-cases which can be made demonstrably improved. You could say querySelector was unnecessary because you can do it in lib. Or filter, or map. Standardization can cover std-lib like features too no?

Doesn’t mean I am in favor. I think you should always default to no unless strong and consistent proven benefits. But why not have good faith arguments for what problems this will or won’t solve. For instance, if hypothetically let’s say react or svelte has a different model that cannot possibly use these signals, then that’s probably a sign it’s not good. My philosophy with proposals is balancing the curiosity and honest inquiry with a grumpy defensive inquisition before saying aye. Flaming though is really not helpful.

> You're basically saying that we should add signals to JS because we've added (much simpler or more needed) things to JS before

> saving bundle size

Yes, I agree these are weak arguments.

Re:A proposal to add signals to JavaScript by jonathanlydall on Monday April 01, 2024 @02:31AM (#39891396)
I think you’re right, I don’t see how building this into the base library makes some things possible which weren’t before.

I think that the Promise API was not the actual thing people directly wanted (and on its own had no compelling reason to be added to the base library), but a standardised Promise API was needed in order to add the hugely useful async/await keywords which are unachievable without changes to the language.

I am however a big fan of a really good base library (it’s one of the things I love about working with .NET), but they should be focussing on functionality with the broadest reach (as in most encountered by average JS devs working on day to day tasks), e.g. things like better tools for working with dates and times.

Re:A proposal to add signals to JavaScript by klabb3 on Monday April 01, 2024 @06:15AM (#39892498)
> I think that the Promise API was not the actual thing people directly wanted (and on its own had no compelling reason to be added to the base library), but a standardised Promise API was needed in order to add the hugely useful async/await keywords which are unachievable without changes to the language.

In JS, the async syntax is largely syntactic sugar around chaining promises with `.then()`. Promises alone fixes callback hell and brings structure and scoping to multi-stage async operations - at least to the extent possible in such a dynamic environment as JS. There are only a couple minor additions relating to the main entry points, microtasks and interactions with the runtime, to make the main async/await experience we enjoy today. This is actually a good thing, when you can build abstractions on top of other core constructs, without affecting the rest of the language too much.

There may absolutely be analogous syntactic ergonomic constructs on top of signals, reactivity or observables - whichever is sensible - that could be layered on top to compose really powerful user-facing features in the future. So the success of promises and async is a still a supporting story, in my view. A lot more research and scrutiny is needed, but it’s certainly one of the top interesting ideas.

Re:A proposal to add signals to JavaScript by troupo on Monday April 01, 2024 @11:55AM (#39895545)
> implement your proposal as a library, AND convince people to use it on its own technical merits.

That is almost literally exactly what happened: most major JS frameworks (except React) converged on Signals.

> Because not having signals in the language was never an actual problem holding back reactive UI development in JS.

Oh, but it did hold back reactive development. There are many limitations on current implementations of signals precisely because there's no proper support for many things in the language.

> You need to sell why your (or any) signals implementation needs to be in the JavaScript standard.

That is why it is:

- a proposal that

- calls for input from implementers, users, library developers etc.

> Unlike e.g. promises that are useful on their own,

But the exact same thing happened with promises: everyone had their implementation, there was no need for a proposal to add that specific API to the standard library. Deferred (the precursor to Promises) existed for several years before Promises. Here's the full history: https://samsaccone.com/posts/history-of-promises.html

And yet, 15 years later here we are

Re:A proposal to add signals to JavaScript by imbnwa on Sunday March 31, 2024 @10:00PM (#39890175)
Remember the Observable proposal?
Re:A proposal to add signals to JavaScript by tengbretson on Sunday March 31, 2024 @10:06PM (#39890210)
The Observable proposal made a stronger case in my opinion, since Observables provide an interface that is functionally unique and useful for the boundaries between app logic and libraries, so a single standard approach has benefits. Signals on the other hand live right where application state binds to the ui. Is this really somewhere that people are patching together a hodge podge of libraries that need to use a consistent api? I'm not so sure.
Re:A proposal to add signals to JavaScript by Offler on Monday April 01, 2024 @07:38AM (#39893036)
Signals are used in every framework except React. Observables weren't. That's a huge difference.
Re:A proposal to add signals to JavaScript by Sammi on Monday April 01, 2024 @06:47PM (#39900303)
There's the old one that fizzled out: https://github.com/tc39/proposal-observable

And there's the new one which seems to be getting implemented in node right now: https://github.com/WICG/observable

Re:A proposal to add signals to JavaScript by Too on Monday April 01, 2024 @02:20AM (#39891351)
One good argument for standardizing Signals is that debugging them seems to be a nightmare. Imagine a deep tree of calculated signals firing off each other and you need to find the source of what started the chain reaction. Standardizing will allow devtools to develop around it.
Re:A proposal to add signals to JavaScript by wruza on Monday April 01, 2024 @04:02AM (#39891776)
They don't fire off each other, they simply depend on each other like functions do:

  a = () => 42
  b = () => a() - 1
  c = () => a() + b() * 2
It isn't a bigger nightmare than debugging pure functions. The source for `c` is `a` and `b`. All signal values (as proposed) will be lexically available in a body of a dependent signal, so there's no hidden registry to navigate anyway. If in-browser IDEs want to record a call tree for an activation record, they can do that without a standard.
Re:A proposal to add signals to JavaScript by Too on Monday April 01, 2024 @12:45PM (#39896122)
There is still a watch() mechanism that from a consumers point of view hide the originating event of an update. Otherwise if all you wanted was functions, just use functions.

When watch fires out of control, you need debugging tools to understand why your render() function is being invoked more often than it should.

This type of problem happens all the time in react and you need to trace upwards to find that 7 components up the chain someone accidentally included new Date() in the state that propagates down through props and re-renders everything.

Re:A proposal to add signals to JavaScript by wruza on Monday April 01, 2024 @05:55PM (#39899858)
Signals are basically lazy functions. You can't "just" use functions if performance is a concern, cause that's the least efficient way to keep everything consistent.

Since watching seems(?) to be synchronous in the proposal, why do you think extra debugging tools are needed? You can breakpoint into render() with a regular debugger and look at the stack trace.

Re:A proposal to add signals to JavaScript by kabes on Monday April 01, 2024 @04:27AM (#39891889)
You can say this about most things in the standard library. But as stated in the motivation, there's an ongoing trend to extend the rather small standard library js offers, so you don't need to have a package for each and every common task.

You can argue about the need for this, but if we're going to extend the standard lib, then looking at what is popular is a good approach IMO.

> Before these libraries used signals, they or their predecessors used virtual DOM

Signals are not a replacement for virtual DOM.