tc39 / proposal-signals

A proposal to add signals to JavaScript.
MIT License
2.87k stars 54 forks source link

Rename `Signal.get` and `Signal.set` to `Signal.read` and `Signal.write` #182

Open titoBouzout opened 1 month ago

titoBouzout commented 1 month ago

That's it. This may be subjective, but I believe It makes it easier to reason about things when you refer to readingand writingthan getting and setting.

I could probably make the point stronger by copy pasting snippets from this very same repo that includes the wording read and write:

  • Ability to read Signals without triggering dependencies to be recorded (untrack)

  • A Signal type which represents state, i.e. **writable**Signal. This is a value that others can **read**.

…calculated again by default when one of their dependencies changes, but rather only run if someone actually reads them.

  • Solution: Disallow reading and writing any Signal from within a synchronous reaction callback

    Note: This proposal does allow signals to be both read and written from computed and effect signals, without restrict…

    // No signals may be read or written during the notify.

    // Our computation somehow led to a cyclic read of itself.

// - The code for the callbacks (for reading signals and running watches)

// A read-write Signal .. export class State {

export function setThrowInvalid**Write**ToSignalError(fn: () => never): void {

consumerAllowSignal**Writes**: false,

  • Whether writes to signals are allowed when this consumer is the activeConsumer.

    it("allows writes during computed", () => {

throwInvalidWriteToSignalError();

Link to "reads" of this repo https://github.com/search?q=repo%3Atc39%2Fproposal-signals+reads&type=issues Link to "writes" of this repo https://github.com/search?q=repo%3Atc39%2Fproposal-signals+writes&type=issues

jeff-hykin commented 3 weeks ago

IMO this is bikeshedding

titoBouzout commented 3 weeks ago

You think? To me the API surface is very important. Code reads more clearly with read/write than with get/set. Is not the typical get/set, both actions have side effects reading and writing communicates the difference properly. Also, you comment adds nothing of value. If you are not interested do something else.

jeff-hykin commented 3 weeks ago

Sorry, I did mean for it to be a throw-away comment. I agree the API surface area and names in general are not bikeshedding.

I personally, coincidentally, find get and set more clear, because read and write are usually associated with cold storage in my experience.

I don't think either get/set or read/write them indicate side effect. If anything pull/push might indicate side effects more and aligns with the terminology of the design.

I'm only one vote and these things are about feedback/consensus. My estimate is the average user isn't going to find push/pull or get/set or read/write notable. But they will find stuff like not working with Async notable.

It's good you brought up the topic, I've said my piece, now it's up to others to say theirs so we can have a complete picture.