diff --git a/docs/interfaces/client.ClientOptions.md b/docs/interfaces/client.ClientOptions.md index 8f934db3..769615b2 100644 --- a/docs/interfaces/client.ClientOptions.md +++ b/docs/interfaces/client.ClientOptions.md @@ -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. @@ -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` diff --git a/src/__tests__/client.ts b/src/__tests__/client.ts index 9f30767b..909eb71d 100644 --- a/src/__tests__/client.ts +++ b/src/__tests__/client.ts @@ -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(); diff --git a/src/client.ts b/src/client.ts index 000e7dae..418828f9 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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 @@ -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,