-
-
Notifications
You must be signed in to change notification settings - Fork 37
Bind sendCommand to the current instance #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bind sendCommand to the current instance #227
Conversation
gamemaker1
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM apart from the one missing test. Thank you :)
| expect(result.totalHits).toEqual(1) | ||
| }) | ||
|
|
||
| it('should bind sendCommand to this', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a similar test for sendCommandCluster?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @gamemaker1
test/store-test.ts is missing a mock function -and some tests- for sendCommandCluster. Have you prepared such a method? It seems that sendCommandCluster option should be tested too. Maybe in a different PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gamemaker1 I have pushed a commit for testing sendCommandCluster binding. It's a simple test using the already implemented mock sendCommand. ioredis-mock provides a Cluster mock class -for future use- but it does not follow the specification of a redis cluster command
https://github.com/stipsan/ioredis-mock/?tab=readme-ov-file#clusterexperimental
source/lib.ts
Outdated
| // Normal case: wrap the sendCommand function to convert from cluster to regular | ||
| this.sendCommand = async ({ command }: SendCommandClusterDetails) => | ||
| options.sendCommand(...command) | ||
| options.sendCommand.bind(this)(...command) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't a huge deal, but could we bind sendCommand & sendCommandCluster once rather than every time they're called?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed a new commit for binding the given sendCommand before using it inside the wrapper func.
The sendCommandCluster function is already bound once.
nfriedly
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more little thing, but otherwise I think this is good to go
| class CustomRedisStore extends RedisStore { | ||
| constructor() { | ||
| super({ | ||
| sendCommand: customSendCommand, | ||
| }) | ||
| } | ||
| } | ||
| const store = new CustomRedisStore() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I missed this yesterday, but I don't think we need the new class here (?)
I think this would work:
| class CustomRedisStore extends RedisStore { | |
| constructor() { | |
| super({ | |
| sendCommand: customSendCommand, | |
| }) | |
| } | |
| } | |
| const store = new CustomRedisStore() | |
| const store = new RedisStore({ | |
| sendCommand: customSendCommand, | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case is being used here for approaching and presenting subclassing, where a class inherits RedisStore and encloses a custom sendCommand -as I have mentioned in the issue-. It's not really needed here. It might be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated sendCommand test case for understanding the usage of this binding while we are using a subclass of RedisStore.It now includes a more realistic case where decrement operation is not allowed for some reasons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I understand what you're doing better now. I think I can go with this.
test/store-test.ts
Outdated
| class CustomRedisClusterStore extends RedisStore { | ||
| constructor() { | ||
| super({ | ||
| sendCommandCluster: customSendCommandCluster, | ||
| }) | ||
| } | ||
| } | ||
| const store = new CustomRedisClusterStore() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto above
| class CustomRedisClusterStore extends RedisStore { | |
| constructor() { | |
| super({ | |
| sendCommandCluster: customSendCommandCluster, | |
| }) | |
| } | |
| } | |
| const store = new CustomRedisClusterStore() | |
| const store = new RedisStore ({ | |
| sendCommandCluster: customSendCommandCluster, | |
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed these lines of code because the usage of this keyword using a subclass is being presented in the previous test.
This PR closes #226 and binds
sendCommand, passed as parameter of theRedisStoreconstructor, to the current instance. This operation allowssendCommandmethod to usethiskeyword, if this is required.