node-lru-cache icon indicating copy to clipboard operation
node-lru-cache copied to clipboard

Is the signal passed to the fetchMethod always triggering the abort event?

Open petertflem opened this issue 1 year ago • 1 comments

Hello! 👋

I did some testing related to implementing this cache in our project, and it seemed that the abort event was always triggered, even though everything else worked as expected. The data was fetched and cached, no problems. But when I added an event handler, it always logged. Something like this (this an excerpt of the code):

this.cache = new LRUCache<string, T, Context<T>>({
    max: 300,
    ttl,
    fetchMethod: async (key: string, staleValue: T | undefined, { signal, context }): Promise<T | undefined> => {
        signal.addEventListener('abort', () => {
            console.log('ABORTED');
        });

        signal.onabort = (e) => {
            console.log('aborted?', e);
        };

        // Fetches and returns data
    }
});

Could it be related to allowStale being true?

If this is not expected behaviour, then I'll see if I can get a repro up.

petertflem avatar Sep 20 '23 12:09 petertflem

The fetch will be aborted if it has not yet resolved, and the data is deleted or overwritten in the cache.

If it's firing all the time, even when that's not the case, then yes, I'd call that a bug. Repro test much appreciated!

isaacs avatar Sep 20 '23 19:09 isaacs

Can't figure out what the issue is from the info provided here, and seems to work for me. Will re-open if reproduction case is provided.

isaacs avatar Jun 27 '24 22:06 isaacs