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] atom effects

See original GitHub issue

Continuing from discussions in the discord channel. Recoil proposes Atom Effects. We don’t need to provide the same API, and probably it’s impossible as there are some conceptual differences between jotai and recoil. (It is interesting to me that things get to be seen differently in jotai from recoil, because originally jotai is modeled after recoil.)

However, there are good use cases. It seems like there are three possibilities for each use case.

  1. can be implemented as derived atoms
  2. can’t be implemented as derived atoms, but something can be added in jotai core
  3. can never be implemented in jotai

Collecting use cases might be important. Please share your ideas.

For 1, we could create new functions in jotai/utils: withEffect and atomWithEffect.

Any thoughts? volunteers?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:23 (17 by maintainers)

github_iconTop GitHub Comments

5reactions
dai-shicommented, Jan 24, 2021

Hm, okay debugLabel is already a camel case.

Here’s version 3.

import { atom } from 'jotai'

const dataAtom = atom(null)
dataAtom.onMount = (get, set) => {
  const unsubscribe = someStore.subscribe((nextData) => {
    set(dataAtom, nextData)
  })
  return unsubscribe
}
3reactions
dai-shicommented, Jan 18, 2021

While I would try to avoid adding a new feature in core, this seems unavoidable.

  • We discussed some patterns in #234, but useEffect based solution only works for non-base atoms
  • We started working on #248, and support for subscription for base atoms seems crucial

So, here’s the proposal.

import { atom } from 'jotai'

const dataAtom = atom(null)
dataAtom.effects = [
  (get, set) => {
    const unsubscribe = someStore.subscribe((nextData) => {
      set(dataAtom, nextData)
    })
    return unsubscribe
  }
]

// effects are invoked in commit phase, when it first have a dependent
// and will be cleaned up when there are no dependents (even if it's a very short period.)
// `get` can read only from the committed state, wip state is just ignored.
Read more comments on GitHub >

github_iconTop Results From Across the Web

The Atom Publishing Protocol RFC 5023 - IETF Datatracker
The Atom format is documented in the Atom Syndication Format. Gregorio & de hOra Standards Track [Page 1] RFC 5023 The Atom Publishing...
Read more >
Atom Effects | Recoil
Atom effects are an API for managing side-effects and synchronizing or initializing Recoil atoms. They have a variety of useful applications such as...
Read more >
RFC822: Standard for ARPA Internet Text Messages
RFC # 822 Obsoletes: RFC #733 (NIC #41952). RFC822: Standard for ARPA Internet Text Messages. Revised by David H. Crocker. Dept. of Electrical...
Read more >
ATOM Program System | Encyclopedia MDPI
The ATOM computer system is designed to study the structure of atoms and the physical processes occurring with their participation. atom RPAE ...
Read more >
RFC 6721 - The Atom "deleted-entry" Element - faqs.org
This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (http://trustee.ietf.org/license-info) in effect on the ...
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