Skip to content

Commit 5d741ad

Browse files
authored
Merge pull request #68 from cloudnc/feat/webpack-5-support
2 parents f84d0ef + 0aba7f4 commit 5d741ad

22 files changed

Lines changed: 107 additions & 154 deletions

.betterer.results

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
// BETTERER RESULTS V1.
22
exports[`stricter compilation`] = {
3-
timestamp: 1616658221627,
3+
timestamp: 1619240872304,
44
value: `{
5-
"projects/observable-webworker/src/lib/from-worker-pool.spec.ts:634732869": [
6-
[288, 33, 33, "Argument of type \'(observer: Observer<number>) => () => void\' is not assignable to parameter of type \'(this: Observable<T>, subscriber: Subscriber<T>) => TeardownLogic\'.\\n Types of parameters \'observer\' and \'subscriber\' are incompatible.\\n Type \'Subscriber<T>\' is not assignable to type \'Observer<number>\'.\\n Types of property \'next\' are incompatible.\\n Type \'(value?: T | undefined) => void\' is not assignable to type \'(value: number) => void\'.\\n Types of parameters \'value\' and \'value\' are incompatible.\\n Type \'number\' is not assignable to type \'T | undefined\'.", "1166033031"]
7-
],
8-
"projects/observable-webworker/src/lib/from-worker.spec.ts:2047567224": [
9-
[92, 4, 20, "Cannot invoke an object which is possibly \'null\'.", "650422727"],
10-
[171, 4, 20, "Cannot invoke an object which is possibly \'null\'.", "650422727"],
11-
[177, 4, 20, "Cannot invoke an object which is possibly \'null\'.", "650422727"]
12-
],
13-
"projects/observable-webworker/src/lib/from-worker.ts:4009652202": [
14-
[28, 56, 11, "Argument of type \'Input | undefined\' is not assignable to parameter of type \'Input\'.\\n \'Input\' could be instantiated with an arbitrary type which could be unrelated to \'Input | undefined\'.\\n Type \'undefined\' is not assignable to type \'Input\'.\\n \'Input\' could be instantiated with an arbitrary type which could be unrelated to \'undefined\'.", "1574273654"]
15-
],
16-
"projects/observable-webworker/src/lib/run-worker.ts:4122498212": [
17-
[4, 65, 7, "Rest parameter \'args\' implicitly has an \'any[]\' type.", "3622679692"],
18-
[51, 38, 26, "Cannot invoke an object which is possibly \'undefined\'.", "4028913799"],
19-
[51, 65, 18, "Argument of type \'O | undefined\' is not assignable to parameter of type \'O\'.\\n \'O\' could be instantiated with an arbitrary type which could be unrelated to \'O | undefined\'.\\n Type \'undefined\' is not assignable to type \'O\'.\\n \'O\' could be instantiated with an arbitrary type which could be unrelated to \'undefined\'.", "3307950093"]
20-
],
21-
"src/app/file-hash.worker.ts:1230899360": [
22-
[0, 21, 8, "Could not find a declaration file for module \'js-md5\'. \'./node_modules/js-md5/src/md5.js\' implicitly has an \'any\' type.\\n Try \`npm install @types/js-md5\` if it exists or add a new declaration (.d.ts) file containing \`declare module \'js-md5\';\`", "3516231053"]
23-
],
245
"src/app/google-charts.service.ts:1308226900": [
256
[3, 29, 15, "Could not find a declaration file for module \'google-charts\'. \'./node_modules/google-charts/dist/googleCharts.js\' implicitly has an \'any\' type.\\n Try \`npm install @types/google-charts\` if it exists or add a new declaration (.d.ts) file containing \`declare module \'google-charts\';\`", "2535818334"]
267
],
@@ -30,15 +11,8 @@ exports[`stricter compilation`] = {
3011
[13, 9, 5, "Property \'color\' has no initializer and is not definitely assigned in the constructor.", "176948952"],
3112
[18, 44, 17, "Argument of type \'string | undefined\' is not assignable to parameter of type \'string\'.\\n Type \'undefined\' is not assignable to type \'string\'.", "463397358"]
3213
],
33-
"src/app/multiple-worker-pool/multiple-worker-pool.component.ts:3101768987": [
34-
[40, 55, 17, "Property \'timelineComponent\' has no initializer and is not definitely assigned in the constructor.", "964610961"],
14+
"src/app/multiple-worker-pool/multiple-worker-pool.component.ts:1042385498": [
3515
[90, 9, 14, "Type \'Observable<{ millisSinceLast: number | null; file?: string | undefined; timestamp: Date; message: string; thread: Thread; fileEventType: FileHashEvent | null; }[]>\' is not assignable to type \'Observable<HashWorkerMessage[]>\'.\\n Type \'{ millisSinceLast: number | null; file?: string | undefined; timestamp: Date; message: string; thread: Thread; fileEventType: FileHashEvent | null; }[]\' is not assignable to type \'HashWorkerMessage[]\'.\\n Type \'{ millisSinceLast: number | null; file?: string | undefined; timestamp: Date; message: string; thread: Thread; fileEventType: FileHashEvent | null; }\' is not assignable to type \'HashWorkerMessage\'.\\n Types of property \'millisSinceLast\' are incompatible.\\n Type \'number | null\' is not assignable to type \'number | undefined\'.\\n Type \'null\' is not assignable to type \'number | undefined\'.", "463576723"]
36-
],
37-
"src/app/single-worker/single-worker.component.ts:228104130": [
38-
[25, 22, 6, "Parameter \'$event\' implicitly has an \'any\' type.", "3058907597"]
39-
],
40-
"src/readme/worker-pool-hash.worker.ts:2220989309": [
41-
[0, 21, 8, "Could not find a declaration file for module \'js-md5\'. \'./node_modules/js-md5/src/md5.js\' implicitly has an \'any\' type.\\n Try \`npm install @types/js-md5\` if it exists or add a new declaration (.d.ts) file containing \`declare module \'js-md5\';\`", "3516231053"]
4216
]
4317
}`
4418
};

