Skip to content

Commit

Permalink
Javascript workers (#27)
Browse files Browse the repository at this point in the history
* Share rhino_worker amongst demos

* Format rhino_worker

* Add pause/resume to rhino worker
  • Loading branch information
David Bartle authored Jun 1, 2020
1 parent d709e49 commit 091a55d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 113 deletions.
1 change: 1 addition & 0 deletions demo/javascript/always-listening/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pv_porcupine.wasm
pv_rhino.js
pv_rhino.wasm
rhino.js
rhino_worker.js
4 changes: 4 additions & 0 deletions demo/javascript/always-listening/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"from": "../../../lib/wasm/pv_rhino.wasm",
"to": "pv_rhino.wasm"
},
{
"from": "../shared/rhino_worker.js",
"to": "rhino_worker.js"
},
{
"from": "../../../resources/porcupine/lib/wasm/pv_porcupine.js",
"to": "pv_porcupine.js"
Expand Down
56 changes: 0 additions & 56 deletions demo/javascript/always-listening/rhino_worker.js

This file was deleted.

67 changes: 67 additions & 0 deletions demo/javascript/shared/rhino_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2018-2020 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/

importScripts("pv_rhino.js");
importScripts("rhino.js");

onmessage = function (e) {
switch (e.data.command) {
case "init":
init(e.data.context);
break;
case "process":
process(e.data.inputFrame);
break;
case "pause":
paused = true;
break;
case "resume":
paused = false;
break;
case "release":
release();
break;
}
};

let context;
let paused;
let rhino = null;

function init(context_) {
context = context_;
paused = false;

if (Rhino.isLoaded()) {
rhino = Rhino.create(context);
}
}

function process(inputFrame) {
if (rhino === null && Rhino.isLoaded()) {
rhino = Rhino.create(context);
}

if (!paused) {
if (rhino !== null) {
let result = rhino.process(inputFrame);
if ("isUnderstood" in result) {
postMessage(result);
}
}
}
}

function release() {
if (rhino != null) {
rhino.release();
}

rhino = null;
}
3 changes: 2 additions & 1 deletion demo/javascript/standalone/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
package-lock.json
pv_rhino.js
pv_rhino.wasm
rhino.js
rhino.js
rhino_worker.js
4 changes: 4 additions & 0 deletions demo/javascript/standalone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
{
"from": "../../../lib/wasm/pv_rhino.wasm",
"to": "pv_rhino.wasm"
},
{
"from": "../shared/rhino_worker.js",
"to": "rhino_worker.js"
}
],
"copyFilesSettings": {
Expand Down
56 changes: 0 additions & 56 deletions demo/javascript/standalone/rhino_worker.js

This file was deleted.

0 comments on commit 091a55d

Please sign in to comment.