Skip to content

Commit 091a55d

Browse files
author
David Bartle
authored
Javascript workers (#27)
* Share rhino_worker amongst demos * Format rhino_worker * Add pause/resume to rhino worker
1 parent d709e49 commit 091a55d

File tree

7 files changed

+78
-113
lines changed

7 files changed

+78
-113
lines changed

demo/javascript/always-listening/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ pv_porcupine.wasm
77
pv_rhino.js
88
pv_rhino.wasm
99
rhino.js
10+
rhino_worker.js

demo/javascript/always-listening/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
"from": "../../../lib/wasm/pv_rhino.wasm",
3232
"to": "pv_rhino.wasm"
3333
},
34+
{
35+
"from": "../shared/rhino_worker.js",
36+
"to": "rhino_worker.js"
37+
},
3438
{
3539
"from": "../../../resources/porcupine/lib/wasm/pv_porcupine.js",
3640
"to": "pv_porcupine.js"

demo/javascript/always-listening/rhino_worker.js

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2018-2020 Picovoice Inc.
3+
You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
4+
file accompanying this source.
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
6+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
7+
specific language governing permissions and limitations under the License.
8+
*/
9+
10+
importScripts("pv_rhino.js");
11+
importScripts("rhino.js");
12+
13+
onmessage = function (e) {
14+
switch (e.data.command) {
15+
case "init":
16+
init(e.data.context);
17+
break;
18+
case "process":
19+
process(e.data.inputFrame);
20+
break;
21+
case "pause":
22+
paused = true;
23+
break;
24+
case "resume":
25+
paused = false;
26+
break;
27+
case "release":
28+
release();
29+
break;
30+
}
31+
};
32+
33+
let context;
34+
let paused;
35+
let rhino = null;
36+
37+
function init(context_) {
38+
context = context_;
39+
paused = false;
40+
41+
if (Rhino.isLoaded()) {
42+
rhino = Rhino.create(context);
43+
}
44+
}
45+
46+
function process(inputFrame) {
47+
if (rhino === null && Rhino.isLoaded()) {
48+
rhino = Rhino.create(context);
49+
}
50+
51+
if (!paused) {
52+
if (rhino !== null) {
53+
let result = rhino.process(inputFrame);
54+
if ("isUnderstood" in result) {
55+
postMessage(result);
56+
}
57+
}
58+
}
59+
}
60+
61+
function release() {
62+
if (rhino != null) {
63+
rhino.release();
64+
}
65+
66+
rhino = null;
67+
}

demo/javascript/standalone/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules
22
package-lock.json
33
pv_rhino.js
44
pv_rhino.wasm
5-
rhino.js
5+
rhino.js
6+
rhino_worker.js

demo/javascript/standalone/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
{
3131
"from": "../../../lib/wasm/pv_rhino.wasm",
3232
"to": "pv_rhino.wasm"
33+
},
34+
{
35+
"from": "../shared/rhino_worker.js",
36+
"to": "rhino_worker.js"
3337
}
3438
],
3539
"copyFilesSettings": {

demo/javascript/standalone/rhino_worker.js

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)