Open cope opened 4 years ago
I would recommend to implement that using DBCore middleware. There is an implementation in Dexie.Observable already, but it is based on the older hooks api and I would like to see a new approach as Dexie.Observable includes a bit much of complexity that makes it hard to maintain. Dexie.Observable does more costly things than just observing which has performance inmplications. Most costly is that all calls to put() results in a get and a put, and that all data is double stored - both in the DB and in the _changes table. To write a fresh addon based on DBCore could probably allow reactive observation without the downsides of Dexie.Observable.
I needed something similar for an internal project, and created a TrackedTransaction add-on as DBCore middleware. You could probably adapt it to create change streams.
We had a very specific use case: our change tracking is opt-in at the transaction level, and its notification callback runs within the same transaction. But it should be easy enough to run on all transactions and use whatever event mechanism makes sense. (I included the test file in the gist; it has examples and covers a few cases we didn't bother implementing yet.)
The v3 hooks middleware source was really helpful in figuring out how to implement it. I did have to copy in a couple of internal Dexie helpers that aren't exported by Dexie itself (getEffectiveKeys and getExistingValues).
Would it be difficult to implement something like Change Streams? :)