Live data from Hacker News

A proposal to add signals to JavaScript

github.com

321–330 of 336 posts

Re: A proposal to add signals to JavaScript

#321
post #59

The lack of signals isn’t remotely the largest issue with JS, and adding them has minimal impact for most users of JavaScript. The biggest issue is the lack of a standard library, resulting in npm hell in most projects.

What do you think is missing from the "standard library" as of ES2024?

That's a complaint I've heard since ES3 if not earlier and the trend ever since ES2015 has been to address that so I'm genuinely wondering what you think the JS "standard library" (aka built-ins) is lacking right now.

Given the different environments JS runs in these days, I'm also okay with extending the definition of "standard library" to "things browsers should have as built-ins" or "things Node.js should offer as a built-in module".

Re: A proposal to add signals to JavaScript

#323
post #244

Earlier quoted context omitted.

This not really correct, the useReducer hook is not some external state which is smuggled in. It is an actual input for the reconciler which defines an output of this component, so it is perfectly functional even by your definition.

The value counter changes in subsequent invocations. Maybe react redefined some standard terminology, but this is really straightforward. If useReducer is not pure, and it's not, any function that calls it is impure.

This reducer is certainly pure, you are confusing it with `st => st++`

Re: A proposal to add signals to JavaScript

#324

Earlier quoted context omitted.

To do parallel tasks in a loop for the situation I outlined (getting a final array of images from URIs), it would be really cumbersome, and your implementation would be pretty much equivalent to the inner workings of Promise.all. As others have mentioned; if you aren’t using Promise.all, you are likely missing a good deal of opportunities for easier and more performant async code.

For non-rejecting promises, `await Promise.all(promises)` is basically performance equivalent to `for (const p of promises) await p;`. In case you think the second one does serial work, here's a codepen for you. https://codepen.io/tomtheisen/pen/QWPOmjp The "inner workings" could be a for loop, with the exception of rejected promises. The only opportunity for quicker resolution is when one of the promises reject, at…

You are right. I was mistakenly thinking that the alternative (naive approach) would be to write the promise-creating tasks within the for loop, but if they are not part of the loop, the functionality is basically equivalent to Promise.all.

  // A -- Async and parallel
  const results = await Promise.all(data.map((i) => delay(i)));

  // B -- Functionally equivalent to the above
  const promises = data.map((i) => delay(i));
  const result = [];
  for (let a of promises) {
    const r = await a;
    result.push(r);
  }

  // C -- Problematic and naive approach: much slower
  const result = [];
  for (let i of data) {
    const r = await delay(i);
    result.push(r);
  }

Re: A proposal to add signals to JavaScript

#325
post #323

Earlier quoted context omitted.

The value counter changes in subsequent invocations. Maybe react redefined some standard terminology, but this is really straightforward. If useReducer is not pure, and it's not, any function that calls it is impure.

This reducer is certainly pure, you are confusing it with `st => st++`

Tell me, what's the value `counter` returned by this "pure" function invocation?

const [counter, tick] = useReducer(st => st + 1, 0)

Answer: It depends. Sometimes 0, sometimes more.

Any function that can return different results for the same inputs is impure. Therefore, `useReducer` is impure. It can return different results for the same input. React may have some alternate definition for "function" or "pure", but these words existed prior to react.

Re: A proposal to add signals to JavaScript

#326

Earlier quoted context omitted.

The rationale for it is the fact that multiple frameworks provide their own versions of this mechanism. The proposal is to relocate extremely popular and common functionality from framework space to the language/runtime space. The popularity of React is itself the rationale for the utility of this idea, and any terse version of the rationale is for show. Is that a good enough rationale? Maybe, maybe not, but you are…

Most importantly: OP is right re: vanilla example is most legible. Reading the proposal, I have no idea what this "Signal" word adds other than complexity. Less important: I really, really, really, really, am reluctant to consider that is something that needs standardizing. Disclaimer: I don't have 100% context if this concept is _really_ the same across all these frameworks. But frankly, I doubt it, if it was that s…

>Usually when it actually is fundamental you can smell it outside of JS as well. (ex. promises futures).

Excellent criterion

