Skip to content

Instantly share code, notes, and snippets.

@chaptastic
Last active December 25, 2015 17:19
Show Gist options
  • Save chaptastic/7012476 to your computer and use it in GitHub Desktop.
Save chaptastic/7012476 to your computer and use it in GitHub Desktop.
(defprotocol ReactorStateRef
(restore-state! [this]
"Restore the referenced state to the most recent snapshot if available
or to the initial state. Update the version to reflect the new state.")
(reset-state! [this]
"Reset the referenced state to the initial state. Update the version to 0.")
(apply-event! [this event version]
"Apply the supplied event to the referenced state and update the version.
Nil events are considered to be no-op and only update the version. If the
version is nil, apply the event but do not update the version.")
(current-version [this]
"Return the current version represented by the refernced state."))
(defn make-reactor
"Create a reactor with the supplied name and handler componenets.
- connection should be an open RabbitMQ connection.
- name is arbitrary but should be valid as a queue name and routing
selector since it is used to direct commands and control messages
to this router.
- state-ref should implement ReactorStateRef. it is responsible for managing
the state affected by this router's event stream
- event-handler is a function from state-ref and event to a pair of
sequences [commands events]. Commands is a possibly empty sequence of
[destination command] pairs. Events is a possibly empty sequence of
events to apply to the state of this handler.
event-handler MUST not have any side effects.
- command-handler is a function from state-ref and command to a pair as
returned by event-handler. command-handler may have external side effects
as needed to complete the requested command. If a command needs to do
long running processing it should spawn a worker task and return the
result as a followup command.
After calling the event and command handlers the reactor will apply any
returned events to the state of this reactor and publish them to the remainder
of the system. After event processing has completed commands returned are
routed to their requested destinations."
[connection name state-ref event-handler command-handler])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment