std.concurrent.bus

+keys+

+resource+

bus

added in 3.0

(bus)(bus m)

creates and starts a bus

bus:all-ids

added in 3.0

(bus:all-ids {:keys [state], :as bus})

returns all registered ids

(bus:with-temp bus (bus:all-ids bus)) => (contains #{string?})

bus:all-threads

added in 3.0

(bus:all-threads {:keys [state], :as bus})

returns all registered threads

(bus:with-temp bus (= (first (vals (bus:all-threads bus))) (Thread/currentThread))) => true

bus:close

added in 3.0

(bus:close bus id)

bus:closes all bus:opened loops

(bus:with-temp bus (let [{:keys stopped id} @(bus:open bus (fn m (update m :value inc)))] (Thread/sleep 10) (bus:close bus id) @stopped (bus:get-count bus))) => (contains-in {:exit :normal, :id string? :unprocessed empty? :start number? :end number?} 1)

bus:close-all

added in 3.0

(bus:close-all bus)

stops all thread loops

bus:create

added in 3.0

(bus:create)(bus:create m)

creates a bus

bus:deregister

added in 3.0

(bus:deregister bus)(bus:deregister {:keys [state], :as bus} id)

deregisters from the bus

bus:get-count

added in 3.0

(bus:get-count {:keys [state], :as bus})

returns the number of threads registered

bus:get-id

added in 3.0

(bus:get-id bus)(bus:get-id {:keys [state]} thread)

gets registered id given thread

(bus:with-temp bus (bus:get-id bus)) => string?

bus:get-queue

added in 3.0

(bus:get-queue bus)(bus:get-queue {:keys [state], :as bus} obj)

gets the message queue associated with the thread

(bus:with-temp bus (vec (bus:get-queue bus))) => []

bus:get-thread

added in 3.0

(bus:get-thread {:keys [state]} id)

gets thread given an id

(bus:with-temp bus (->> (bus:get-id bus) (bus:get-thread bus))) => (cc/thread:current)

bus:has-id?

added in 3.0

(bus:has-id? {:keys [state]} id)

checks that the bus has a given id

bus:kill

added in 3.0

(bus:kill bus id)

bus:closes all bus:opened loops

bus:kill-all

added in 3.0

(bus:kill-all bus)

stops all thread loops

bus:open

added in 3.0

(bus:open bus handler)(bus:open bus handler {:keys [id], :as opts, :or {id (std.lib.foundation/sid)}})

bus:opens a new handler loop given function

(bus:with-temp bus (let [{:keys id} @(bus:open bus (fn m (update m :value inc)))] (Thread/sleep 100) @(bus:send bus id {:value 1}))) => (contains {:value 2, :id string?})

bus:register

added in 3.0

(bus:register bus)(bus:register bus id)(bus:register {:keys [state], :as bus} id thread)

registers a thread to the bus

bus:reset-counters

added in 4.0

(bus:reset-counters bus)

resets the counters for a bus

bus:send

added in 3.0

(bus:send {:keys [results counters], :as bus} id msg)

sends a message to the given thread

bus:send-all

added in 3.0

(bus:send-all bus msg)

bus:sends message to all thread queues

bus:wait

added in 3.0

(bus:wait bus)(bus:wait bus {:keys [timeout timeunit], :or {timeunit :ms}})

bus:waits on the message queue for message

(bus:with-temp bus (bus:send bus (bus:get-id bus) {:op :hello :message “world”}) (bus:wait bus)) => (contains {:op :hello, :message “world”, :id string?})

(bus:with-temp bus (bus:wait bus {:timeout 100}))

bus:with-temp

macro

added in 3.0

(bus:with-temp var & body)

checks if object is instance of Bus

bus?

added in 3.0

(bus? obj)

checks if object is instance of Bus

handler-thunk

added in 3.0

(handler-thunk {:keys [output], :as bus} handler {:keys [stopped on-timeout], session-id :id, :as opts})

creates a thread loop for given message handler

info-bus

added in 3.0

(info-bus {:keys [state results output counters], :as bus})

returns info about the bus

main-loop

added in 3.0

(main-loop {:keys [state], :as bus})

creates a new message return loop

main-thunk

added in 3.0

(main-thunk {:keys [results output counters], :as bus})

creates main message return handler

run-handler

added in 3.0

(run-handler bus handler)(run-handler bus handler {:keys [id started stopped on-start on-stop], :as opts})

runs the handler in a thread loop

start-bus

added in 3.0

(start-bus bus)

starts the bus

started?-bus

added in 3.0

(started?-bus {:keys [state], :as bus})

checks if bus is running

(bus:with-temp bus (Thread/sleep 10) (started?-bus bus)) => true

stop-bus

added in 3.0

(stop-bus {:keys [state], :as bus})

stops the bus