Documentation
¶
Overview ¶
Package worker provides a Web Workers driver for Go code compiled to WebAssembly.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type GlobalSelf ¶
type GlobalSelf struct {
// contains filtered or unexported fields
}
GlobalSelf represents the global scope, named "self", in the context of using Workers. Supports sending and receiving messages via PostMessage() and Listen().
func (*GlobalSelf) Close ¶
func (s *GlobalSelf) Close() error
Close discards any tasks queued in the global scope's event loop, effectively closing this particular scope.
func (*GlobalSelf) Listen ¶
func (s *GlobalSelf) Listen(ctx context.Context) (<-chan MessageEvent, error)
Listen sends message events on a channel for events fired by worker.postMessage() calls inside the main thread's global scope. Stops the listener and closes the channel when ctx is canceled.
func (*GlobalSelf) Name ¶
func (s *GlobalSelf) Name() (string, error)
Name returns the name that the Worker was (optionally) given when it was created.
func (*GlobalSelf) PostMessage ¶
PostMessage sends data in a message to the main thread that spawned it, optionally transferring ownership of all items in transfers.
The data may be any value handled by the "structured clone algorithm", which includes cyclical references.
Transfers is an optional array of Transferable objects to transfer ownership of. 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. Transferable objects are instances of classes like ArrayBuffer, MessagePort or ImageBitmap objects that can be transferred. null is not an acceptable value for transfer.
type MessageEvent ¶
type MessageEvent struct {
// contains filtered or unexported fields
}
MessageEvent is received from the channel returned by Listen(). Represents a JS MessageEvent.
type Options ¶
type Options struct { // Name specifies an identifying name for the DedicatedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes. Name string }
Options contains optional configuration for new Workers
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker is a Web Worker, which represents a background task created via a script. Use Listen() and PostMessage() to communicate with the worker.
func NewFromScript ¶
NewFromScript is like New, but starts the worker with the given script (in JavaScript)
func (*Worker) Listen ¶
func (w *Worker) Listen(ctx context.Context) (<-chan MessageEvent, error)
Listen sends message events on a channel for events fired by self.postMessage() calls inside the Worker's global scope. Stops the listener and closes the channel when ctx is canceled.
func (*Worker) PostMessage ¶
PostMessage sends data in a message to the worker, optionally transferring ownership of all items in transfers.
The data may be any value handled by the "structured clone algorithm", which includes cyclical references.
Transfers is an optional array of Transferable objects to transfer ownership of. 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. Transferable objects are instances of classes like ArrayBuffer, MessagePort or ImageBitmap objects that can be transferred. null is not an acceptable value for transfer.