What are the specific steps to add a SharedWorker
to an @angular/cli >1.2
-generated project. I would like the SharedWorker to be defined in TypeScript (with full/correct type-definitions editor support), to share interfaces with the main project, and to be continuously compiled and tested along with the main project.
I haven't discovered an example or blog post that describes how to modify a cli-generated project to include either a Worker
or a SharedWorker
. However, there are several posts (such as this one) which show how to transform an entire cli-generated project to run it as a web worker. But that's not my use-case.
I want to confine interaction with a Lovefield database to a SharedWorker
that runs in a separate process from the main application.
Though I've experimented a bit attempting to figure this out, I haven't made much progress. Hopefully someone can save me (and future readers) a lot of time.
I got this to work following these steps:
src\app\shared-worker
Create file
src\app\shared-worker\shared-worker.d.ts
with the following contents:SharedWorker definitions
Create file
src\app\shared-worker\shared-worker.ts
with the following demo contents:Sharedworker source
Update
src\app\app.component.ts
as follows:AppComponent
Add
"app/shared-worker/shared-worker.js"
to theapps.assets
node in.angular-cli.json
concurrently
as a dev dependency:npm i -D concurrently
Create
package.json
scriptspackage.json