Worker

Simple Web Workers registration and communication.

Usage

<script>
    import { worker } from "@sveu/browser"

    const { data, post, error, cleanup } = worker("/path/to/worker.js")
</script>
<script>
    import { worker } from "@sveu/browser"

    const { data, post, error, cleanup } = worker("/path/to/worker.js")
</script>
svelte

Example

Data: null


<script>
    import { worker } from "@sveu/browser"

    let value = "svelte"

    const { data, post, error, cleanup } = worker("/worker.js")

    $: console.log($error)
</script>

<div class="flex flex-col space-y-9 mx-9 mt-9 text-center">
    <h1>Data: {$data}</h1>
    <input
        type="text"
        id="message"
        bind:value="{value}" />

    <button on:click="{() => post(value)}">Send</button>
    <br />
    <button on:click="{() => cleanup()}">Stop</button>
</div>
<script>
    import { worker } from "@sveu/browser"

    let value = "svelte"

    const { data, post, error, cleanup } = worker("/worker.js")

    $: console.log($error)
</script>

<div class="flex flex-col space-y-9 mx-9 mt-9 text-center">
    <h1>Data: {$data}</h1>
    <input
        type="text"
        id="message"
        bind:value="{value}" />

    <button on:click="{() => post(value)}">Send</button>
    <br />
    <button on:click="{() => cleanup()}">Stop</button>
</div>
svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Click fold/expand code

API

Arguments

Name Description Type Required
url Worker path url string No(if worker is provided )
worker Worker function or Worker instance Worker or WorkerFn No(if url is provided )


Options

See Worker Options



Returns

Name Description Type
data Data from the worker Readable<any>
error Error from the worker Readable<any>
wk Worker instance Readable<Worker>
cleanup Function to terminate the worker () => void
post Function to send data to the worker (message: any, transfer: Transferable[]): void or (message: any, options?: StructuredSerializeOptions): void