README.md

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ yarn add observable-webworker
5959

6060
#### Main Thread
6161

62+
💡 Take note! The webworker construction syntax differs for different version of webpack:
63+
64+
#### Webpack < 5
65+
6266
```ts
6367
// src/readme/hello.ts
6468

@@ -71,18 +75,34 @@ fromWorker<string, string>(() => new Worker('./hello.worker', { type: 'module' }
7175
console.log(message); // Outputs 'Hello from webworker'
7276
});
7377

78+
```
79+
#### Webpack 5
80+
81+
```ts
82+
// src/readme/hello-webpack-5.ts#L2-L12
83+
84+
import { fromWorker } from 'observable-webworker';
85+
import { of } from 'rxjs';
86+
87+
const input$ = of('Hello from main thread');
88+
89+
fromWorker<string, string>(
90+
() => new Worker(new URL('./app.worker', import.meta.url), { type: 'module' }),
91+
input$,
92+
).subscribe(message => {
93+
console.log(message); // Outputs 'Hello from webworker'
94+
});
7495
```
7596

7697
#### Worker Thread
7798

7899
```ts
79100
// src/readme/hello.worker.ts
80101

81-
import { DoWork, ObservableWorker } from 'observable-webworker';
102+
import { DoWork, runWorker } from 'observable-webworker';
82103
import { Observable } from 'rxjs';
83104
import { map } from 'rxjs/operators';
84105

85-
@ObservableWorker()
86106
export class HelloWorker implements DoWork<string, string> {
87107
public work(input$: Observable<string>): Observable<string> {
88108
return input$.pipe(
@@ -94,35 +114,20 @@ export class HelloWorker implements DoWork<string, string> {
94114
}
95115
}
96116

97-
```
98-
99-
##### Important Note
100-
You **must** export your worker class (`export class ...`) from the file if you're using a minifier. If you don't, your
101-
class will be removed from the bundle, causing your worker to do nothing!
102-
103-
You'll probably need to export the class anyway as you are unit testing it right?!
104-
105-
##### Don't like decorators? Don't use 'em!
117+
runWorker(HelloWorker);
106118

