1

I tried to call an unsubscribe function:

unsubscribeFromThingIds(arg.tids);

after the await cacheEntryRemoved; line in this example.

The function is defined like this:

export const unsubscribeFromThingIds = (tids: string[]) => {
  followedThingIds = followedThingIds.filter(
    id => !tids.includes(id)
  );
  const argument = { tids: followedThingIds } as ITimersChannelInitParams;
  myIo!.emit("configure timers channel", argument);
};

(where myIo is the Socket.IO client)

This is not working currently for me. I am currently debugging this issue.

I also tried to use an useEffect hook with a returned cleanup function for running it when the component unmounts, something like this:

useEffect(() => {
  return () => {
    unsubscribeFromThingIds(getTimersQueryParams.tids);
  };
}, [getTimersQueryParams]);

I want to ask for this information because I am not sure which one of the two ideas should I work on more, or if there is another thing I can do that is better.

silviubogan
  • 3,343
  • 3
  • 31
  • 57

1 Answers1

2

Could it be that you are just impatient? The cache entry will be removed 60 seconds (the keepUnusedDataFor option) after the last component stops using it.

Generally: using the endpoint lifecycle and awaiting cacheEntryRemoved is the right way to do this if you started the listener within onCacheEntryAdded. If you want it to happen earlier, just use a shorter keepUnusedDataFor duration.

phry
  • 35,762
  • 5
  • 67
  • 81
  • I solved the issue as you said, by setting `keepUnusedDataFor` to `0`, since it is about highly dynamic data that should not get cached. Thank you! – silviubogan Aug 09 '21 at 10:04