Skip to content

Commit

Permalink
Dont dispatch on every event
Browse files Browse the repository at this point in the history
  • Loading branch information
mnemitz committed Dec 4, 2024
1 parent 75eb114 commit 1034d7f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions examples/nextjs/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
/* config options here */
reactStrictMode: false,
};

export default nextConfig;
17 changes: 14 additions & 3 deletions examples/nextjs/src/app/flow/OutputView.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
'use client';
import { useErrorBoundary } from 'react-error-boundary';
import { useFlowEventListener } from '@speechmatics/flow-client-react';
import {
type Message,
useFlowEventListener,
} from '@speechmatics/flow-client-react';
import { useFlowTranscript } from '@speechmatics/flow-client-react';
import { useRef } from 'react';

export function OutputView() {
useErrorView();

console.log('RENDER');

const { messages, handleEvent } = useFlowTranscript();
// lastMessagesRef.current ??= messages;
// console.log(messages === lastMessagesRef.current);

// console.log('messages=', messages);

useFlowEventListener('message', ({ data }) => {
if (
Expand All @@ -21,7 +32,7 @@ export function OutputView() {
return (
<article>
<header>Output</header>
<section>
{/* <section>
{messages.map((message) => (
<article key={`${message.startTime}-${message.endTime}`}>
<header>{message.speaker}</header>
Expand All @@ -31,7 +42,7 @@ export function OutputView() {
</span>
</article>
))}
</section>
</section> */}
</article>
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/flow-client-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@speechmatics/flow-client-react",
"version": "0.1.2-rc4",
"version": "0.1.2-rc5",
"description": "React hooks for interacting with the Speechmatics Flow API",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down
19 changes: 14 additions & 5 deletions packages/flow-client-react/src/use-flow-transcript.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
'use client';
import type {
AddPartialTranscriptMessage,
AddTranscriptMessage,
ResponseCompletedMessage,
ResponseStartedMessage,
ResponseInterruptedMessage,
} from '@speechmatics/flow-client';
import { useReducer } from 'react';
import { useCallback, useMemo, useReducer } from 'react';

export function useFlowTranscript() {
const [state, dispatch] = useReducer(messagesReducer, []);

return {
messages: state,
handleEvent: dispatch,
};
const handleEvent = useCallback((ev: IncomingEvent) => {
if (!eventIsEmpty(ev)) {
dispatch(ev);
}
}, []);

return useMemo(() => {
return {
messages: state,
handleEvent,
};
}, [state, handleEvent]);
}

export type Message = {
Expand Down

0 comments on commit 1034d7f

Please sign in to comment.