Repository navigation
Conversation
unsafeResetDatabase() closes Loki and then deletes the database. close() only waits for a save when Loki is dirty, but an autosave clears the dirty flags as soon as it starts. If one was still running, close() emitted 'close' synchronously and IncrementalIndexedDBAdapter.deleteDatabase threw "another operation is already in progress", rejecting the reset. Drain Loki's throttled save queue after close() and before deleting. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
Hi @joker-777, thank you, nice find! I maintain NitromelonDB, a maintained fork of WatermelonDB (New Architecture, Nitro Modules). It still ships the LokiJS adapter with the same Also possibly worth a look: on the web, our If you don't have the time or would rather not, no worries at all, I'm happy to port it myself. Just let me know. Thanks a lot! |
Problem
unsafeResetDatabase()on the LokiJS adapter callsdeleteDatabase(loki), which doesloki.close(() => loki.deleteDatabase(...)).close()only waits for a save if Loki is dirty. An autosave clears the dirty flags when it starts (ingetLokiCopy), so while one is in flight Loki looks clean.close()then emits'close'synchronously (Loki's listeners are synchronous), andIncrementalIndexedDBAdapter.deleteDatabasethrowsError while deleting database - another operation is already in progressbecauseoperationInProgressis still true. The throw comes out ofloki.close()inside the Promise executor, so the reset rejects.Fix
After
close(), wait for Loki's throttled save queue to drain (throttledSaveDrain) before callingdeleteDatabase. Two small guards:throttledSaveDraincalls its callback twice whenthrottledSavesis disabled (a lokijs bug: missingreturn), so the drain is skipped in that case.try/catchso it still rejects.Note: when a save is pending, Loki's drain runs one more save before calling back. That save finishes before the delete starts, so it's harmless.
Test
New Jest test in
src/adapters/lokijs/test.js. A fake incremental persistence adapter passed through_testLokiAdaptermimicsIncrementalIndexedDBAdapter: it throws on save/delete while another operation is in progress and can keep a save in flight. The test starts a save, callsunsafeResetDatabase(), checks that nothing is deleted while the save runs, then finishes the save and checks that the reset completes and the data is gone. Without the fix it fails with "another operation is already in progress".jest src/adapters/lokijs src/adapters/__tests__: 140 passedflow focus-checkon the changed file: cleanIndependent of #1979 (persistence error reporting); the two touch
lokiExtensions.jsin different places and merge cleanly in either order.🤖 Generated with Claude Code