File tree Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change 1105
1105
1106
1106
(source-only s'))))
1107
1107
1108
+ (defn dropping-stream
1109
+ " Creates a new stream with a buffer of size `n`, which will drop
1110
+ incoming items when full.
1111
+
1112
+ If `source` is supplied, inserts a dropping stream after `source`
1113
+ with the provided capacity."
1114
+ ([n]
1115
+ (let [in (stream )
1116
+ out (dropping-stream n in)]
1117
+ (splice in out)))
1118
+ ([n source]
1119
+ (let [sink (stream n)]
1120
+ (connect-via
1121
+ source
1122
+ (fn [val]
1123
+ (d/let-flow [put-result (try-put! sink val 0 :timeout )]
1124
+ (case put-result
1125
+ true true
1126
+ false false
1127
+ :timeout true )))
1128
+ sink
1129
+ {:upstream? true
1130
+ :downstream? true })
1131
+ sink)))
1132
+
1133
+ (defn sliding-stream
1134
+ " Creates a new stream with a buffer of size `n`, which will drop
1135
+ the oldest items when full to make room for new items.
1136
+
1137
+ If `source` is supplied, inserts a sliding stream after `source`
1138
+ with the provided capacity."
1139
+ ([n]
1140
+ (let [in (stream )
1141
+ out (sliding-stream n in)]
1142
+ (splice in out)))
1143
+ ([n source]
1144
+ (let [sink (stream n)]
1145
+ (connect-via
1146
+ source
1147
+ (fn [val]
1148
+ (d/loop []
1149
+ (d/chain
1150
+ (try-put! sink val 0 :timeout )
1151
+ (fn [put-result]
1152
+ (case put-result
1153
+ true true
1154
+ false false
1155
+ :timeout (d/chain (take! sink)
1156
+ (fn [_] (d/recur ))))))))
1157
+ sink
1158
+ {:upstream? true
1159
+ :downstream? true })
1160
+ sink)))
1161
+
1108
1162
; ;;
1109
1163
1110
1164
(alter-meta! #'->Callback assoc :private true )
Original file line number Diff line number Diff line change 465
465
(is (s/closed? sink))
466
466
(is (s/closed? src))))
467
467
468
+ (deftest test-window-streams
469
+ (testing " dropping-stream"
470
+ (let [s (s/->source (range 11 ))
471
+ sliding-s (s/dropping-stream 10 s)]
472
+ (is (= (range 10 )
473
+ (s/stream->seq sliding-s)))))
474
+
475
+ (testing " sliding-stream"
476
+ (let [s (s/->source (range 11 ))
477
+ sliding-s (s/sliding-stream 10 s)]
478
+ (is (= (range 1 11 )
479
+ (s/stream->seq sliding-s))))) )
480
+
468
481
; ;;
469
482
470
483
(deftest ^:stress stress-buffered-stream
You can’t perform that action at this time.
0 commit comments