Keep two services holding resources of one type, such as tasks, in step.
This is the engine behind dgknght.tasks.sync and dgknght.memos.sync,
which describe the sync itself; apps use those.
A component describes the type of resource synchronized:
:list A function of a service to every resource it holds :put A function of a service and a resource that saves it and returns it as saved :delete A function of a service and a resource that deletes it :item The key under which a resource appears in a base entry, a summary's failed operation, etc., e.g. :task :items The key under which a base holds its entries, e.g. :tasks :encode A function of a resource to JSON-ready data :decode A function of a map read back from JSON with string keys to a resource :options The spec of the options map :base The spec of a base :errors A map of :invalid-options, :wrong-base and :invalid-base to the :type of the ex-info thrown for each
Resources are matched by their :uid. A resource's :url and :etag are the provider's, not part of its content, and aren't copied from one side to the other.
Keep two services holding resources of one type, such as tasks, in step.
This is the engine behind `dgknght.tasks.sync` and `dgknght.memos.sync`,
which describe the sync itself; apps use those.
A component describes the type of resource synchronized:
:list A function of a service to every resource it holds
:put A function of a service and a resource that saves it and
returns it as saved
:delete A function of a service and a resource that deletes it
:item The key under which a resource appears in a base entry, a
summary's failed operation, etc., e.g. :task
:items The key under which a base holds its entries, e.g. :tasks
:encode A function of a resource to JSON-ready data
:decode A function of a map read back from JSON with string keys to a
resource
:options The spec of the options map
:base The spec of a base
:errors A map of :invalid-options, :wrong-base and :invalid-base to
the :type of the ex-info thrown for each
Resources are matched by their :uid. A resource's :url and :etag are the
provider's, not part of its content, and aren't copied from one side to
the other.(local-wins _conflict)Resolves a conflict in favor of the local side.
Resolves a conflict in favor of the local side.
(parse-base {:keys [item items] :as component} s)Reads a base written by serialize-base.
Reads a base written by `serialize-base`.
(remote-wins _conflict)Resolves a conflict in favor of the remote side. The default.
Resolves a conflict in favor of the remote side. The default.
(serialize-base {:keys [item items encode]}
{:keys [local-id remote-id] :as base})The base as a JSON string, which parse-base reads back unchanged.
The base as a JSON string, which `parse-base` reads back unchanged.
(synchronize
{:keys [items] :as component}
local
remote
{:keys [base resolve dry-run?] :or {resolve remote-wins} :as options})Brings the local and remote services holding the component's resources into line: each resource created, updated or deleted on one side since the last sync is created, updated or deleted on the other. Returns a map of:
:summary What was done: :pulled Resources saved to the local side, as a map of :created, :updated and :deleted :pushed Likewise for the remote side :conflicts The resources changed on both sides since the last sync, each a map of its :uid, its :base, :local and :remote content (nil where it was deleted), and its :resolution: :local, :remote, :merged (with the :merged resource) or :unresolved :failed The operations that threw, each a map of the :operation (e.g. :push-update), the resource (under the component's :item) and the :error :base The base for the next sync, for the app to keep
Options:
:local-id Required. A string identifying the local service, e.g. the
file's path
:remote-id Required. A string identifying the remote service, e.g. the
calendar's URL
:base The base the last sync returned, or nil for the first sync,
when every resource is new on its side. A base made for
other ids is refused with a :wrong-base error, since it
would make the resources missing on one side look deleted
there
:resolve Settles a conflict: given its map of :uid, :base, :local and
:remote, returns :local or :remote for the side whose content
wins, a resource map to save to both, or nil to leave the
conflict unresolved (both sides as they are) until a later
sync. Defaults to remote-wins
:dry-run? When true, returns the summary of what would be done, with
nothing :failed, and the base unchanged, without saving
anything
Both sides are listed in full every time. Creates are carried out before updates and updates before deletes. When an operation fails, the uid's base entry is left as it was, so that the next sync tries again; the base records only what succeeded.
The app must not save resources through either service, or run another sync of the same pair, while a sync is running (e.g. by syncing and saving from the same agent or under the same lock), or a change can be lost.
Brings the local and remote services holding the component's resources
into line: each resource created, updated or deleted on one side since the
last sync is created, updated or deleted on the other. Returns a map of:
:summary What was done:
:pulled Resources saved to the local side, as a map of
:created, :updated and :deleted
:pushed Likewise for the remote side
:conflicts The resources changed on both sides since the
last sync, each a map of its :uid, its :base,
:local and :remote content (nil where it was
deleted), and its :resolution: :local, :remote,
:merged (with the :merged resource) or
:unresolved
:failed The operations that threw, each a map of the
:operation (e.g. :push-update), the resource
(under the component's :item) and the :error
:base The base for the next sync, for the app to keep
Options:
:local-id Required. A string identifying the local service, e.g. the
file's path
:remote-id Required. A string identifying the remote service, e.g. the
calendar's URL
:base The base the last sync returned, or nil for the first sync,
when every resource is new on its side. A base made for
other ids is refused with a :wrong-base error, since it
would make the resources missing on one side look deleted
there
:resolve Settles a conflict: given its map of :uid, :base, :local and
:remote, returns :local or :remote for the side whose content
wins, a resource map to save to both, or nil to leave the
conflict unresolved (both sides as they are) until a later
sync. Defaults to `remote-wins`
:dry-run? When true, returns the summary of what would be done, with
nothing :failed, and the base unchanged, without saving
anything
Both sides are listed in full every time. Creates are carried out before
updates and updates before deletes. When an operation fails, the uid's
base entry is left as it was, so that the next sync tries again; the base
records only what succeeded.
The app must not save resources through either service, or run another
sync of the same pair, while a sync is running (e.g. by syncing and
saving from the same agent or under the same lock), or a change can be
lost.cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |