Skip to content

Commit 998ae33

Browse files
committed
fix: handle keyspace in RedisChannel#test method
1 parent 895fd1a commit 998ae33

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/channel.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { isString } from "radashi";
33
import { RedisKey, RedisKeyspace } from "./key";
44
import { MessageEvent } from "./subscriber";
55
import { RedisTransform } from "./transform";
6+
import { resolveName } from "./utils/resolve-name";
67

78
/**
89
* Channels use the `SUBSCRIBE` command.
@@ -33,21 +34,19 @@ export class RedisChannel<
3334
name: K extends RedisKeyspace<infer Key> ? Key : string | number,
3435
schema: TSchema = this.schema,
3536
) {
36-
return new RedisChannel<any>(
37-
`${isString(this.name) ? this.name : this.name.name}:${name}`,
38-
schema,
39-
);
37+
return new RedisChannel<any>(`${resolveName(this)}:${name}`, schema);
4038
}
4139

4240
/**
4341
* Returns true if the event originated from this channel or a
4442
* subchannel.
4543
*/
4644
test(event: MessageEvent<any>): event is MessageEvent<T> {
45+
const name = resolveName(this);
4746
return (
4847
event.key === this ||
49-
event.channel === this.name ||
50-
event.channel.startsWith(this.name + ":")
48+
event.channel === name ||
49+
event.channel.startsWith(name + ":")
5150
);
5251
}
5352
}

src/key.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as Type from "@sinclair/typebox/type";
22
import { StaticEncode, TObject, TSchema } from "@sinclair/typebox/type";
33
import { Decode, Encode } from "@sinclair/typebox/value";
4-
import { isString } from "radashi";
54
import { RedisValue } from "./command";
65
import { RedisTransform } from "./transform";
76
import { JSONPath, resolveSchemaForJSONPath } from "./utils/json-path";
7+
import { resolveName } from "./utils/resolve-name";
88

99
/**
1010
* Represents a namespace of keys in a Redis database.
@@ -59,10 +59,7 @@ export abstract class RedisKey<
5959
schema: TSchema,
6060
) => any;
6161

62-
return new RedisKey(
63-
`${isString(this.name) ? this.name : this.name.name}:${name}`,
64-
schema,
65-
);
62+
return new RedisKey(`${resolveName(this)}:${name}`, schema);
6663
}
6764
}
6865

src/utils/resolve-name.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { isString } from "radashi";
2+
import { RedisKeyspace } from "../key";
3+
4+
export function resolveName(key: { name: string | RedisKeyspace }) {
5+
return isString(key.name) ? key.name : key.name.name;
6+
}

0 commit comments

Comments
 (0)