Skip to content

Commit

Permalink
web error handler (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksyeo1010 authored Oct 26, 2023
1 parent 85f9d57 commit 39248a5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion binding/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Cobra VAD engine for web browsers (via WebAssembly)",
"author": "Picovoice Inc",
"license": "Apache-2.0",
"version": "2.0.0",
"version": "2.0.1",
"keywords": [
"cobra",
"web",
Expand Down
6 changes: 6 additions & 0 deletions binding/web/src/cobra_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class CobraWorker {
private readonly _version: string;
private readonly _frameLength: number;
private readonly _sampleRate: number;
private static _sdk: string = 'web';

private static _wasm: string;
private static _wasmSimd: string;
Expand Down Expand Up @@ -90,6 +91,10 @@ export class CobraWorker {
}
}

public static setSdk(sdk: string): void {
CobraWorker._sdk = sdk;
}

/**
* Creates a worker instance of the Picovoice Cobra VAD engine.
* Behind the scenes, it requires the WebAssembly code to load and initialize before
Expand Down Expand Up @@ -168,6 +173,7 @@ export class CobraWorker {
accessKey: accessKey,
wasm: this._wasm,
wasmSimd: this._wasmSimd,
sdk: this._sdk,
options: workerOptions,
});

Expand Down
21 changes: 15 additions & 6 deletions binding/web/src/cobra_worker_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ self.onmessage = async function (

Cobra.setWasm(event.data.wasm);
Cobra.setWasmSimd(event.data.wasmSimd);
Cobra.setSdk(event.data.sdk);
cobra = await Cobra.create(
event.data.accessKey,
voiceProbabilityCallback,
Expand All @@ -68,12 +69,20 @@ self.onmessage = async function (
sampleRate: cobra.sampleRate,
});
} catch (e: any) {
self.postMessage({
command: 'error',
status: PvStatus.RUNTIME_ERROR,
shortMessage: e.shortMessage,
messageStack: e.messageStack
});
if (e instanceof CobraError) {
self.postMessage({
command: 'error',
status: e.status,
shortMessage: e.shortMessage,
messageStack: e.messageStack
});
} else {
self.postMessage({
command: 'error',
status: PvStatus.RUNTIME_ERROR,
shortMessage: e.message
});
}
}
break;
case 'process':
Expand Down
2 changes: 1 addition & 1 deletion demo/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"author": "Picovoice Inc",
"license": "Apache-2.0",
"dependencies": {
"@picovoice/cobra-web": "~2.0.0",
"@picovoice/cobra-web": "~2.0.1",
"@picovoice/web-voice-processor": "~4.0.8"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions demo/web/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@picovoice/cobra-web@~2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@picovoice/cobra-web/-/cobra-web-2.0.0.tgz#fae10248546072faba3e784288135b07e576b0cd"
integrity sha512-twgDJKkty+NXUG9/1feM15TLTvyCTjiDLwoncWFeHqcJ02peIa84T27/5atzpYkXKKA3sp+/B1a+6EKDq7P6/g==
"@picovoice/cobra-web@~2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@picovoice/cobra-web/-/cobra-web-2.0.1.tgz#b2235af36439c23c192957abfed1b329c9a7b7a5"
integrity sha512-2ealbr5Km7NtvuAdTfQyWMI7DvHoi+A4wRnZx1JHRFp4NutfPwCO/xCmxmh2neiya4J1L6tpEuOafputdAfS/w==
dependencies:
"@picovoice/web-utils" "=1.3.1"

Expand Down

0 comments on commit 39248a5

Please sign in to comment.