mcollina / async-cache-dedupe

Async cache with dedupe support
MIT License
640 stars 40 forks source link

Browsers do not implement setImmediate #55

Closed dimfeld closed 1 year ago

dimfeld commented 1 year ago

The memory storage module uses setImmediate, which is not implemented by modern browsers. I see that the test suite adds some kind of shim to work around this but it's not otherwise mentioned anywhere.

I propose a solution that doesn't require externally providing setImmediate:

const { isServerSide } = require('../util')
const setImmediate = isServerSide ? globalThis.setImmediate : (fn, ...args) => setTimeout(fn, 0, ...args);

I've done this in a fork and it passes tests as well as working in my actual application: https://github.com/dimfeld/async-cache-dedupe/tree/fix-setimmediate-in-browser

The main problem is that the code coverage check fails because it's not exercising the browser-only part of that line. I'm not sure how to deal with that.

What do you think? Happy to submit a PR if this sounds good to you and there's some way to make the test coverage happy.

mcollina commented 1 year ago

Send a PR! Check out the docs of our coverage tool, it’s possibble to skip that line.