webxdc / webxdc-dev

A development server for webxdc apps
The Unlicense
20 stars 4 forks source link

clear indexedDB when wiping other storage APIs #55

Closed ansuz closed 2 months ago

ansuz commented 2 months ago

I discovered that indexedDB was not being cleared like localStorage and sessionStorage are, so I gave it a try. I have very little experience with typescript, however, so I just wrote everything as regular JavaScript. It seems to work, but I have no idea how the current code might impact any TypeScript tooling, and will need an expert to check that.

I found a comment indicating where the cleanup should happen, so somebody had at least thought of this issue before, which made it much easier than it might have otherwise been.

Since most interaction with indexedDB is asynchronous I had to defer the reload() call to run after all databases have been cleared. Currently this is done with a .then invocation, but .finally might be more appropriate just in case there are unhandled errors.

I tested this in the context of the app I was developing and confirmed that it cleaned up the stray databases that were left behind from previous versions of the app.

It relies on the indexedDB.databases() method which used to have very inconsistent support, but which should now work in any modern browser. the indexedDB.deleteDatabase() method likewise has very broad support.

WofWca commented 2 months ago

Thanks! I tried to look at the code that depends on clear being sync, but it was a little hard, couldn't find anything. Either way, this improves things.

FYI I pushed a refactor commit.

ansuz commented 2 months ago

Thanks! I'm going to be doing a lot more stuff with indexedDB, so this should get some much more thorough testing over the next few days :)