Skip to content

Commit 8e27c2b

Browse files
committed
util/channels
1 parent 85086e8 commit 8e27c2b

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

.dir-locals.el

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
((clojure-mode . ((cider-clojure-cli-aliases . "-A:dev"))))

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
.idea/
22
talk.iml
3+
.clj-kondo/
4+
.cpcache/
5+
.lsp/
6+
.nrepl-port

src/talk/util.clj

+11-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969

7070
(defn wrap-channel
7171
"Make io.netty.channel.Channel look a bit like a clojure map of the Channel's AttributeMap.
72-
Only supports plain keyword keys. Doesn't distinguish between Attribute with nil value and absent attribute."
72+
Only supports plain keyword keys. Doesn't distinguish between Attribute with nil value and absent attribute.
73+
NB Can't seq properly yet."
7374
[^Channel channel]
7475
(reify
7576
ILookup
@@ -101,10 +102,16 @@
101102
; Counted:
102103
(count [_] (unsupported "This is a wrapped io.netty.channel.Channel. No count."))))
103104

105+
(defn channels
106+
"Workaround for inability to reify seq (see `wrap-channel-group`).
107+
Returns map of ChannelId -> Channel (not wrapped)."
108+
[{:keys [clients] :as server}]
109+
(->> clients .iterator iterator-seq (map (fn [ch] [(.id ch) ch])) (into {})))
110+
104111
(defn wrap-channel-group
105112
"Make io.netty.channel.group.ChannelGroup look a bit like a clojure map of `ChannelId` -> `Channel`.
106113
Each `Channel` is wrapped with `wrap-channel`.
107-
NB Can't seq properly yet!!, use `(iterator-seq (.iterator x))` to get seq of raw channels..."
114+
NB Can't seq properly yet, see `channels` instead."
108115
; It's already a java.util.Set<io.netty.channel.Channel>.
109116
; Don't want to bring in clj-commons/potemkin for def-map-type!
110117
[^ChannelGroup channel-group]
@@ -131,9 +138,9 @@
131138
;; (map (fn [^Channel ch]
132139
;; (log/debug "Trying to create MapEntry for" (.id ch))
133140
;; (MapEntry. (.id ch) (wrap-channel ch))) channel-group))
134-
Iterable ;; NB these pretend it's just a Set
141+
Iterable
135142
;; (forEach [_ action] (.forEach channel-group action))
136-
(iterator [_] (.iterator channel-group))
143+
(iterator [_] (.iterator channel-group)) ; NB returns channels, not wrapped!
137144
;;(spliterator [_] (.spliterator channel-group))
138145
ILookup
139146
(valAt [_ k]

0 commit comments

Comments
 (0)