107-
If decorators is not something you use regularly and prefer direct functions, simply
108-
use the `runWorker` function instead.
119+
```
109120

110-
```ts
111-
// src/readme/hello-no-decorator.worker.ts#L5-L16
121+
#### Decorator deprecation notice
122+
Future versions of webpack (Webpack 5) minify webworkers overly aggressively, causing
123+
the `@ObservableWorker()` decorator pattern to no longer function. This decorator
124+
has now been deprecated, and will be removed in the next major version of this library.
112125

113-
class HelloWorker implements DoWork<string, string> {
114-
public work(input$: Observable<string>): Observable<string> {
115-
return input$.pipe(
116-
map(message => {
117-
console.log(message); // outputs 'Hello from main thread'
118-
return `Hello from webworker`;
119-
}),
120-
);
121-
}
122-
}
126+
To migrate from decorators, simply remove the decorator, and invoke the `runWorker`
127+
with your class passed as argument (see example above).
123128

124-
runWorker(HelloWorker);
125-
```
129+
Make sure you **don't forget to remove the decorator** when you add the `runWorker(...)`
130+
function, otherwise the worker will be run twice, each acting on any message sent.
126131

127132
## Transferable
128133

@@ -209,11 +214,10 @@ export function computeHashes(files: File[]): Observable<string> {
209214
// src/readme/worker-pool-hash.worker.ts
210215

211216
import * as md5 from 'js-md5';
212-
import { DoWorkUnit, ObservableWorker } from 'observable-webworker';
217+
import { DoWorkUnit, runWorker } from 'observable-webworker';
213218
import { Observable } from 'rxjs';
214219
import { map } from 'rxjs/operators';
215220

216-
@ObservableWorker()
217221
export class WorkerPoolHashWorker implements DoWorkUnit<File, string> {
218222
public workUnit(input: File): Observable<string> {
219223
return this.readFileAsArrayBuffer(input).pipe(map(arrayBuffer => md5(arrayBuffer)));
@@ -239,6 +243,8 @@ export class WorkerPoolHashWorker implements DoWorkUnit<File, string> {
239243
}
240244
}
241245

246+
runWorker(WorkerPoolHashWorker);
247+
242248
```
243249

