|
1 | 1 | //go:build js && wasm |
2 | | -// +build js,wasm |
3 | 2 |
|
4 | 3 | // Package worker provides a Web Workers driver for Go code compiled to WebAssembly. |
5 | 4 | package worker |
6 | 5 |
|
7 | | -import "errors" |
| 6 | +import ( |
| 7 | + "context" |
8 | 8 |
|
9 | | -// Worker is a Web Worker, which represents a background task that can be created via script. |
10 | | -// Workers can send messages back to its creator. |
11 | | -type Worker struct{} |
| 9 | + "github.com/hack-pad/safejs" |
| 10 | +) |
12 | 11 |
|
13 | | -// NewWorker returns a new Worker |
14 | | -func NewWorker() (*Worker, error) { |
15 | | - return nil, errors.New("not implemented") |
| 12 | +var ( |
| 13 | + jsWorker = safejs.MustGetGlobal("Worker") |
| 14 | + jsURL = safejs.MustGetGlobal("URL") |
| 15 | + jsBlob = safejs.MustGetGlobal("Blob") |
| 16 | +) |
| 17 | + |
| 18 | +// Worker is a Web Worker, which represents a background task created via a script. |
| 19 | +// Use Listen() and PostMessage() to communicate with the worker. |
| 20 | +type Worker struct { |
| 21 | + worker safejs.Value |
| 22 | + port *messagePort |
| 23 | +} |
| 24 | + |
| 25 | +// Options contains optional configuration for new Workers |
| 26 | +type Options struct { |
| 27 | + // Name specifies an identifying name for the DedicatedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes. |
| 28 | + Name string |
| 29 | +} |
| 30 | + |
| 31 | +func (w Options) toJSValue() (safejs.Value, error) { |
| 32 | + options := make(map[string]any) |
| 33 | + if w.Name != "" { |
| 34 | + options["name"] = w.Name |
| 35 | + } |
| 36 | + return safejs.ValueOf(options) |
| 37 | +} |
| 38 | + |
| 39 | +// New starts a worker with the given script's URL and returns it |
| 40 | +func New(url string, options Options) (*Worker, error) { |
| 41 | + jsOptions, err := options.toJSValue() |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + worker, err := jsWorker.New(url, jsOptions) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + port, err := wrapMessagePort(worker) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + return &Worker{ |
| 54 | + port: port, |
| 55 | + worker: worker, |
| 56 | + }, nil |
| 57 | +} |
| 58 | + |
| 59 | +// NewFromScript is like New, but starts the worker with the given script (in JavaScript) |
| 60 | +func NewFromScript(jsScript string, options Options) (*Worker, error) { |
| 61 | + blob, err := jsBlob.New([]any{jsScript}, map[string]any{ |
| 62 | + "type": "text/javascript", |
| 63 | + }) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + objectURL, err := jsURL.Call("createObjectURL", blob) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + objectURLStr, err := objectURL.String() |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + return New(objectURLStr, options) |
| 76 | +} |
| 77 | + |
| 78 | +// Terminate immediately terminates the Worker. |
| 79 | +// This does not offer the worker an opportunity to finish its operations; it is stopped at once. |
| 80 | +func (w *Worker) Terminate() error { |
| 81 | + _, err := w.worker.Call("terminate") |
| 82 | + return err |
| 83 | +} |
| 84 | + |
| 85 | +// PostMessage sends data in a message to the worker, optionally transferring ownership of all items in transfers. |
| 86 | +// |
| 87 | +// The data may be any value handled by the "structured clone algorithm", which includes cyclical references. |
| 88 | +// |
| 89 | +// Transfers is an optional array of Transferable objects to transfer ownership of. |
| 90 | +// If the ownership of an object is transferred, it becomes unusable in the context it was sent from and becomes available only to the worker it was sent to. |
| 91 | +// Transferable objects are instances of classes like ArrayBuffer, MessagePort or ImageBitmap objects that can be transferred. |
| 92 | +// null is not an acceptable value for transfer. |
| 93 | +func (w *Worker) PostMessage(data safejs.Value, transfers []safejs.Value) error { |
| 94 | + return w.port.PostMessage(data, transfers) |
| 95 | +} |
| 96 | + |
| 97 | +// Listen sends message events on a channel for events fired by self.postMessage() calls inside the Worker's global scope. |
| 98 | +// Stops the listener and closes the channel when ctx is canceled. |
| 99 | +func (w *Worker) Listen(ctx context.Context) (<-chan MessageEvent, error) { |
| 100 | + return w.port.Listen(ctx) |
16 | 101 | } |
0 commit comments