question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: New effect & update Signature

See original GitHub issue

Request for comments for new update signature inside effect.

@RunDevelopment, @dani-mp, @RichieAHB, @icyJoseph, and anyone interested.

Problem Statement

One issue we currently have within effects is that even though send and update are different functions, they’re frequently used together (update the context, then trigger a transition). E.g.:

effect(send, update) {
  const fetchData = async () => {
    let response: Response;
    try {
      data = await someApi();
      update(context => ({ ...context, data: data }));
      send('SUCCESS');
    } catch (error) {
      update(context => ({ ...context, error: error.message }));
      send('FAILURE');
    }
  };
  fetchData();
}

Proposal (Updated)

The proposal is to pass a new assign parameter to effect. It’s a function that takes an Object as argument with the type:

{
    update?: (context: Context) => Context;
    event?: EventString | EventObject<EventString>;
}

The above fetch example would look like this:

effect(assign) {
  const fetchData = async () => {
    let response: Response;
    try {
      data = await someApi();
      assign({update: context => ({ ...context, data: data }), event: 'SUCCESS'})
    } catch (error) {
      assign({update: context => ({ ...context, error: error.message }), event: 'FAILURE'})
    }
  };
  fetchData();
}

Proposal (Old)

The proposal is to keep passing both send and update (as well as event, in the future) to effect, BUT with one change: update will also return send, so you can couple updating the context and sending event on a single line:

update(updaterFn)('SUCCESS');

Granted, it might look a little weird at first, but it has a few advantages:

  • You can still just send or just update (keep a simple API and backward compatible)
  • It relates to how the state machine is configured (curried function, context first, state machine second) - can aid learning/remembering the API because it’s mnemonic.

Alternative Considered

We could remove the update method from effect and overload send to also update the context:

send(event: string)
send(event: string, updater: fn)
send(updater:fn)

I see a few disadvantages here:

  • Send would have many different overloaded signatures, which could be confusing.
  • Since we don’t want to allow free mutations of the context outside an effect, the send method inside effect would be different from the send returned when calling useStateMachine. We might need to come up with different nomenclature or incur the risk of making the API confusing.

Thoughts? Ideas? Comments?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
icyJosephcommented, Jun 2, 2021

I could try to take on the partial updates some time later on, if not done by then.

Well, it is worth exploring all alternatives, but at the end of the day, specially named keys might just be the solution.

1reaction
cassiozencommented, May 29, 2021

Updated the proposal

Read more comments on GitHub >

github_iconTop Results From Across the Web

RFC 8463 - A New Cryptographic Signature Method for ...
A New Cryptographic Signature Method for DomainKeys Identified Mail (DKIM) (RFC 8463, September 2018.
Read more >
RFC 8358: Update to Digital Signatures on Internet-Draft ...
This document updates the handling of digital signatures on Internet-Drafts that contain non-ASCII characters in a text file. This document updates RFC 5485...
Read more >
Overview of Request For Change (RFC) Review Process
The manager who approves the Request for Change (RFC) is ultimately responsible for assessing the impact of the Request for Change (RFC) before...
Read more >
XML Signature Syntax and Processing Version 1.1 - W3C
Note: On 23 April 2013, the reference to the "Additional XML Security URIs" RFC was updated. The Director previously authorized the ...
Read more >
Request for Change (RFC) - Dell
Unless specified otherwise in the Reason for Change section, this RFC shall take effect on the latest signature date. DELL CONFIDENTIAL.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found