Skip to content

Commit fa88e5d

Browse files
committed
feat: add control client for adjusting blocking behavior
BREAKING CHANGE: only permitted options are accepted
1 parent 52317a3 commit fa88e5d

File tree

10 files changed

+180
-84
lines changed

10 files changed

+180
-84
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,3 @@ async function populate(stream, count) {
3232

3333
See the [API Docs](docs/classes/RedisStream.md#constructor) for available options.
3434

35-
## Advanced Usage
36-
37-
A common use case for redis streams is task processing. This iterable abstraction can help with handling tasks _at least once_.
38-
39-
For example, You can immediately setup a group and consumer for any stream or streams.

docs/classes/RedisStream.md

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- [buffers](RedisStream.md#buffers)
1616
- [client](RedisStream.md#client)
1717
- [consumer](RedisStream.md#consumer)
18+
- [control](RedisStream.md#control)
1819
- [count](RedisStream.md#count)
1920
- [deleteOnAck](RedisStream.md#deleteonack)
2021
- [done](RedisStream.md#done)
@@ -46,7 +47,7 @@
4647

4748
#### Defined in
4849

49-
[stream.ts:63](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L63)
50+
[stream.ts:75](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L75)
5051

5152
## Properties
5253

@@ -56,7 +57,7 @@
5657

5758
#### Defined in
5859

59-
[stream.ts:35](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L35)
60+
[stream.ts:43](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L43)
6061

6162
___
6263

@@ -66,7 +67,7 @@ ___
6667

6768
#### Defined in
6869

69-
[stream.ts:31](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L31)
70+
[stream.ts:39](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L39)
7071

7172
___
7273

@@ -76,21 +77,17 @@ ___
7677

7778
#### Defined in
7879

79-
[stream.ts:32](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L32)
80+
[stream.ts:40](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L40)
8081

8182
___
8283

8384
### client
8485

8586
`Readonly` **client**: `Redis`
8687

87-
'entry' mode will dispense each entry of each stream
88-
'stream' mode will dispense each stream containing entries
89-
'batch' mode will dispense all streams with all entries
90-
9188
#### Defined in
9289

93-
[stream.ts:23](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L23)
90+
[stream.ts:30](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L30)
9491

9592
___
9693

@@ -100,7 +97,17 @@ ___
10097

10198
#### Defined in
10299

103-
[stream.ts:25](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L25)
100+
[stream.ts:33](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L33)
101+
102+
___
103+
104+
### control
105+
106+
`Optional` `Readonly` **control**: `Redis`
107+
108+
#### Defined in
109+
110+
[stream.ts:31](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L31)
104111

105112
___
106113

@@ -110,7 +117,7 @@ ___
110117

111118
#### Defined in
112119

113-
[stream.ts:29](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L29)
120+
[stream.ts:37](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L37)
114121

115122
___
116123

@@ -120,7 +127,7 @@ ___
120127

121128
#### Defined in
122129

123-
[stream.ts:36](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L36)
130+
[stream.ts:44](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L44)
124131

125132
___
126133

@@ -132,7 +139,7 @@ Flag for iterable state
132139

133140
#### Defined in
134141

135-
[stream.ts:48](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L48)
142+
[stream.ts:56](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L56)
136143

137144
___
138145

@@ -142,7 +149,7 @@ ___
142149

143150
#### Defined in
144151

145-
[stream.ts:49](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L49)
152+
[stream.ts:57](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L57)
146153

147154
___
148155

@@ -152,7 +159,7 @@ ___
152159

153160
#### Defined in
154161

155-
[stream.ts:24](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L24)
162+
[stream.ts:32](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L32)
156163

157164
___
158165

@@ -162,7 +169,7 @@ ___
162169

163170
#### Defined in
164171

165-
[stream.ts:30](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L30)
172+
[stream.ts:38](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L38)
166173

167174
___
168175

@@ -171,12 +178,12 @@ ___
171178
**pendingAcks**: `Map`<`string`, `string`[]\>
172179

173180
Acks waiting to be sent on either:
174-
- timeout
181+
- interval
175182
- async iteration
176183

177184
#### Defined in
178185

179-
[stream.ts:44](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L44)
186+
[stream.ts:52](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L52)
180187

181188
___
182189

@@ -186,7 +193,7 @@ ___
186193

187194
#### Defined in
188195

189-
[stream.ts:28](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L28)
196+
[stream.ts:36](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L36)
190197

191198
## Methods
192199

@@ -200,7 +207,7 @@ ___
200207

201208
#### Defined in
202209

203-
[stream.ts:128](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L128)
210+
[stream.ts:145](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L145)
204211

205212
___
206213

@@ -221,7 +228,7 @@ ___
221228

222229
#### Defined in
223230

224-
[stream.ts:185](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L185)
231+
[stream.ts:204](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L204)
225232

226233
___
227234

@@ -235,7 +242,7 @@ ___
235242

236243
#### Defined in
237244

238-
[stream.ts:174](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L174)
245+
[stream.ts:190](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L190)
239246

240247
___
241248

@@ -249,4 +256,4 @@ ___
249256

250257
#### Defined in
251258

252-
[stream.ts:195](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L195)
259+
[stream.ts:214](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L214)

docs/interfaces/RedisStreamOptions.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [group](RedisStreamOptions.md#group)
1717
- [noack](RedisStreamOptions.md#noack)
1818
- [redis](RedisStreamOptions.md#redis)
19+
- [redisControl](RedisStreamOptions.md#rediscontrol)
1920
- [streams](RedisStreamOptions.md#streams)
2021

2122
## Properties
@@ -33,7 +34,7 @@ false
3334

3435
#### Defined in
3536

36-
[types.ts:71](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/types.ts#L71)
37+
[types.ts:77](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/types.ts#L77)
3738

3839
___
3940

@@ -46,7 +47,7 @@ while waiting for new entries on any stream, passed to xread or xreadgroup
4647

4748
#### Defined in
4849

49-
[types.ts:65](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/types.ts#L65)
50+
[types.ts:71](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/types.ts#L71)
5051

5152
___
5253

@@ -59,7 +60,7 @@ This applies to entry id and kv results
5960

6061
#### Defined in
6162

62-
[types.ts:54](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/types.ts#L54)
63+
[types.ts:60](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/types.ts#L60)
6364

6465
___
6566

@@ -72,7 +73,7 @@ Note: if only consumer is provided, a group is created automatically
7273

7374
#### Defined in
7475

75-
[types.ts:44](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/types.ts#L44)
76+
[types.ts:44](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/types.ts#L44)
7677

7778
___
7879

@@ -89,7 +90,7 @@ eg. the "highWaterMark"
8990

9091
#### Defined in
9192

92-
[types.ts:60](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/types.ts#L60)
93+
[types.ts:66](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/types.ts#L66)
9394

9495
___
9596

@@ -106,7 +107,7 @@ false
106107

107108
#### Defined in
108109

109-
[types.ts:77](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/types.ts#L77)
110+
[types.ts:83](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/types.ts#L83)
110111

111112
___
112113

@@ -121,7 +122,7 @@ TODO: not yet implemented
121122

122123
#### Defined in
123124

124-
[types.ts:89](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/types.ts#L89)
125+
[types.ts:95](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/types.ts#L95)
125126

126127
___
127128

@@ -134,7 +135,7 @@ Note: if only a group is provided a consumer is created automatically
134135

135136
#### Defined in
136137

137-
[types.ts:39](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/types.ts#L39)
138+
[types.ts:39](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/types.ts#L39)
138139

139140
___
140141

@@ -150,20 +151,34 @@ false
150151

151152
#### Defined in
152153

153-
[types.ts:82](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/types.ts#L82)
154+
[types.ts:88](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/types.ts#L88)
154155

155156
___
156157

157158
### redis
158159

159160
`Optional` **redis**: `string` \| `Redis` \| `RedisOptions`
160161

161-
The IORedis client connection.
162+
The IORedis client connection (reader).
162163
NOTE: by default this connection becomes a "reader" when block > 0
163164

164165
#### Defined in
165166

166-
[types.ts:49](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/types.ts#L49)
167+
[types.ts:49](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/types.ts#L49)
168+
169+
___
170+
171+
### redisControl
172+
173+
`Optional` **redisControl**: `string` \| `Redis` \| `RedisOptions`
174+
175+
The IORedis control client connection (writer).
176+
NOTE: by default this connection becomes a "writer" when block = 0 or Infinity
177+
Only allowed if block = 0 or Infinity
178+
179+
#### Defined in
180+
181+
[types.ts:55](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/types.ts#L55)
167182

168183
___
169184

@@ -175,4 +190,4 @@ Redis stream keys to be read. If a Record is provided each value is the starting
175190

176191
#### Defined in
177192

178-
[types.ts:34](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/types.ts#L34)
193+
[types.ts:34](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/types.ts#L34)

docs/modules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@
3535

3636
#### Defined in
3737

38-
[stream.ts:200](https://github.com/calebboyd/redis-x-stream/blob/4a00dad/src/stream.ts#L200)
38+
[stream.ts:219](https://github.com/calebboyd/redis-x-stream/blob/52317a3/src/stream.ts#L219)

src/redis.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ChainableCommander } from 'ioredis'
1+
import Redis, { ChainableCommander, RedisOptions } from 'ioredis'
22
import { RedisStream } from './stream.js'
33
import mkDebug from 'debug'
4-
import { XBatchResult, XStreamResult } from './types.js'
4+
import { XBatchResult, XStreamResult, env } from './types.js'
55

66
const debug = mkDebug('redis-x-stream')
77

@@ -102,3 +102,23 @@ function xreadgroup(
102102
...streams.values()
103103
)
104104
}
105+
106+
export function createClient(options?: Redis | string | RedisOptions) {
107+
let client: Redis,
108+
created = true
109+
if (typeof options === 'object') {
110+
if ('pipeline' in options) {
111+
client = options
112+
created = false
113+
} else {
114+
client = new Redis({ ...options })
115+
}
116+
} else if (typeof options === 'string') {
117+
client = new Redis(options)
118+
} else if (env.REDIS_X_STREAM_URL) {
119+
client = new Redis(env.REDIS_X_STREAM_URL)
120+
} else {
121+
client = new Redis()
122+
}
123+
return { client, created }
124+
}

0 commit comments

Comments
 (0)