jsbridge

package
v1.15.5 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Rendered for js/wasm

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMismatchedInputLength the length of input are mismatched
	ErrMismatchedInputLength = errors.New("binder: mismatched input length")

	// ErrMismatchedOutputLength the length of output are mismatched
	ErrMismatchedOutputLength = errors.New("binder: mismatched output length")

	// ErrBinderNotImplemented the type binder is not implemented yet
	ErrBinderNotImplemented = errors.New("binder: not impelmented")

	// ErrFuncNotSupported the type function is not supported yet
	ErrFuncNotSupported = errors.New("func: not supported")

	// ErrIsNotFunc bind works with func only
	ErrIsNotFunc = errors.New("func: bind works with func only")
)
View Source
var (
	TypeFunc   = reflect.TypeOf(func() {}).String()
	TypeError  = "error"
	TypeString = reflect.TypeOf("string").String()
	TypeBytes  = reflect.TypeOf([]byte{}).String()
)
View Source
var WorkerJSTpl []byte

Functions

func Await

func Await(awaitable js.Value) ([]js.Value, []js.Value)

This function try to execute wasm functions that are wrapped with "Promise" see: https://stackoverflow.com/questions/68426700/how-to-wait-a-js-async-function-from-golang-wasm/68427221#comment120939975_68427221

func BindAsyncFuncs added in v1.3.5

func BindAsyncFuncs(global js.Value, fnList map[string]interface{})

func BindFunc

func BindFunc(global js.Value, jsFuncName string, fn interface{}) error

BindFunc bind go func to js func in global only support - func(...) - func(...) error - func(...) T - func(...) (T,error)

func BindFuncs

func BindFuncs(global js.Value, fnList map[string]interface{})

func Close

func Close()

func GetSelfWorker added in v1.15.4

func GetSelfWorker() *worker.GlobalSelf

func NewArray added in v1.3.5

func NewArray(items ...interface{}) js.Value

func NewBytes added in v1.3.6

func NewBytes(buf []byte) js.Value

func NewJsError

func NewJsError(message interface{}) js.Value

func NewObject added in v1.3.5

func NewObject(obj interface{}) js.Value

func NewSelfWorker added in v1.15.4

func NewSelfWorker() (*worker.GlobalSelf, error)

func RemoveWorker added in v1.15.4

func RemoveWorker(blobberID string)

Types

type AsyncInvoker

type AsyncInvoker func(resolve, reject js.Value, fn reflect.Value, in []reflect.Value, err error)

func Async

func Async(funcType reflect.Type) (AsyncInvoker, error)

type Bytes added in v1.8.13

type Bytes struct {
	Buffer []byte
}

func (*Bytes) UnmarshalJSON added in v1.8.13

func (bytes *Bytes) UnmarshalJSON(buf []byte) error

type FileReader added in v1.8.12

type FileReader struct {
	// contains filtered or unexported fields
}

func NewFileReader added in v1.8.12

func NewFileReader(readChunkFuncName string, fileSize, chunkReadSize int64) (*FileReader, error)

func (*FileReader) Read added in v1.8.12

func (r *FileReader) Read(p []byte) (int, error)

func (*FileReader) Seek added in v1.8.12

func (r *FileReader) Seek(offset int64, whence int) (int64, error)

type FileWriter added in v1.11.4

type FileWriter struct {
	// contains filtered or unexported fields
}

func NewFileWriter added in v1.11.4

func NewFileWriter(filename string) (*FileWriter, error)

func (*FileWriter) Close added in v1.11.4

func (w *FileWriter) Close() error

func (*FileWriter) Read added in v1.11.4

func (w *FileWriter) Read(p []byte) (int, error)

func (*FileWriter) Seek added in v1.11.4

func (w *FileWriter) Seek(offset int64, whence int) (int64, error)

func (*FileWriter) Stat added in v1.11.4

func (w *FileWriter) Stat() (fs.FileInfo, error)

func (*FileWriter) Sync added in v1.11.4

func (w *FileWriter) Sync() error

func (*FileWriter) Write added in v1.11.4

func (w *FileWriter) Write(p []byte) (int, error)

len(p) will always be <= 64KB

type InputBinder

type InputBinder func([]js.Value) ([]reflect.Value, error)