244250
Note here that the worker class `implements DoWorkUnit<File, string>`. This is different to before where we implemented
@@ -254,11 +260,12 @@ from the `workUnit` method.
254260
```ts
255261
// src/app/doc/async-work.worker.ts#L7-L14
256262

257-
@ObservableWorker()
258263
export class FactorizationWorker implements DoWorkUnit<number, number[]> {
259264
public async workUnit(input: number): Promise<number[]> {
260265
return factorize(input);
261266
}
262267
}
263268

264-
```
269+
runWorker(FactorizationWorker);
270+
271+
```

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Karma configuration file, see link for more information
22
// https://karma-runner.github.io/1.0/config/configuration-file.html
33

4-
module.exports = function(config) {
4+
module.exports = function (config) {
55
config.set({
66
basePath: '',
77
frameworks: ['jasmine', '@angular-devkit/build-angular'],

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"@types/google.visualization": "0.0.48",
6363
"@types/jasmine": "~3.3.8",
6464
"@types/jasminewd2": "~2.0.3",
65+
"@types/js-md5": "^0.4.2",
6566
"@types/node": "^12.11.1",
6667
"codelyzer": "^6.0.0",
6768
"commitizen": "4.0.3",
@@ -76,7 +77,7 @@
7677
"karma-jasmine": "~4.0.0",
7778
"karma-jasmine-html-reporter": "^1.5.0",
7879
"ng-packagr": "^10.1.0",
79-
"prettier": "1.18.2",
80+
"prettier": "2.2.1",
8081
"semantic-release": "^17.2.3",
8182
"ts-node": "~7.0.0",
8283
"tslint": "~6.1.0",

projects/observable-webworker/karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Karma configuration file, see link for more information
22
// https://karma-runner.github.io/1.0/config/configuration-file.html
33

4-
module.exports = function(config) {
4+
module.exports = function (config) {
55
config.set({
66
basePath: '',
77
frameworks: ['jasmine', '@angular-devkit/build-angular'],

projects/observable-webworker/src/lib/from-worker-pool.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fakeAsync, tick } from '@angular/core/testing';
2-
import { Observable, Observer, Subject, Subscription } from 'rxjs';
2+
import { Observable, Subject, Subscription } from 'rxjs';
33
import { Notification } from 'rxjs/internal/Notification';
44
import { reduce } from 'rxjs/operators';
55
import { fromWorkerPool } from './from-worker-pool';
@@ -285,14 +285,14 @@ describe('fromWorkerPool', () => {
285285

286286
const operatorSpy = jasmine.createSpy('subscriptionSpy');
287287

288-
function customOperator<T extends number>(outerObservable$: Observable<Observable<T>>): Observable<T> {
289-
return new Observable<T>((observer: Observer<number>) => {
288+
function customOperator(outerObservable$: Observable<Observable<number>>): Observable<number> {
289+
return new Observable<number>(subscriber => {
290290
const innerSubs: Subscription[] = [];
291291

292292
const outerSub = outerObservable$.subscribe(innerObservable$ => {
293293
innerSubs.push(
294294
innerObservable$.subscribe(value => {
295-
observer.next(value * 2);
295+
subscriber.next(value * 2);
296296
operatorSpy();
297297
}),
298298
);

projects/observable-webworker/src/lib/from-worker.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('fromWorker', () => {
9090
const subscriptionErrorSpy = jasmine.createSpy('subscriptionErrorSpy');
9191
const sub = stubbedWorkerStream.subscribe({ error: subscriptionErrorSpy });
9292

93-
stubWorker.onmessage(
93+
stubWorker.onmessage!(
9494
new MessageEvent('message', {
9595
data: new Notification('E', undefined, 'Nope!'),
9696
}),
@@ -169,13 +169,13 @@ describe('fromWorker', () => {
169169
testValue.buffer,
170170
]);
171171

172-
stubWorker.onmessage(
172+
stubWorker.onmessage!(
173173
new MessageEvent('message', {
174174
data: new Notification('N', 1),
175175
}),
176176
);
177177

178-
stubWorker.onmessage(
178+
stubWorker.onmessage!(
179179
new MessageEvent('message', {
180180
data: new Notification('C'),
181181
}),

projects/observable-webworker/src/lib/from-worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function fromWorker<Input, Output>(
2626
materialize(),
2727
tap(input => {
2828
if (selectTransferables && input.hasValue) {
29-
const transferables = selectTransferables(input.value);
29+
const transferables = selectTransferables(input.value as Input);
3030
worker.postMessage(input, transferables);
3131
} else {
3232
worker.postMessage(input);

projects/observable-webworker/src/lib/observable-worker.decorator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { take } from 'rxjs/operators';
33
import { ObservableWorker } from './observable-worker.decorator';
44
import { DoWork, WorkerMessageNotification } from './observable-worker.types';
55

6-
describe('@ObservableWorker', () => {
6+
describe('@ObservableWorker (deprecated, remove in next major version)', () => {
77
it('should automatically run the worker', () => {
88
const postMessageSpy = spyOn(window, 'postMessage');
99
postMessageSpy.calls.reset();

projects/observable-webworker/src/lib/observable-worker.decorator.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { ObservableWorkerConstructor, runWorker } from './run-worker';
22

3+
/**
4+
* @deprecated - use the `runWorker(YourWorkerClass)` strategy instead, for
5+
* compatibility with future webpack versions, and a slightly smaller bundle
6+
* @see https://github.com/cloudnc/observable-webworker#decorator-deprecation-notice
7+
*/
38
export function ObservableWorker() {
49
return <I, O>(workerConstructor: ObservableWorkerConstructor<I, O>): void => {
510
runWorker(workerConstructor);

0 commit comments

Comments
 (0)