Skip to content

Commit

Permalink
feat(client): Provide subscribe payload in generateID
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Sep 16, 2022
1 parent 1f2ad89 commit d0bc6e1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
10 changes: 8 additions & 2 deletions docs/interfaces/client.ClientOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ ___

### generateID

`Optional` **generateID**: () => `string`
`Optional` **generateID**: (`payload`: [`SubscribePayload`](common.SubscribePayload.md)) => `string`

#### Type declaration

▸ (): `string`
▸ (`payload`): `string`

A custom ID generator for identifying subscriptions.

Expand All @@ -100,6 +100,12 @@ in case you need more uniqueness.

Reference: https://gist.github.com/jed/982883

##### Parameters

| Name | Type |
| :------ | :------ |
| `payload` | [`SubscribePayload`](common.SubscribePayload.md) |

##### Returns

`string`
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,27 @@ describe('subscription operation', () => {
expect(generateIDFn).toBeCalled();
});

it('should provide subscription payload in `generateID`', async () => {
const { url, ...server } = await startTServer();

const generateIDFn = jest.fn(() => '1');

const client = createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
generateID: generateIDFn,
});

const payload = {
query: '{ getValue }',
};
tsubscribe(client, payload);
await server.waitForOperation();

expect(generateIDFn).toBeCalledWith(payload);
});

it('should dispose of the subscription on complete', async () => {
const { url, ...server } = await startTServer();

Expand Down
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export interface ClientOptions<
*
* Reference: https://gist.github.com/jed/982883
*/
generateID?: () => ID;
generateID?: (payload: SubscribePayload) => ID;
/**
* An optional override for the JSON.parse function used to hydrate
* incoming messages to this client. Useful for parsing custom datatypes
Expand Down Expand Up @@ -879,7 +879,7 @@ export function createClient<
return {
on: emitter.on,
subscribe(payload, sink) {
const id = generateID();
const id = generateID(payload);

let done = false,
errored = false,
Expand Down

0 comments on commit d0bc6e1

Please sign in to comment.