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

Commit fb2c592

Browse files
committed
chore: refactor service
1 parent 3dfd75e commit fb2c592

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

projects/demo/src/app/app.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class AppComponent {
1111
public workerThread: WebWorker<string, string>;
1212

1313
constructor(webWorkerExecutor: WebWorkerExecutor) {
14-
this.workerThread = webWorkerExecutor.execute((result: string) =>
14+
this.workerThread = webWorkerExecutor.createWorker((result: string) =>
1515
Promise.resolve(`Message from worker: ${result}`),
1616
);
1717
}

projects/workers/src/web-worker/services/web-worker-executor.service.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@ import {WebWorkerModule} from '../web-worker.module';
1212
},
1313
})
1414
export class WebWorkerExecutor {
15-
execute<T, R>(fn: WebWorkerFunction<T, R>, options?: WorkerOptions): WebWorker<T, R> {
15+
execute<T, R>(fn: WebWorkerFunction<T, R>, data: T): Promise<R> {
16+
const worker = this.createWorker(fn);
17+
const promise = worker.toPromise();
18+
19+
worker.next(data);
20+
21+
return promise;
22+
}
23+
24+
createWorker<T, R>(
25+
fn: WebWorkerFunction<T, R>,
26+
options?: WorkerOptions,
27+
): WebWorker<T, R> {
1628
return WebWorker.fromFunction<T, R>(fn, options);
1729
}
1830
}

0 commit comments

Comments
 (0)