Re: A proposal to add signals to JavaScript

#327
post #323

Earlier quoted context omitted.

This reducer is certainly pure, you are confusing it with `st => st++`

Tell me, what's the value `counter` returned by this "pure" function invocation? const [counter, tick] = useReducer(st => st + 1, 0) Answer: It depends. Sometimes 0, sometimes more. Any function that can return different results for the same inputs is impure. Therefore, `useReducer` is impure. It can return different results for the same input. React may have some alternate definition for "function" or "pure", but th…

Given function is pure, for any given input state, it will return state + 1, the state is an accumulated result of all invocations, there are no arguments nor external factors that would cause it to behave differently.

There are no side-effects either, it solely relies on it's input state to produce the output, it doesn't mutate any variables outside it's scope, doesn't interact with the outside world, nor modify any global state.

Therefore it is perfectly pure by the classic definition of pure, react did not redefine anything in that matter, it is a pure functional renderer by design.

Re: A proposal to add signals to JavaScript

#328
post #327

Earlier quoted context omitted.

Tell me, what's the value `counter` returned by this "pure" function invocation? const [counter, tick] = useReducer(st => st + 1, 0) Answer: It depends. Sometimes 0, sometimes more. Any function that can return different results for the same inputs is impure. Therefore, `useReducer` is impure. It can return different results for the same input. React may have some alternate definition for "function" or "pure", but th…

Given function is pure, for any given input state, it will return state + 1, the state is an accumulated result of all invocations, there are no arguments nor external factors that would cause it to behave differently. There are no side-effects either, it solely relies on it's input state to produce the output, it doesn't mutate any variables outside it's scope, doesn't interact with the outside world, nor modify any…

Crucially, the state you're referring to exists outside the function parameters.

Invoking the returned reducer function mutates the `memoizedState` property on the component's fiber object. The fiber object is the state external to the function. It's not one of arguments to the function.

Here's a function. Is it pure?

    let current = 0;
    function getNext() { return current++; }
Note that the result is an accumulated result of all invocations. I don't think this is a pure function. Now imagine `current` was a react fiber object, accessed through a hook dispatcher.

What exactly is the difference?

Re: A proposal to add signals to JavaScript

#329
post #327

Earlier quoted context omitted.

Given function is pure, for any given input state, it will return state + 1, the state is an accumulated result of all invocations, there are no arguments nor external factors that would cause it to behave differently. There are no side-effects either, it solely relies on it's input state to produce the output, it doesn't mutate any variables outside it's scope, doesn't interact with the outside world, nor modify any…

Crucially, the state you're referring to exists outside the function parameters. Invoking the returned reducer function mutates the `memoizedState` property on the component's fiber object. The fiber object is the state external to the function. It's not one of arguments to the function. Here's a function. Is it pure? let current = 0; function getNext() { return current++; } Note that the result is an accumulated res…

It all seems right, except the reducer function does not mutate the fiber object.

Your function is certainly not pure, it boldly mutates the outside state via increment operator. It is incorrect to extrapolate this on how fiber works. The difference is that React hook, with both state and setter lives inside the dispatcher, and calling the hook setter only enqueues an update which is then handled by dispatcher, so technically speaking the hook does not mutate it's outer state directly, the dispatcher updates it's inner state while processing the queue.

Re: A proposal to add signals to JavaScript

#330
post #329

Earlier quoted context omitted.

Crucially, the state you're referring to exists outside the function parameters. Invoking the returned reducer function mutates the `memoizedState` property on the component's fiber object. The fiber object is the state external to the function. It's not one of arguments to the function. Here's a function. Is it pure? let current = 0; function getNext() { return current++; } Note that the result is an accumulated res…

It all seems right, except the reducer function does not mutate the fiber object. Your function is certainly not pure, it boldly mutates the outside state via increment operator. It is incorrect to extrapolate this on how fiber works. The difference is that React hook, with both state and setter lives inside the dispatcher, and calling the hook setter only enqueues an update which is then handled by dispatcher, so te…

Enqueuing an update is mutating an update queue.

None of these semantic games affect the fact that the value of `count` does not depend only on the parameters to the function.

Post reply on HN