Skip to content
This repository has been archived by the owner on May 29, 2023. It is now read-only.

Commit

Permalink
chore: use Subject instead of an array of observers
Browse files Browse the repository at this point in the history
  • Loading branch information
IKatsuba committed Nov 23, 2020
1 parent b20aad5 commit e1e7d0a
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions projects/workers/src/worker/classes/web-worker.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {EMPTY, fromEvent, merge, Observable, Observer} from 'rxjs';
import {take, tap} from 'rxjs/operators';
import {EMPTY, fromEvent, merge, Observable, Subject} from 'rxjs';
import {take, takeUntil, tap} from 'rxjs/operators';
import {WORKER_BLANK_FN} from '../consts/worker-fn-template';
import {TypedMessageEvent} from '../types/typed-message-event';
import {WorkerFunction} from '../types/worker-function';

export class WebWorker<T = any, R = any> extends Observable<TypedMessageEvent<R>> {
private readonly worker: Worker | undefined;
private readonly url: string;
private isStopped: boolean;
private observers: Observer<TypedMessageEvent<R>>[];
private readonly destroy$: Subject<void>;

constructor(url: string, options?: WorkerOptions) {
let worker: Worker | undefined;
Expand All @@ -25,7 +24,7 @@ export class WebWorker<T = any, R = any> extends Observable<TypedMessageEvent<R>

if (error) {
subscriber.error(error);
} else if (this.isStopped) {
} else if (this.destroy$.isStopped) {
subscriber.complete();
} else if (worker) {
eventStream$ = merge(
Expand All @@ -35,19 +34,15 @@ export class WebWorker<T = any, R = any> extends Observable<TypedMessageEvent<R>
fromEvent<ErrorEvent>(worker, 'error').pipe(
tap(event => subscriber.error(event)),
),
);

this.observers.push(subscriber);
).pipe(takeUntil(this.destroy$));
}

return eventStream$.subscribe();
eventStream$.subscribe().add(subscriber);
});

this.worker = worker;
this.url = url;

this.isStopped = false;
this.observers = [];
this.destroy$ = new Subject<void>();
}

static fromFunction<T, R>(
Expand Down Expand Up @@ -82,7 +77,7 @@ export class WebWorker<T = any, R = any> extends Observable<TypedMessageEvent<R>
}

terminate() {
if (this.isStopped) {
if (this.destroy$.isStopped) {
return;
}

Expand All @@ -92,13 +87,8 @@ export class WebWorker<T = any, R = any> extends Observable<TypedMessageEvent<R>

URL.revokeObjectURL(this.url);

this.isStopped = true;
this.observers.forEach(observer => {
if (!observer.closed) {
observer.complete();
}
});
this.observers = [];
this.destroy$.next();
this.destroy$.complete();
}

postMessage(value: T) {
Expand Down

0 comments on commit e1e7d0a

Please sign in to comment.