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.

Feature request - destroy/reinitialize atom.

See original GitHub issue

Hello, thanks for recoil, simple & powerful!

I have a use case where I open a document, split it into atom tree, then I can modify the nodes of the document. If I decide that the changes are bad, I can discard the changes & start again by reading the document into recoil.

The trouble is that when I reopen the document, the app displays the cached state from previous steps, because recoil has the old values under the atom keys. So to get ‘fresh atoms’ I can choose new keys, which is bad since the previous will still consume memory. Other thing I do now, is that I pass the opened document through selector, where I have access to the reset api, which I apply to my document tree after I turn it into atoms, thus I set the atom values to the default (initial) value.

Also not sure how to handle this case properly, because now I’m getting lot’s of warnings:

Duplicate atom key "node-c272f880-9518-4658-a33c-459a008889c9". This is a FATAL ERROR in
      production. But it is safe to ignore this warning if it occurred because of
      hot module replacement.

So maybe my approach is incorrect.

The error flow is:

// 1. initialize document into atoms 
const textAtom = atom({key: '1', default: {type: "TEXT", id: '1', value: "Hello world"});
// 2. update the node
const setText = useSetRecoilValue(textAtom);
setText("Bad text");
// 3. Reopen the document
// 4. Initialize the atoms (as step 1)
const textAtom = atom({key: '1', default: {type: "TEXT", id: '1', value: "Hello world"});
// 5. render app
const text = useRecoilValue(textAtom);
console.log(text) // "Bad text".

The workaround flow is:

// 5. reset atom in selector

export const persistedTextSelector = selector<Node>({
  key: "persistedTextSelector",
  get: ({get}) => get(persistedTextAtom),
  set: ({set, get, reset}, node) => {

     const textAtom = atom({key: node.id, default: node);
     set(persistedTextAtom, textAtom);
     reset(textAtom)
  }
})

// 6. render app
const text = useRecoilValue(persistedTextAtom);
console.log(text) // "Hello world".

What I would like to have is some flag on atom: atom({key: ‘1’, default: {type: “TEXT”, id: ‘1’, value: “Hello world”},reinitialize: true})

But that still won’t save me from getting the duplicate error warning. So I guess some Recoil.removeKeys([...ids]); api could work?

Thanks

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:12
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
drarmstrcommented, Feb 23, 2021

Symbols won’t work for keys as they would not persist across multiple executions and WeakMap isn’t sufficient for this use case. Something like FinalizationRegistry would be really convenient. Unfortunately, it doesn’t have enough browser support yet to use. In the meantime we’ve added garbage collection functionality for this. It’s currently behind a feature flag, but should be exposed in an upcoming release.

5reactions
ifokeevcommented, Sep 10, 2021

need this feature

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to, so to speak, restart or close browser after every ...
Set it to true . FYI, Here is the initial feature request: Feature Request: to have an option to restart new browser session...
Read more >
Backbone.js
Destroys the model on the server by delegating an HTTP DELETE request to Backbone.sync. Returns a jqXHR object, or false if the model...
Read more >
Question to fix atom/swap function - LAMMPS General ...
Recently, I tried to use fix atom/swap function in lammps to do monte carlo simulation. The issue I have is at some timestep,...
Read more >
virsh - Libvirt
net-destroy ... To list domains with this feature disabled use --no-autostart. ... If --pivot is specified, this requests that an active copy or...
Read more >
Table of Contents - Micronaut Documentation
HTTP Features · WebSocket Ping API · HTTP2 Server Push · JsonView on request bodies · WebSocket ws/wss protocol support · SSL handshake...
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