import { OnDestroy , Pipe , PipeTransform } from '@angular/core' ; import { Observable } from 'rxjs' ; import { WebWorker } from '../classes/web-worker' ; import { toData } from '../operators/to-data' ; import { WorkerFunction } from '../types/worker-function' ; @ Pipe ( { name : 'waWorker' , } ) export class WorkerPipe implements PipeTransform , OnDestroy { private fn ! : WorkerFunction ; private worker ! : WebWorker ; private observer ! : Observable < any > ; transform < T , R > ( value , fn ) { if ( this . fn !== fn ) { this . terminateWorker ( ) ; this . initNewWorker ( fn ) ; } this . worker . postMessage ( value ) ; return this . observer ; } ngOnDestroy ( ) { this . terminateWorker ( ) ; } private terminateWorker ( ) { if ( this . worker ) { this . worker . terminate ( ) ; } } private initNewWorker < T , R > ( fn ) { this . fn = fn ; this . worker = WebWorker . fromFunction ( fn ) ; this . observer = this . worker . pipe ( toData ( ) ) ; } }