-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56067d9
commit f657619
Showing
2 changed files
with
19 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
import { implementWorker } from "kiss-worker"; | ||
import { implementWorkerExternal } from "kiss-worker"; | ||
|
||
// The function we want to execute on a worker thread (worker function). | ||
const getFibonacci = (n: number): number => | ||
((n < 2) ? Math.floor(n) : getFibonacci(n - 1) + getFibonacci(n - 2)); | ||
// Import the type of the worker function ... | ||
import type { GetFibonacci } from "./getFibonacci.js"; | ||
|
||
export const FibonacciWorker = implementWorker( | ||
// A function that creates a web worker running this script | ||
// ... and pass it to establish type safety | ||
export const FibonacciWorker = implementWorkerExternal<GetFibonacci>( | ||
// A function that creates a web worker running the script that serves | ||
// the worker function | ||
() => new Worker( | ||
new URL("FibonacciWorker.js", import.meta.url), | ||
new URL("getFibonacci.js", import.meta.url), | ||
{ type: "module" } | ||
), | ||
// Our worker function | ||
getFibonacci, | ||
); |
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 @@ | ||
import { serve } from "kiss-worker"; | ||
|
||
// The function we want to execute on a worker thread (worker function) | ||
const getFibonacci = (n: number): number => | ||
((n < 2) ? Math.floor(n) : getFibonacci(n - 1) + getFibonacci(n - 2)); | ||
|
||
// Serve the function, so that it can be called from another thread | ||
serve(getFibonacci); | ||
|
||
// Export the type only | ||
export type GetFibonacci = typeof getFibonacci; |