Skip to content

Instantly share code, notes, and snippets.

@petterik
Created February 22, 2016 14:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petterik/50779d8cf15d6b6443e4 to your computer and use it in GitHub Desktop.
Save petterik/50779d8cf15d6b6443e4 to your computer and use it in GitHub Desktop.
Trying optimistic updates with om.next, datascript and datomic [Questions]
I'm trying to implement optimistic updates using om.next, datascript and datomic. Mutations should be visible instantly in the UI (the optimistic part) and synced with datomic once the mutation has been successfully transacted. Mutations should also be retryable.
I might be going about this all wrong, but here's the way I'm going about it:
Client:
0. Call om/transact with a mutation `[(my/mutation { ... }) :read/this...]
1. Transact update to datascript and save the optimistic transaction's tx number.
2. Pass the optimistic tx number in the remote's ast's :params (more on this later).
Server:
3. Transact update to datomic returning entities created in datomic and the optimistic tx number from :params, to client.
Client:
4. In merge!, revert datoms in the optimistic transaction by flipping the added bit in the datoms, and transact entities created in datomic.
5. Done.
I'm putting datascript's tx number in the remote's ast so that I can:
- Retry the remote mutation.
- Merge datomic's entities.
The problem I have is that I need to communicate the datascript tx number which is acquired in the parser's mutate's {:action (fn [] <here>)}, to the mutate's {:remote (assoc-in ast [params x] <to here>)}.
But the call to the thunk under :action is separated from when the parser uses the :remote key. See next.cljs line 880 and 881:
https://github.com/omcljs/om/blob/93e007fb72327ea35c3ad106f090f1209d442a7c/src/main/om/next.cljs#L880
https://github.com/omcljs/om/blob/93e007fb72327ea35c3ad106f090f1209d442a7c/src/main/om/next.cljs#L881
Would it make sense to pass the return value of when the parser transacts mutations locally (line 880) to gather-sends line 881 and in the call to mutate, pass the return value of the local mutation?
Is there anyone else who has done datascript+datomic syncing with om.next's mutations?
What other solutions are there?
What other ways are there to execute the merge! to get rid of the optimistic update and sync with datomic?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment