Skip to content

Commit

Permalink
Documentation for EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
codebeige committed Dec 13, 2023
1 parent 4ad50f6 commit c8d3904
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions src/moira/log/event_emitter.cljs
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
(ns moira.log.event-emitter
"Create the Application Log, a central hub for propagating Application
Events."

(:require [moira.event :as event]))

(defprotocol Listenable
(emit [this e])
(listen [this f] [this k f])
(unlisten [this] [this f] [this k f]))
"Provide means to subscribe to or unsubscribe from events of a specific
type."

(emit [this e]
"Dispatch event `e` to all listeners.
`:type` should be defined as keyword.")

(listen [this f] [this k f]
"Register `f` to be notified about events of type `k`.
If `k` is not given trigger `f` on all events.")

(unlisten [this] [this f] [this k f]
"Unregister `f` to no longer be triggered on events of type `k`.
If `k` is not given unregister `f` from all events. If both `k` and `f` are
not specified unregister all handlers from all events."))

(defprotocol Resumable
(pause [this])
(resume [this]))
"Pause dispatching events and buffer events to notify handlers on resume."

(pause [this]
"Pause dispatching of Application Events.
Buffer events for later dispatch.")

(resume [this]
"Resume dispatching of Application Events.
All buffered events are triggered first and in order."))

(def ^:private empty-registry {::any []})

Expand All @@ -27,7 +54,16 @@
(fire event registry)
(finally (resume this))))

(deftype EventEmitter [factory !registry !buffer !paused?]
(deftype
^{:doc
"Produce Application Events and propagate them to registered listeners.
Dispatching of events can be paused and resumed."}

EventEmitter

[factory !registry !buffer !paused?]

Listenable
(emit [this e]
(let [e* (event/->event factory e)]
Expand Down Expand Up @@ -55,7 +91,11 @@
(vswap! !buffer pop))
(vreset! !paused? false)))

(defn create []
(defn create
"Create [[EventEmitter]] instance to serve as Application Log."

[]

(->EventEmitter (event/factory)
(volatile! empty-registry)
(volatile! #queue [])
Expand Down

0 comments on commit c8d3904

Please sign in to comment.