-
Notifications
You must be signed in to change notification settings - Fork 4
feat(ama): guest, public realtime + guild list ordering #336
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| 'use client'; | ||
|
|
||
| import { useRef } from 'react'; | ||
| import type { RealtimeClient } from '@/api/ws'; | ||
| import { createPublicRealtimeClient } from '@/api/ws'; | ||
|
|
||
| /** | ||
| * The public answers page's gateway client (#323), created once per share token and kept stable for as long as | ||
| * that token is the one on screen -- `useRealtimeInvalidate` tears down and re-subscribes whenever the client | ||
| * identity changes, so an unstable reference would churn a socket per render. | ||
| * | ||
| * A ref rather than `useMemo` because this is a correctness requirement, not a performance one: React is free | ||
| * to discard a `useMemo` result whenever it likes. Keyed on the token rather than created once for the | ||
| * component's lifetime because the App Router reuses this component across `/ama-answers/[shareToken]` | ||
| * navigations -- only the param changes, so a lifetime-scoped client would keep minting tickets for the | ||
| * previous AMA. The outgoing client needs no explicit teardown: its socket closes on its own once the last | ||
| * subscription drops, which the re-subscribe on the next render triggers. | ||
| */ | ||
| export function usePublicRealtimeClient(shareToken: string): RealtimeClient { | ||
| const ref = useRef<{ client: RealtimeClient; shareToken: string } | null>(null); | ||
|
|
||
| if (ref.current?.shareToken !== shareToken) { | ||
| ref.current = { client: createPublicRealtimeClient(shareToken), shareToken }; | ||
| } | ||
|
|
||
| return ref.current.client; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/private/backend-core/src/lib/__tests__/realtimeBroadcast.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { beforeEach, expect, test, vi } from 'vitest'; | ||
| import { REALTIME_INVALIDATE_CHANNEL, publishRealtimeInvalidate } from '../realtimeBroadcast.js'; | ||
|
|
||
| const publish = vi.fn(); | ||
| const warn = vi.fn(); | ||
|
|
||
| // `realtimeBroadcast.ts` reaches for the redis client and logger through `getContext()` at call time, so | ||
| // stubbing the context is enough -- no real connection, and vitest hoists this above the static import above. | ||
| vi.mock('../context.js', () => ({ | ||
| getContext: () => ({ redis: { publish }, logger: { warn } }), | ||
| })); | ||
|
|
||
| function published(): { channel: string; originClientId?: string; type: string }[] { | ||
| return publish.mock.calls.map((call) => JSON.parse(call[1] as string)); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| publish.mockReset().mockResolvedValue(1); | ||
| warn.mockReset(); | ||
| }); | ||
|
|
||
| test('publishes a single channel on the shared invalidate channel', async () => { | ||
| await publishRealtimeInvalidate('ama-questions:123:1'); | ||
|
|
||
| expect(publish).toHaveBeenCalledOnce(); | ||
| expect(publish.mock.calls[0]![0]).toBe(REALTIME_INVALIDATE_CHANNEL); | ||
| expect(published()).toStrictEqual([{ type: 'invalidate', channel: 'ama-questions:123:1' }]); | ||
| }); | ||
|
|
||
| test('publishes one message per channel when given several', async () => { | ||
| // The WS subscriber dispatches on a single `channel`, so a batch is still n messages -- what the array | ||
| // form saves is issuing them in one tick (pipelined) rather than n sequential round trips. | ||
| await publishRealtimeInvalidate(['ama-questions:123:1', 'ama-public:1']); | ||
|
|
||
| expect(publish).toHaveBeenCalledTimes(2); | ||
| expect(published()).toStrictEqual([ | ||
| { type: 'invalidate', channel: 'ama-questions:123:1' }, | ||
| { type: 'invalidate', channel: 'ama-public:1' }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('tags every channel in a batch with the same originClientId', async () => { | ||
| await publishRealtimeInvalidate(['ama-questions:123:1', 'ama-public:1'], 'tab-abc'); | ||
|
|
||
| expect(published()).toStrictEqual([ | ||
| { type: 'invalidate', channel: 'ama-questions:123:1', originClientId: 'tab-abc' }, | ||
| { type: 'invalidate', channel: 'ama-public:1', originClientId: 'tab-abc' }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('omits originClientId entirely when absent, rather than sending undefined', async () => { | ||
| await publishRealtimeInvalidate('ama-questions:123:1'); | ||
|
|
||
| expect(published()[0]).not.toHaveProperty('originClientId'); | ||
| }); | ||
|
|
||
| test('is a no-op for an empty batch', async () => { | ||
| await publishRealtimeInvalidate([]); | ||
|
|
||
| expect(publish).not.toHaveBeenCalled(); | ||
| expect(warn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('swallows and logs a failed publish instead of throwing at the caller', async () => { | ||
| // The mutation that triggered this has already committed by the time any call site gets here -- a missed | ||
| // live refresh must not turn into a failed request. | ||
| publish.mockRejectedValue(new Error('redis is down')); | ||
|
|
||
| await expect(publishRealtimeInvalidate(['ama-questions:123:1', 'ama-public:1'])).resolves.toBeUndefined(); | ||
| expect(warn).toHaveBeenCalledOnce(); | ||
| expect(warn.mock.calls[0]![0]).toMatchObject({ channels: ['ama-questions:123:1', 'ama-public:1'] }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.