jsutil

package
v0.0.28 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Rendered for js/wasm

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExpandArgs

func ExpandArgs(args []js.Value, target ...*js.Value)

ExpandArgs unpacks function arguments to target values.

func FromJSON

func FromJSON(s string) js.Value

FromJSON converts the supplied JSON string to a Javascript value.

func Log

func Log(format string, objs ...interface{})

Log logs general information to the Javascript Console.

func LogDebug

func LogDebug(format string, objs ...interface{})

LogDebug logs a debug message to the Javascript Console.

func LogError

func LogError(format string, objs ...interface{})

LogError logs an error to the Javascript Console.

func NewObject

func NewObject() js.Value

NewObject returns a new 'Object' instance.

func ObjectKeys

func ObjectKeys(val js.Value) ([]string, error)

ObjectKeys returns the keys for a given object.

func OneTimeFuncOf

func OneTimeFuncOf(f func(this js.Value, args []js.Value) interface{}) js.Func

OneTimeFuncOf returns a js.Func that can be invoked once. It is automatically released when invoked.

func SetTimeout

func SetTimeout(timeout time.Duration, callback func())

SetTimeout registers a callback to be invoked when the timeout has expired.

func SingleArg

func SingleArg(args []js.Value) js.Value

SingleArg unpacks a single function argument and returns it.

func ToJSON

func ToJSON(val js.Value) string

ToJSON converts the supplied value to a JSON string.

Types

type AsyncContext

type AsyncContext interface {
	// AFunc is a dummy method to avoid arbitrary types from satisfying this
	// interface.
	AFunc()
}

AsyncContext is a type of context passed to a function executing asynchronously.

type CleanupFunc

type CleanupFunc func()

CleanupFunc is a function to be invoked to cleanup resources.

func DefineAsyncFunc

func DefineAsyncFunc(o js.Value, name string, f func(ctx AsyncContext, this js.Value, args []js.Value) (js.Value, error)) CleanupFunc

DefineAsyncFunc defines a new async function and attaches it to the specified object. The defined function will return a promise that is resolved or rejected when it completes. The returned cleanup function must be invoked to detach the function and release it.

func DefineFunc

func DefineFunc(o js.Value, name string, f func(this js.Value, args []js.Value) interface{}) CleanupFunc

DefineFunc defines a new function and attaches it to the specified object. The returned cleanup function must be invoked to detach the function and release it.

type CleanupFuncs

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

CleanupFuncs is a set of cleanup functions.

func (*CleanupFuncs) Add

func (c *CleanupFuncs) Add(f CleanupFunc)

Add adds a cleanup func.

func (*CleanupFuncs) Do

func (c *CleanupFuncs) Do()

Do invokes all of the cleanup functions.

type JSError

type JSError struct {
	js.Value
}

JSError represents javascript's Error type.

func NewError

func NewError(err error) JSError

NewError returns a Javascript error corresponding to a Go error.

func NewErrorFromVal

func NewErrorFromVal(val js.Value) JSError

NewErrorFromVal returns a Javascript error corresponding to an arbitrary value. We do our best to preserve the original value, but we do assume that it reflects an error and is not some arbitrary value.

func (JSError) AsJSValue

func (e JSError) AsJSValue() js.Value

AsJSValue returns the error as a Javascript Value.

func (JSError) Error

func (e JSError) Error() string

Error implements Go's error interface.

func (JSError) Message

func (e JSError) Message() string

Message returns the Error's message.

func (JSError) Name

func (e JSError) Name() string

Name returns the Error's name.

type Promise

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

Promise encapsulates a javascript Promise type.

func AsPromise

func AsPromise(v js.Value) *Promise

AsPromise returns a Promise for javascript value. The value is assumed to be of the Promise type.

func Async

func Async(f func(ctx AsyncContext) (js.Value, error)) *Promise

Async executes a function asynchronously. A promise corresponding to the function is returned.

func NewPromise

func NewPromise(f func(ctx AsyncContext, resolve ResolveFunc, reject RejectFunc)) *Promise

NewPromise runs a function in the background. The function must invoke resolve or reject when complete. The function is passed an AsyncContext which can then be used to invoke Await() on promises that it subsequently creates.

func (*Promise) Await

func (p *Promise) Await(ctx AsyncContext) (js.Value, error)

Await blocks until a Promise is either resolved or rejected. It must only be invoked from within an AsyncContext.

func (*Promise) JSValue

func (p *Promise) JSValue() js.Value

JSValue returns the javascript value representing the promise.

func (*Promise) Then

func (p *Promise) Then(resolve ResolveFunc, reject RejectFunc) *Promise

Then implements Promise.then(). Resolve is invoked when the promise is resolved, and reject is invoked when it is rejected.

type RejectFunc

type RejectFunc func(err error)

RejectFunc is a function type that can be used to reject a promise.

type ResolveFunc

type ResolveFunc func(value js.Value)

ResolveFunc is a function type that can be used to resolve a promise.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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