-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
136 lines (120 loc) · 2.73 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
browser,
noop,
on_destroy,
toReadable,
toWritable,
unstore,
} from "@sveu/shared"
import { WebWorkerReturn, WorkerFn } from "../utils"
/**
* Simple Web Workers registration and communication.
*
* @param url - URL of the worker
*
* @param options - [WorkerOptions](https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker)
*
* @example
* ```ts
* const { data, error, wk, post, cleanup } = worker("worker.js")
* ```
*
* @example
* ```ts
* const { data, error, wk, post, cleanup } = worker(() => new Worker("worker.js"))
* ```
*
* @example
* ```ts
* const { data, error, wk, post, cleanup } = worker(new Worker("worker.js"))
* ```
*
* @returns
* - `data` - Data from the worker
* - `error` - Error from the worker
* - `wk` - Worker instance
* - `post` - Function to send data to the worker
* - `cleanup` - Function to terminate the worker
*/
export function worker<T>(
url: string,
options?: WorkerOptions
): WebWorkerReturn<T>
/**
* Simple Web Workers registration and communication.
*
* @param worker - Worker function or Worker instance
*
* @example
* ```ts
* const { data, error, wk, post, cleanup } = worker("worker.js")
* ```
*
* @example
* ```ts
* const { data, error, wk, post, cleanup } = worker(() => new Worker("worker.js"))
* ```
*
* @example
* ```ts
* const { data, error, wk, post, cleanup } = worker(new Worker("worker.js"))
* ```
*
* @returns
* - `data` - Data from the worker
* - `error` - Error from the worker
* - `wk` - Worker instance
* - `post` - Function to send data to the worker
* - `cleanup` - Function to terminate the worker
*/
export function worker<T>(worker: Worker | WorkerFn): WebWorkerReturn<T>
export function worker<T>(
arg: string | WorkerFn | Worker,
options?: WorkerOptions
): WebWorkerReturn<T> {
let unsubscribe: () => void = noop
const data = toWritable<any>(null)
const error = toWritable<any>(null)
const wk = toWritable<Worker | undefined>(undefined)
if (browser) {
if (typeof arg === "string") wk.set(new Worker(arg, options))
else if (typeof arg === "function") wk.set(arg())
else wk.set(arg)
unsubscribe = wk.subscribe((_worker) => {
if (!_worker) return
_worker.onmessage = (e: MessageEvent) => {
data.set(e.data)
}
_worker.onerror = (e: ErrorEvent) => {
error.set(e.error)
}
})
}
/**
* Send data to the worker
*
* @param value - Data to send
*
* @example
* ```ts
* post("Hello World!")
* ```
*/
function post<Y>(value: Y) {
const _wk = unstore(wk)
if (!_wk) return
_wk.postMessage(value)
}
function cleanup() {
unstore(wk)?.terminate()
unsubscribe()
}
on_destroy(cleanup)
return {
data: toReadable(data),
error: toReadable(error),
wk: toReadable(wk),
post,
cleanup,
}
}