Skip to content

Commit

Permalink
React v2.0.2 patch (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
albho authored Nov 30, 2023
1 parent 25b057d commit 55af0bc
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
2 changes: 1 addition & 1 deletion binding/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ npm install --save @picovoice/leopard-react @picovoice/web-voice-processor

## Usage

Leopard requires a model file (`.pv`) at initialization. Use one of the default language models found in [lib/common](../../lib/common), or create a custom Leopard model in the [Picovoice Console](https://console.picovoice.ai/) for the target platform `Web (WASM)`.
Leopard requires a model file (`.pv`) at initialization. Use one of the default language models found in [lib/common](https://github.com/Picovoice/leopard/tree/master/lib/common), or create a custom Leopard model in the [Picovoice Console](https://console.picovoice.ai/) for the target platform `Web (WASM)`.

There are two methods to initialize Leopard.

Expand Down
2 changes: 1 addition & 1 deletion binding/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@picovoice/leopard-react",
"version": "2.0.1",
"version": "2.0.2",
"description": "React hook for Leopard Web SDK",
"entry": "src/index.ts",
"module": "dist/esm/index.js",
Expand Down
14 changes: 6 additions & 8 deletions binding/react/src/use_leopard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,12 @@ export const useLeopard = (): {

const stopRecording = useCallback(async (): Promise<void> => {
try {
if (!isRecording) {
if (!timerRef.current) {
return;
}

if (timerRef.current) {
clearInterval(timerRef.current);
timerRef.current = null;
}
clearInterval(timerRef.current);
timerRef.current = null;

await WebVoiceProcessor.unsubscribe(recorderEngineRef.current);
setIsRecording(false);
Expand All @@ -171,7 +169,7 @@ export const useLeopard = (): {
} catch (e: any) {
setError(e);
}
}, [isRecording]);
}, []);

const startRecording = useCallback(
async (maxRecordingSec = DEFAULT_MAX_RECORDING_SEC): Promise<void> => {
Expand All @@ -182,7 +180,7 @@ export const useLeopard = (): {
return;
}

if (isRecording) {
if (timerRef.current) {
return;
}

Expand All @@ -209,7 +207,7 @@ export const useLeopard = (): {
setError(e);
}
},
[isRecording]
[]
);

const release = useCallback(async (): Promise<void> => {
Expand Down
29 changes: 29 additions & 0 deletions binding/react/test/use_leopard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,35 @@ describe('Leopard binding', () => {
});
});

it('should be able to stop audio recording if limit reached', () => {
const { result } = renderHook(() => useLeopard());
const MAX_RECORDING_SEC = 1;

cy.wrapHook(() =>
result.current.init(ACCESS_KEY, {
publicPath: '/test/leopard_params.pv',
forceWrite: true,
})
).then(() => {
expect(
result.current.isLoaded,
`Failed to load 'leopard_params.pv' with ${result.current.error}`
).to.be.true;
});

cy.wrapHook(() => result.current.startRecording(MAX_RECORDING_SEC))
.then(() => {
expect(result.current.isRecording).to.be.true;
})
.wait(MAX_RECORDING_SEC * 1000)
.then(() => {
expect(result.current.isRecording).to.be.false;
expect(result.current.error?.toString()).to.contain(
'Maximum recording time reached'
);
});
});

for (const testParam of testData.tests.language_tests) {
const suffix = testParam.language === 'en' ? '' : `_${testParam.language}`;

Expand Down
2 changes: 1 addition & 1 deletion demo/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"description": "Leopard React demo (made with Create React App)",
"dependencies": {
"@picovoice/leopard-react": "=2.0.1",
"@picovoice/leopard-react": "=2.0.2",
"@picovoice/web-voice-processor": "~4.0.8",
"@types/node": "^18.11.9",
"@types/react": "^18.0.17",
Expand Down
8 changes: 4 additions & 4 deletions demo/react/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1608,10 +1608,10 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@picovoice/leopard-react@=2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@picovoice/leopard-react/-/leopard-react-2.0.1.tgz#5b9c3bc49ec708e0993bc9fbd65926de8ed98eda"
integrity sha512-pj0Q3xfuBdHfH9WsF1pu5Zo/pN+Yr8IkHE0RxveitTC/7MGMGXB+xWtU4wPiUYuezePNhwc4FOB12xxWGetQcQ==
"@picovoice/leopard-react@=2.0.2":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@picovoice/leopard-react/-/leopard-react-2.0.2.tgz#9bd78b85c837039ae17cf14e1a5dbb3b4da25eb6"
integrity sha512-UxbgXK49SeJ6jmSGsitFJtG3tiKm7N6v86D2UiWjwDN+RIrAu7pC9DpO5TZtFdl99hiCu7Ju/dCkC11GS6rwGg==
dependencies:
"@picovoice/leopard-web" "=2.0.1"

Expand Down

0 comments on commit 55af0bc

Please sign in to comment.