Skip to content

Commit 116a0b8

Browse files
committed
fix readme
1 parent 759ca63 commit 116a0b8

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

README.md

+27-15
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ let mpx = new Multiplexer();
4545
// as Buffer instances, use `.toString()` to decode their
4646
// contents if necessary.
4747
async funcion onMessage(channel, message){
48-
console.log("ch:", channel.toString(), "msg:", message.toString());
48+
console.log("ch:", channel.toString(), "msg:", message.toString());
4949
}
5050

5151
// onDisconnect is a callback (can be async)
@@ -71,6 +71,10 @@ function onActivation(name){
7171
let channelSub = mpx.createChannelSubcription(onMessage, null, null);
7272
let patternSub = mpx.createPatternSubscription("hello-*", onMessage, null, onActivation);
7373
let promiseSub = mpx.createPromiseSubscription("hello-");
74+
75+
76+
// Close the multiplexer once you're done with it.
77+
mpx.close();
7478
```
7579

7680
### ChannelSubscription
@@ -89,6 +93,9 @@ channelSub.add("chan3");
8993
// Remove a channel
9094
channelSub.remove("chan2");
9195

96+
// Clear the subscription (remove all channels)
97+
channelSub.clear();
98+
9299
// Close the subscription
93100
channelSub.close();
94101
```
@@ -133,8 +140,10 @@ try {
133140
//
134141
// > PUBLISH hello-world "your-promise-payload"
135142
//
136-
} catch redismpx.SubscriptionInactiveError as e {
137-
// Wait and then Retry? Return an error to the user? Up to you.
143+
} catch (e) {
144+
if (e instanceof redismpx.SubscriptionInactiveError){
145+
// Wait and then Retry? Return an error to the user? Up to you.
146+
}
138147
}
139148

140149
// A way of creating a promise that ensures no SubscriptionInactiveError
@@ -151,20 +160,23 @@ try {
151160
let result = await promise
152161
console.log(result.toString()) // prints your-promise-payload
153162
} catch (e) {
154-
if (e instanceof redismpx.PromiseTimeoutError){
155-
// The promise timed out.
156-
} else if (e instanceof redismpx.SubscriptionInactiveError) {
157-
// The subscription became inactive while the promise was
158-
// still pending.
159-
} else if (e instanceof redismpx.SubscriptionClosedError) {
160-
// The subscription was closed while the promise was
161-
// still pending.
162-
}
163+
if (e instanceof redismpx.PromiseTimeoutError){
164+
// The promise timed out.
165+
} else if (e instanceof redismpx.SubscriptionInactiveError) {
166+
// The subscription became inactive while the promise was
167+
// still pending.
168+
} else if (e instanceof redismpx.SubscriptionClosedError) {
169+
// The subscription was closed while the promise was
170+
// still pending.
171+
}
163172
}
164173

165-
// Close the subscription (will automatically cancel all
166-
// outstanding promises and unlock all `waitFor*` waiters).
167-
promiseSub.close()
174+
// Clear the subscription (will reject all outstanding
175+
// promises and unlock all `waitFor*` waiters)
176+
promiseSub.clear();
177+
178+
// Close the subscription (will call clear()).
179+
promiseSub.close();
168180
```
169181

170182
## WebSocket Example

examples/channel.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ server.on('connection', function connection(ws) {
4646
});
4747

4848
ws.send("// Sending `+hello` will subscribe you to channel `hello`, while `-hello` will do the opposite.");
49-
ws.send("Sending `!hello` will broadcast the next message you send to `hello`.");
50-
ws.send("You can subscribe to more channels than one.");
49+
ws.send("// Sending `!hello` will broadcast the next message you send to `hello`.");
50+
ws.send("// You can subscribe to more channels than one.");
51+
ws.send("// If the server loses connection with Redis, it will automatically try to reconnect.");
52+
5153

5254
});
5355

src/promise.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ PromiseSubscription.prototype.waitForNewPromise =
6262
PromiseSubscription.prototype.newPromise =
6363
function(suffix, timeout) {
6464
if (this.closed) throw new SubscriptionClosedError("subscription closed");
65-
if (!this.active) throw SubscriptionInactiveError("the PromiseSubscription is not active");
65+
if (!this.active) throw new SubscriptionInactiveError("the PromiseSubscription is not active");
6666

6767
let channel = this.prefix + suffix;
6868
if (!this.channels[channel]) {

0 commit comments

Comments
 (0)