-
-
Notifications
You must be signed in to change notification settings - Fork 59
State machine for posts
This page documents the sync algorithm used to enable offline operations.
Posts move through various states based on user actions. Each user action may move a post from its current state to one of several possible next states, depending on the action.
- Current state = current post status + current pending actions
- Next state = next post status + next pending actions
Current status + pending actions | User action | Next status | Pending actions |
---|---|---|---|
-- | create | draft | create |
draft (create) | edit | draft | create |
draft (create) | publish | published | create |
published (create) | edit | published | create |
published (create) | unpublish | draft | create |
draft | edit | draft | edit |
draft | publish | published | edit |
draft (edit) | publish | published | edit |
published | edit | published [1] | edit_local |
published | unpublish | draft | edit |
published (edit) | unpublish | draft | edit |
published (edit_local) | publish | published | edit |
Current status + pending actions | User action | Next status | Pending actions |
---|---|---|---|
new | delete | -- | (delete immediately) |
X (any other state) | delete | X | delete [2] |
It's pretty straight-forward:
- Upload posts with pending action ==
create
. - Upload posts with pending action ==
edit
.
- NOTE: this will overwrite any changes on the server that have not been synced to the app, but for now we'll live with that
- Download remote changes from server and update the db.
- Skip updating all posts which have local-only changes, to avoid overwriting them, because they are not uploaded until the user explicitly publishes them.
For published posts, we want to make sure we don't publish edits automatically. So there will have to be a temporary copy on the device until the user explicitly hits "Publish". For this we have a special pending action called edit_local
, which is never synced to the server (only posts with create
and edit
actions are synced). Once the user explicitly publishes, we simply set the pending action to edit
and it will get synced in the next sync cycle.
There are 2 additional wrinkles:
- What about posts that are edited remotely before the user publishes their changes? Right now the remote changes will be ignored during the sync, and eventually the local changes will overwrite the server copy on publishing.
- What about posts that are deleted remotely before the user publishes their changes? As of now the local copy will be deleted.
These posts are displayed grayed out, and are entirely unactionable until they are deleted remotely.