dumbmatter / fakeIndexedDB

A pure JS in-memory implementation of the IndexedDB API
Apache License 2.0
562 stars 69 forks source link

feat: configure nextMacroTask timer function #67 #68

Closed ph-fritsche closed 2 years ago

ph-fritsche commented 2 years ago

This change allows to replace calls to setImmediate (or the polyfill) e.g. in jsdom environment:

import { setNextMacroTask } from 'fake-indexeddb/auto'
setNextMacroTask(setTimeout)
import indexedDB, { setNextMacroTask } from 'fake-indexeddb'
setNextMacroTask(setTimeout)

Closes #67 Closes #64

dumbmatter commented 2 years ago

Thoughts on doing this, versus just replacing all setImmediate calls with setTimeout? That also seems to work without breaking any tests, and then no extra work is required to use it with jsdom. Maybe there was no good reason to use setImmediate in the first place..

ph-fritsche commented 2 years ago

Thoughts on doing this, versus just replacing all setImmediate calls with setTimeout? That also seems to work without breaking any tests, and then no extra work is required to use it with jsdom. Maybe there was no good reason to use setImmediate in the first place..

Dunno if there was a good reason when it was implemented. But replacing it might shuffle the order of execution and break tests in consuming projects and it might not be obvious why the tests start failing. So if it is replaced that should be a breaking change.

Additionally plugging in an own timer function allows to control the timing in tests. This allows to test intermediate states and race conditions.

dumbmatter commented 2 years ago

I have quite a few tests in this project, including on order of execution for async events. Of course that's no guarantee there will be no problem, but I think it will be okay. I'm going to go with that. If people actually have a use case for overriding besides this jsdom issue then I can add this new configurable API in the future, but I'm reluctant to add an API that possibly nobody actually would use.