react-sweet-state icon indicating copy to clipboard operation
react-sweet-state copied to clipboard

Ability to do cleanup on store destroy

Open anacierdem opened this issue 5 years ago • 0 comments

Currently, thecreateContainer provides an ability to do some cleanup on a per-container basis. It is also possible to create a single store instance shared among a number of containers and know when each of them is destroyed.

const Container = createContainer(Store, {
  onCleanup: () => {
    // This is run for every container that gets destroyed.
  }
});

Then when rendering we can do;

<Container scope="my-scope"> ... </..

We also know that by design RSS destroys the store when there are no more containers left in a given scope (for this case my-scope). Currently there is not an integrated way of knowing when the store is destroyed. This can become necessary for example to abort all ongoing requests.

As a solution consider;

const Container = createContainer(Store, {
  onDestroy: () => {
    // This is run when the store is destroyed.
  }
});

Or there may be alternatives where we can 'deduce' when to do the final cleanup, for example providing number of containers left as a prop to onCleanup etc.

anacierdem avatar Sep 14 '20 08:09 anacierdem