InputBinder convert inputs from js.Value to reflect.Value

type InputBuilder

type InputBuilder struct {
	IsVariadic bool
	// contains filtered or unexported fields
}

InputBuilder binder builder

func NewInputBuilder

func NewInputBuilder(fn reflect.Type) *InputBuilder

NewInputBuilder create InputBuilder

func (*InputBuilder) Bind

func (b *InputBuilder) Bind(args []js.Value) ([]reflect.Value, error)

Bind bind js inputs to reflect values

func (*InputBuilder) Build

func (b *InputBuilder) Build() (InputBinder, error)

Build build InputBinder js.ValueOf returns x as a JavaScript value:

| Go                     | JavaScript             |
| ---------------------- | ---------------------- |
| js.Value               | [its value]            |
| js.Func                | function               |
| nil                    | null                   |
| bool                   | boolean                |
| integers and floats    | number                 |
| string                 | string                 |
| []interface{}          | new array              |
| map[string]interface{} | new object             |

Panics if x is not one of the expected types.

type OutputBinder

type OutputBinder func([]reflect.Value) []js.Value

OutputBinder convert Outputs from js.Value to reflect.Value

type OutputBuilder

type OutputBuilder struct {
	// contains filtered or unexported fields
}

OutputBuilder binder builder

func NewOutputBuilder

func NewOutputBuilder(fn reflect.Type) *OutputBuilder

NewOutputBuilder create OutputBuilder

func (*OutputBuilder) Bind

func (b *OutputBuilder) Bind(args []reflect.Value) []js.Value

Bind bind js Outputs to reflect values

func (*OutputBuilder) Build

func (b *OutputBuilder) Build() (OutputBinder, error)

Build build OutputBinder js.ValueOf returns x as a JavaScript value:

| Go                     | JavaScript             |
| ---------------------- | ---------------------- |
| js.Value               | [its value]            |
| js.Func                | function               |
| nil                    | null                   |
| bool                   | boolean                |
| integers and floats    | number                 |
| string                 | string                 |
| []interface{}          | new array              |
| map[string]interface{} | new object             |

Panics if x is not one of the expected types.

type SyncInvoker added in v1.3.5

type SyncInvoker func(fn reflect.Value, in []reflect.Value) js.Value

func Sync added in v1.3.5

func Sync(funcType reflect.Type) (SyncInvoker, error)

type Timer added in v1.8.8

type Timer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewTimer added in v1.8.8

func NewTimer(interval time.Duration, callback func()) *Timer

func (*Timer) Start added in v1.8.8

func (t *Timer) Start()

func (*Timer) Stop added in v1.8.8

func (t *Timer) Stop()

type WasmWebWorker added in v1.15.4

type WasmWebWorker struct {
	// Name specifies an identifying name for the DedicatedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes.
	// If this is not specified, `Start` will create a UUIDv4 for it and populate back.
	Name string

	// Path is the path of the WASM to run as the Web Worker.
	// This can be a relative path on the server, or an abosolute URL.
	Path string

	// Args holds command line arguments, including the WASM as Args[0].
	// If the Args field is empty or nil, Run uses {Path}.
	Args []string

	// Env specifies the environment of the process.
	// Each entry is of the form "key=value".
	// If Env is nil, the new Web Worker uses the current context's
	// environment.
	// If Env contains duplicate environment keys, only the last
	// value in the slice for each duplicate key is used.
	Env []string
	// contains filtered or unexported fields
}

func GetWorker added in v1.15.4

func GetWorker(blobberID string) *WasmWebWorker

func NewWasmWebWorker added in v1.15.4

func NewWasmWebWorker(blobberID, blobberURL, clientID, publicKey, privateKey, mnemonic string) (*WasmWebWorker, error)

func (*WasmWebWorker) Listen added in v1.15.4

func (ww *WasmWebWorker) Listen(ctx context.Context) (<-chan worker.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 (*WasmWebWorker) PostMessage added in v1.15.4

func (ww *WasmWebWorker) 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.

func (*WasmWebWorker) Start added in v1.15.4

func (ww *WasmWebWorker) Start() error

func (*WasmWebWorker) Terminate added in v1.15.4

func (ww *WasmWebWorker) Terminate()

Terminate immediately terminates the Worker.

Jump to

Keyboard shortcuts

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