Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/client/lib/client/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import * as PUBSUB_CHANNELS from '../commands/PUBSUB_CHANNELS';
import * as PUBSUB_NUMPAT from '../commands/PUBSUB_NUMPAT';
import * as PUBSUB_NUMSUB from '../commands/PUBSUB_NUMSUB';
import * as PUBSUB_SHARDCHANNELS from '../commands/PUBSUB_SHARDCHANNELS';
import * as PUBSUB_SHARDNUMSUB from '../commands/PUBSUB_SHARDNUMSUB';
import * as RANDOMKEY from '../commands/RANDOMKEY';
import * as READONLY from '../commands/READONLY';
import * as READWRITE from '../commands/READWRITE';
Expand Down Expand Up @@ -323,6 +324,8 @@ export default {
pubSubNumSub: PUBSUB_NUMSUB,
PUBSUB_SHARDCHANNELS,
pubSubShardChannels: PUBSUB_SHARDCHANNELS,
PUBSUB_SHARDNUMSUB,
pubSubShardNumSub: PUBSUB_SHARDNUMSUB,
RANDOMKEY,
randomKey: RANDOMKEY,
READONLY,
Expand Down
35 changes: 35 additions & 0 deletions packages/client/lib/commands/PUBSUB_SHARDNUMSUB.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './PUBSUB_SHARDNUMSUB';

describe('PUBSUB SHARDNUMSUB', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments(),
['PUBSUB', 'SHARDNUMSUB']
);
});

it('string', () => {
assert.deepEqual(
transformArguments('channel'),
['PUBSUB', 'SHARDNUMSUB', 'channel']
);
});

it('array', () => {
assert.deepEqual(
transformArguments(['1', '2']),
['PUBSUB', 'SHARDNUMSUB', '1', '2']
);
});
});

testUtils.testWithClient('client.pubSubShardNumSub', async client => {
assert.deepEqual(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please make the test "non empty" so it'll actually test transforReply as well?

await client.pubSubShardNumSub(),
Object.create(null)
);
}, GLOBAL.SERVERS.OPEN);
});
24 changes: 24 additions & 0 deletions packages/client/lib/commands/PUBSUB_SHARDNUMSUB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { pushVerdictArguments } from './generic-transformers';
import { RedisCommandArgument, RedisCommandArguments } from '.';

export const IS_READ_ONLY = true;

export function transformArguments(
channels?: Array<RedisCommandArgument> | RedisCommandArgument
): RedisCommandArguments {
const args = ['PUBSUB', 'SHARDNUMSUB'];

if (channels) return pushVerdictArguments(args, channels);

return args;
}

export function transformReply(rawReply: Array<string | number>): Record<string, number> {
const transformedReply = Object.create(null);

for (let i = 0; i < rawReply.length; i +=2) {
Copy link

@younes-io younes-io Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use entries() to iterate over the array of records.. something idiomatic like:

 for (const [channel, numSubscribers] of rawReply.entries()) {
    transformedReply[channel] = numSubscribers;
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every first factor of the iterator returned by the entries() is index, so I think it is not an appropriate use 🤔

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does "rawReply" look like ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://redis.io/commands/pubsub-shardnumsub/

Same as reply in pubsub numsub. Repeated channel(string) and count(number)

transformedReply[rawReply[i]] = rawReply[i + 1];
}

return transformedReply;
}