dumbmatter / fakeIndexedDB

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

Rewrite setTimeout / setImmediate behavior #70

Closed joshkel closed 2 years ago

joshkel commented 2 years ago

This revises fake-indexeddb's scheduling to work in Jest 27 + jsdom while still remanining performant. (See #69, #67, and related issues.) To review / summarize my current understanding:

I initially thought that I could improve performance by only using setTimeout to finish transactions and (to avoid setTimeout's slowness) using the microtask queue for everything else. This mostly worked, but I ran into a test failure because microtasks allowed fake-indexeddb database deletion operations to be interleaved with test code, producing out-of-order results. I fixed that by moving database deletion to setTimeout, which broke another test invoking database opening, and moving database opening to setTimeout broke another test. (See intermediate commits in this PR.)

So that approach didn't seem like it would pan out, so I gave up and broke jsdom's sandbox to access setImmediate, based on scala-js/scala-js-macrotask-executor#17 and jsdom/jsdom#2729. (It turns out that fake-indexeddb is not the first project to encounter this issue.) This is a dirty hack, but it's documented as a possibility, and it was really easy, and another project is using it.

dumbmatter commented 2 years ago

so I gave up and broke jsdom's sandbox

lol :)

This is great. Thank you so much! It does indeed seem to fix the problem, so I will release a new version shortly.