worker

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2023 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Rendered for js/wasm

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 Self

func Self() (*GlobalSelf, error)

Self returns the global "self"

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

func (s *GlobalSelf) PostMessage(message safejs.Value, transfers []safejs.Value) error

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.

func (MessageEvent) Data

func (e MessageEvent) Data() (safejs.Value, error)

Data returns this event's data or a parse error

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 New

func New(url string, options Options) (*Worker, error)

New starts a worker with the given script's URL and returns it

func NewFromScript

func NewFromScript(jsScript string, options Options) (*Worker, error)

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

func (w *Worker) PostMessage(data safejs.Value, transfers []safejs.Value) error

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.

func (*Worker) Terminate

func (w *Worker) Terminate() error

Terminate immediately terminates the Worker. This does not offer the worker an opportunity to finish its operations; it is stopped at once.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL