chore: Implement `useWriteStream` ServerContext (#36715)

Co-authored-by: Pierre Lehnen <55164754+pierre-lehnen-rc@users.noreply.github.com>
pull/36755/head^2
gabriellsh 5 months ago committed by GitHub
parent 34db122451
commit 6e009b1fbf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      apps/meteor/client/providers/ServerProvider.tsx
  2. 2
      apps/meteor/tests/mocks/client/ServerProviderMock.tsx
  3. 1
      packages/mock-providers/src/MockedAppRootBuilder.tsx
  4. 4
      packages/ui-contexts/src/ServerContext.ts
  5. 19
      packages/ui-contexts/src/hooks/useWriteStream.ts
  6. 1
      packages/ui-contexts/src/index.ts

@ -69,6 +69,9 @@ const getStream =
<K extends StreamKeys<N>>(eventName: K, callback: (...args: StreamerCallbackArgs<N, K>) => void): (() => void) =>
sdk.stream(streamName, [eventName], callback).stop;
const writeStream = <N extends StreamNames, K extends StreamKeys<N>>(streamName: N, streamKey: K, ...args: StreamerCallbackArgs<N, K>) =>
sdk.publish(streamName, [streamKey, ...args]);
const disconnect = () => Meteor.disconnect();
const reconnect = () => Meteor.reconnect();
@ -92,6 +95,7 @@ const ServerProvider = ({ children }: ServerProviderProps) => {
callEndpoint,
uploadToEndpoint,
getStream,
writeStream,
disconnect,
reconnect,
}),

@ -59,6 +59,7 @@ const getStream = () => () => () => undefined; // to be implemented
const callEndpoint = () => {
throw new Error('not implemented');
}; // to be implemented
const writeStream = () => undefined; // to be implemented
const contextValue: ServerContextValue = {
connected: true,
@ -72,6 +73,7 @@ const contextValue: ServerContextValue = {
getStream,
reconnect: () => undefined,
disconnect: () => undefined,
writeStream,
};
type ServerProviderMockProps = {

@ -85,6 +85,7 @@ export class MockedAppRootBuilder {
callMethod: () => Promise.reject(new Error('not implemented')),
disconnect: () => Promise.reject(new Error('not implemented')),
reconnect: () => Promise.reject(new Error('not implemented')),
writeStream: () => Promise.reject(new Error('not implemented')),
};
private router: ContextType<typeof RouterContext> = {

@ -48,6 +48,7 @@ export type ServerContextValue = {
retransmitToSelf?: boolean | undefined;
},
) => (eventName: K, callback: (...args: StreamerCallbackArgs<N, K>) => void) => () => void;
writeStream: <N extends StreamNames, K extends StreamKeys<N>>(streamName: N, eventName: K, ...args: StreamerCallbackArgs<N, K>) => void;
disconnect: () => void;
reconnect: () => void;
};
@ -65,6 +66,9 @@ export const ServerContext = createContext<ServerContextValue>({
throw new Error('not implemented');
},
getStream: () => () => (): void => undefined,
writeStream: () => {
throw new Error('not implemented');
},
disconnect: () => {
throw new Error('not implemented');
},

@ -0,0 +1,19 @@
import type { StreamNames, StreamKeys, StreamerCallbackArgs } from '@rocket.chat/ddp-client';
import { useCallback, useContext } from 'react';
import { ServerContext } from '../ServerContext';
type WriteStreamCallback<N extends StreamNames> = <K extends StreamKeys<N>>(eventName: K, ...args: StreamerCallbackArgs<N, K>) => void;
export function useWriteStream<N extends StreamNames>(streamName: N): WriteStreamCallback<N> {
const { writeStream } = useContext(ServerContext);
if (!writeStream) {
throw new Error(`cannot use useWriteStream(${streamName}) hook without a wrapping ServerContext`);
}
return useCallback(
<K extends StreamKeys<N>>(eventName: K, ...args: StreamerCallbackArgs<N, K>) => writeStream(streamName, eventName, ...args),
[writeStream, streamName],
);
}

@ -96,6 +96,7 @@ export { useAccountsCustomFields } from './hooks/useAccountsCustomFields';
export { useUserPresence } from './hooks/useUserPresence';
export { useUnstoreLoginToken } from './hooks/useUnstoreLoginToken';
export { useOnLogout } from './hooks/useOnLogout';
export { useWriteStream } from './hooks/useWriteStream';
export { UploadResult } from './ServerContext';
export { TranslationKey, TranslationLanguage } from './TranslationContext';

Loading…
Cancel
Save