Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
show only what's really needed to use a web worker within a library
- Loading branch information
1 parent
d7944e9
commit 6c4ca2d
Showing
10 changed files
with
108 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 36 additions & 3 deletions
39
projects/demo-lib-consuming-webworker/src/lib/demo-lib-consuming-webworker.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,40 @@ | ||
import { Component } from '@angular/core'; | ||
import { Component, Inject } from '@angular/core'; | ||
import { FIBONACCI_WEBWORKER_FACTORY } from 'fibonacci-webworker'; | ||
import { interval } from 'rxjs'; | ||
|
||
@Component({ | ||
selector: 'lib-demo-lib-consuming-webworker', | ||
template: ``, | ||
template: ` | ||
<p> | ||
Timer just to prove that the page isn't frozen while we compute fibonacci | ||
(which it would if we were doing it on the main thread!): | ||
{{ timer$ | async }} | ||
</p> | ||
<div *ngIf="result; else computingTpl"> | ||
<h1>Fibonacci 45</h1> | ||
<p>{{ result | json }}</p> | ||
</div> | ||
<ng-template #computingTpl> | ||
Computing fibonacci(45)... Be patient =)! | ||
</ng-template> | ||
`, | ||
}) | ||
export class DemoLibConsumingWebworkerComponent {} | ||
export class DemoLibConsumingWebworkerComponent { | ||
public result; | ||
|
||
public timer$ = interval(1000); | ||
|
||
constructor( | ||
@Inject(FIBONACCI_WEBWORKER_FACTORY) fibonacciWebworkerFactory: () => Worker | ||
) { | ||
const fibonacciWebworker = fibonacciWebworkerFactory(); | ||
|
||
fibonacciWebworker.onmessage = ({ data }) => { | ||
this.result = data; | ||
}; | ||
|
||
fibonacciWebworker.postMessage(45); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
export const FIBONACCI_WEBWORKER_FACTORY = new InjectionToken<() => Worker>( | ||
'fibonacci' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference lib="webworker" /> | ||
|
||
// this file is excluded from the compilation of the `fibonacci-webworker` library | ||
// this is due to the fact that an app has to import it directly (instead of a path | ||
// coming from a ts alias path) | ||
|
||
import { fibonacci } from 'fibonacci'; | ||
|
||
addEventListener('message', ({ data }) => { | ||
postMessage(fibonacci(data)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lib/fibonacci.token'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// I've chosen a slow implementation of fibonacci on purpose | ||
// so that it takes more time to run otherwise an optimised | ||
// version could take less than 1s to compute fibonacci(3000) | ||
// while this one will struggle with fibonacci(30) | ||
// https://stackoverflow.com/questions/11287418/why-is-this-js-code-so-slow | ||
export const fibonacci = (n) => { | ||
if (n < 2) { | ||
return n; | ||
} else { | ||
return fibonacci(n - 1) + fibonacci(n - 2); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<router-outlet></router-outlet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
import { NgModule } from '@angular/core'; | ||
|
||
import { BrowserModule } from '@angular/platform-browser'; | ||
import { RouterModule } from '@angular/router'; | ||
import { FIBONACCI_WEBWORKER_FACTORY } from 'fibonacci-webworker'; | ||
import { AppComponent } from './app.component'; | ||
|
||
@NgModule({ | ||
declarations: [ | ||
AppComponent | ||
], | ||
declarations: [AppComponent], | ||
imports: [ | ||
BrowserModule | ||
BrowserModule, | ||
RouterModule.forRoot([ | ||
{ | ||
path: '', | ||
loadChildren: () => | ||
import('demo-lib-consuming-webworker').then( | ||
(m) => m.DemoLibConsumingWebworkerModule | ||
), | ||
}, | ||
]), | ||
], | ||
providers: [ | ||
{ | ||
provide: FIBONACCI_WEBWORKER_FACTORY, | ||
useValue: function (): Worker { | ||
return new Worker('projects/fibonacci-webworker/src/lib/fibonacci', { | ||
name: 'fibonacci.worker', | ||
type: 'module', | ||
}); | ||
}, | ||
}, | ||
], | ||
providers: [], | ||
bootstrap: [AppComponent] | ||
bootstrap: [AppComponent], | ||
}) | ||
export class AppModule { } | ||
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./out-tsc/worker", | ||
"lib": ["es2018", "webworker"], | ||
"types": [] | ||
}, | ||
"include": [ | ||
"src/**/*.worker.ts", | ||
"projects/fibonacci-webworker/src/lib/fibonacci.ts" | ||
] | ||
} |