-
Notifications
You must be signed in to change notification settings - Fork 18
Atomic compare-and-swap operation? #56
Comments
Off-topic, but I think this would work as an addition to idb-keyval: export function update(key: IDBValidKey, updater: (val: any) => any, store = getDefaultStore()): Promise<void> {
return store._withIDBStore('readwrite', store => {
const req = store.get(key);
req.onsuccess = () => {
store.put(updater(req.result), key);
};
});
} |
re: offtopic - Oh that's so much simpler than what I was doing! I was messing around with trying to abort the transaction. |
This is very tempting to me, but in the past our storage folks have said that we should not encroach too much on IndexedDB's territory, instead referring people to the |
For the explicit request: I don't think compare-and-swap is appropriate semantics to layer on top of transactions. As I understand the compare-and-swap primitive it is a general low-level multi-threading primitive and in the event of failure values are re-computed and the operation re-attempted. It seems more appropriate to use transactions to address the concurrency issue, as Jake has proposed. For implementing something like |
compare-and-swap seems like a specific case of read-modify-write. If we do add a primitive, I'd prefer that it's I've written |
I recently ran into an issue with race conditions between the main thread and a worker thread clobbering each other's writes using @jakearchibald 's idb-keyval, and decided I needed a function like:
as a primitive in order to implement a mutex. Unfortunately, even after forking idb-keyval and hacking on it for a bit, I wasn't able to code such a function correctly, AFAICT. 😭
I know that thread-safety is probably beyond the scope of this module (see #5) but I think it would be nice if a future version could add support for atomic operations.
The text was updated successfully, but these errors were encountered: