Skip to content

Instantly share code, notes, and snippets.

@wactbprot
Created January 5, 2021 12:26
Show Gist options
  • Save wactbprot/3125d65ab6eec6f7dcd0e2d869a1ec56 to your computer and use it in GitHub Desktop.
Save wactbprot/3125d65ab6eec6f7dcd0e2d869a1ec56 to your computer and use it in GitHub Desktop.
p-Registers a listener prints a event table
;;------------------------------
;; p-ubsub events
;;------------------------------
(def p-table (atom []) )
(defn p-start-table
"Registers a listener. Pretty prints a p-table on events.
Example:
```clojure
(p-start-table)
;; or
(p-start-table \"ref\")
;; or
(p-start-table \"ref\" \"*\" \"*\" \"state\")
```
Output example:
```
| :h | :m | :s | :meth | :k | :val |
|----+----+----+------------+---------------------------+----------|
| 12 | 54 | 25 | psubscribe | | |
| 12 | 55 | 21 | psubscribe | | |
| 12 | 55 | 26 | pmessage | ref@container@0@state@0@0 | working |
| 12 | 55 | 26 | pmessage | ref@container@0@state@0@1 | working |
| 12 | 55 | 29 | pmessage | ref@container@0@state@0@0 | executed |
| 12 | 55 | 30 | pmessage | ref@container@0@state@0@1 | executed |
| 12 | 55 | 30 | pmessage | ref@container@0@state@1@0 | working |
| 12 | 55 | 30 | pmessage | ref@container@0@state@1@1 | working |
| 12 | 55 | 30 | pmessage | ref@container@0@state@1@2 | working |
| 12 | 55 | 30 | pmessage | ref@container@0@state@1@3 | working |
| 12 | 55 | 33 | pmessage | ref@container@0@state@1@0 | executed |
| 12 | 55 | 33 | pmessage | ref@container@0@state@1@1 | executed |
| 12 | 55 | 33 | pmessage | ref@container@0@state@1@2 | executed |
| 12 | 55 | 34 | pmessage | ref@container@0@state@1@3 | executed |
| 12 | 55 | 34 | pmessage | ref@container@0@state@2@0 | working |
| 12 | 55 | 35 | pmessage | ref@container@0@state@2@0 | executed |
| 12 | 55 | 35 | pmessage | ref@container@0@state@0@0 | ready |
| 12 | 55 | 35 | pmessage | ref@container@0@state@0@1 | ready |
| 12 | 55 | 35 | pmessage | ref@container@0@state@1@0 | ready |
| 12 | 55 | 35 | pmessage | ref@container@0@state@1@1 | ready |
| 12 | 55 | 35 | pmessage | ref@container@0@state@1@2 | ready |
| 12 | 55 | 35 | pmessage | ref@container@0@state@1@3 | ready |
| 12 | 55 | 35 | pmessage | ref@container@0@state@2@0 | ready |
```
"
([]
(p-start-table "*" "*" "*" "*"))
([mp-id struct]
(p-start-table mp-id struct "*" "*"))
([mp-id struct i]
(p-start-table mp-id struct i "*"))
([mp-id struct i func]
(let [i (u/lp i)
cb! (fn [msg]
(let [d (u/get-date-object)
k (st/msg->key msg)
val (st/key->val k)]
(swap! p-table conj {:h (u/get-hour d)
:m (u/get-min d)
:s (u/get-sec d)
:meth (nth msg 0)
:k k
:val val })
(pp/print-table (deref p-table))))]
(st/register! mp-id struct i func cb!))))
(defn p-clear-table
[]
"Resets the p-table `atom`."
(reset! p-table []))
(defn p-stop-table
"De-registers the pubsub listener. Resets the p-table `atom`."
([]
(p-stop-table "*" "*" "*" "*"))
([mp-id struct]
(p-stop-table mp-id struct "*" "*"))
([mp-id struct i]
(p-stop-table mp-id struct i "*"))
([mp-id struct i func]
(p-clear-table)
(st/de-register! mp-id struct (u/lp i) func)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment