js

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package js provides additional functionality on top of syscall/js package for WASM.

Index

Constants

View Source
const (
	TypeObject   = js.TypeObject
	TypeFunction = js.TypeFunction
)

Variables

This section is empty.

Functions

func NewError

func NewError(e Wrapper) error

NewError creates a new Go error from JS error value.

func Set

func Set(name string, v interface{})

Set is a shorthand for Global().Set().

Types

type Arr

type Arr = []interface{}

Arr is an alias for []interface{}.

type Callback deprecated

type Callback = Func

Callback is a wrapped Go function to be called by JavaScript.

Deprecated: use Func

type CallbackGroup deprecated

type CallbackGroup = FuncGroup

CallbackGroup is a list of Go functions attached to an object.

Deprecated: use FuncGroup

type Error

type Error = js.Error

Error is an alias for syscall/js.Error.

type Func

type Func = js.Func

Func is a wrapped Go function to be called by JavaScript.

func AsyncCallbackOf

func AsyncCallbackOf(fnc func(v []Value)) Func

AsyncCallbackOf returns a wrapped callback function.

Invoking the callback in JavaScript will queue the Go function fn for execution. This execution happens asynchronously.

Callback.Release must be called to free up resources when the callback will not be used any more.

func CallbackOf

func CallbackOf(fnc func(v []Value)) Func

CallbackOf returns a wrapped callback function.

Invoking the callback in JavaScript will queue the Go function fn for execution. This execution happens asynchronously on a special goroutine that handles all callbacks and preserves the order in which the callbacks got called. As a consequence, if one callback blocks this goroutine, other callbacks will not be processed. A blocking callback should therefore explicitly start a new goroutine.

Callback.Release must be called to free up resources when the callback will not be used any more.

func FuncOf

func FuncOf(fnc func(this Value, args []Value) interface{}) Func

FuncOf returns a wrapped function.

Invoking the JavaScript function will synchronously call the Go function fn with the value of JavaScript's "this" keyword and the arguments of the invocation. The return value of the invocation is the result of the Go function mapped back to JavaScript according to ValueOf.

A wrapped function triggered during a call from Go to JavaScript gets executed on the same goroutine. A wrapped function triggered by JavaScript's event loop gets executed on an extra goroutine. Blocking operations in the wrapped function will block the event loop. As a consequence, if one wrapped function blocks, other wrapped funcs will not be processed. A blocking function should therefore explicitly start a new goroutine.

Func.Release must be called to free up resources when the function will not be used any more.

func NewCallback deprecated

func NewCallback(fnc func(v []Value)) Func

NewCallback returns a wrapped callback function.

Invoking the callback in JavaScript will queue the Go function fn for execution. This execution happens asynchronously on a special goroutine that handles all callbacks and preserves the order in which the callbacks got called. As a consequence, if one callback blocks this goroutine, other callbacks will not be processed. A blocking callback should therefore explicitly start a new goroutine.

Callback.Release must be called to free up resources when the callback will not be used any more.

Deprecated: use CallbackOf

func NewCallbackAsync deprecated

func NewCallbackAsync(fnc func(v []Value)) Func

NewCallbackAsync returns a wrapped callback function.

Invoking the callback in JavaScript will queue the Go function fn for execution. This execution happens asynchronously.

Callback.Release must be called to free up resources when the callback will not be used any more.

Deprecated: AsyncCallbackOf

func NewEventCallback

func NewEventCallback(fnc func(v Value)) Func

NewEventCallback is a shorthand for NewEventCallbackFlags with default flags.

func NewFunc deprecated

func NewFunc(fnc func(this Value, args []Value) interface{}) Func

NewFunc returns a wrapped function that will be executed synchronously.

Deprecated: use FuncOf

type FuncGroup

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

FuncGroup is a list of Go functions attached to an object.

func (*FuncGroup) Add

func (g *FuncGroup) Add(cb Func)

func (*FuncGroup) AddEventListener

func (g *FuncGroup) AddEventListener(event string, fnc func(Value))

func (*FuncGroup) ErrorEvent

func (g *FuncGroup) ErrorEvent(fnc func(error))

func (*FuncGroup) ErrorEventChan

func (g *FuncGroup) ErrorEventChan() <-chan error

func (*FuncGroup) OneTimeEvent

func (g *FuncGroup) OneTimeEvent(event string, fnc func(Value))

func (*FuncGroup) OneTimeEventChan

func (g *FuncGroup) OneTimeEventChan(event string) <-chan Value

func (*FuncGroup) OneTimeTrigger

func (g *FuncGroup) OneTimeTrigger(event string) <-chan struct{}

func (*FuncGroup) Release

func (g *FuncGroup) Release()

func (*FuncGroup) Set

func (g *FuncGroup) Set(name string, fnc func([]Value))

type JSRef deprecated

type JSRef = Wrapper

JSRef is a common interface for object that are backed by a JS object.

Deprecated: see Wrapper

type Obj

type Obj = map[string]interface{}

Obj is an alias for map[string]interface{}.

type Promise

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

Promise represents a JavaScript Promise.

func (*Promise) Await

func (p *Promise) Await() ([]Value, error)

Await for the promise to resolve.

func (*Promise) AwaitContext

func (p *Promise) AwaitContext(ctx context.Context) ([]Value, error)

AwaitContext for the promise to resolve or context to be canceled.

func (*Promise) JSValue

func (p *Promise) JSValue() Ref

JSValue implements Wrapper interface.

type Ref

type Ref = js.Value

Ref is an alias for syscall/js.Value.

type Type

type Type = js.Type

Type is a type name of a JS value, as returned by "typeof".

type Value

type Value struct {
	Ref
}

Value is a convenience wrapper for syscall/js.Value. It provides some additional functionality, while storing no additional state. Its safe to instantiate Value directly, by wrapping syscall/js.Value.

func Array

func Array() Value

Array returns an Array JS class.

func Call

func Call(name string, args ...interface{}) Value

Call is a shorthand for Global().Call().

func Class

func Class(class string) Value

Class searches for a class in global scope. It caches results, so the lookup should be faster than calling Get.

func Get

func Get(name string, path ...string) Value

Get is a shorthand for Global().Get().

func NativeFuncOf

func NativeFuncOf(argsAndCode ...string) Value

NativeFuncOf creates a function from a JS code string.

Example:

NativeFuncOf("a", "b", "return a+b").Call(a, b)

func New

func New(class string, args ...interface{}) Value

New searches for a class in global scope and creates a new instance of that class.

func NewArray

func NewArray() Value

NewArray creates an empty JS array.

func NewFuncJS deprecated

func NewFuncJS(argsAndCode ...string) Value

NewFuncJS creates a function from a JS code string.

Deprecated: use RawFuncOf

Example:

NativeFuncOf("a", "b", "return a+b").Call(a, b)

func NewObject

func NewObject() Value

NewObject creates an empty JS object.

func NewPromise

func NewPromise(fnc func() ([]interface{}, error)) Value

NewPromise runs a given function asynchronously by converting it to JavaScript promise. Promise will be resolved if functions returns a nil error and will be rejected otherwise.

func Object

func Object() Value

Object returns an Object JS class.

func ValueOf

func ValueOf(o interface{}) Value

ValueOf returns x as a JavaScript value:

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

func (Value) Await

func (v Value) Await() ([]Value, error)

Await wait for the promise to be resolved or rejected. A shorthand for calling Await on the promise returned by Promised.

func (Value) Call

func (v Value) Call(name string, args ...interface{}) Value

Call does a JavaScript call to the method m of value v with the given arguments. It panics if v has no method m. The arguments get mapped to JavaScript values according to the ValueOf function.

func (Value) Get

func (v Value) Get(name string, path ...string) Value

Get returns the JS property by name.

func (Value) Index

func (v Value) Index(i int) Value

Index returns JS index i of value v.

func (Value) InstanceOf

func (v Value) InstanceOf(class Value) bool

InstanceOf reports whether v is an instance of type t according to JavaScript's instanceof operator.

func (Value) InstanceOfClass

func (v Value) InstanceOfClass(class string) bool

InstanceOfClass reports whether v is an instance of named type according to JavaScript's instanceof operator.

func (Value) Invoke

func (v Value) Invoke(args ...interface{}) Value

Invoke does a JavaScript call of the value v with the given arguments. It panics if v is not a function. The arguments get mapped to JavaScript values according to the ValueOf function.

func (Value) IsNull

func (v Value) IsNull() bool

IsNull checks if a value represents JS null object.

func (Value) IsUndefined

func (v Value) IsUndefined() bool

IsUndefined checks if a value represents JS undefined object.

func (Value) JSRef deprecated

func (v Value) JSRef() Ref

JSRef returns a JS object reference as defined by syscall/js.

Deprecated: use JSValue

func (Value) JSValue

func (v Value) JSValue() Ref

JSValue implements Wrapper interface.

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

MarshalJSON encodes a value into JSON by using native JavaScript function (JSON.stringify).

func (Value) New

func (v Value) New(args ...interface{}) Value

New uses JavaScript's "new" operator with value v as constructor and the given arguments. It panics if v is not a function. The arguments get mapped to JavaScript values according to the ValueOf function.

func (Value) NewCallbackGroup deprecated

func (v Value) NewCallbackGroup() *FuncGroup

NewCallbackGroup creates a new function group on this object.

Deprecated: use NewFuncGroup

func (Value) NewFuncGroup

func (v Value) NewFuncGroup() *FuncGroup

NewFuncGroup creates a new function group on this object.

func (Value) Promised

func (v Value) Promised() *Promise

Promised returns converts the value into a Promise.

func (Value) Set

func (v Value) Set(name string, val interface{})

Set sets the JS property to ValueOf(x).

func (Value) SetIndex

func (v Value) SetIndex(i int, val interface{})

SetIndex sets the JavaScript index i of value v to ValueOf(x).

func (Value) Slice

func (v Value) Slice() []Value

Slice converts JS Array to a Go slice of JS values.

func (Value) String

func (v Value) String() string

String converts a value to a string.

func (Value) Type

func (v Value) Type() Type

Type is an analog for JS "typeof" operator.

func (*Value) UnmarshalJSON

func (v *Value) UnmarshalJSON(p []byte) (err error)

UnmarshalJSON decodes a value from JSON by using native JavaScript functions (JSON.parse).

func (Value) Valid

func (v Value) Valid() bool

Valid checks if object is defined and not null.

type Wrapper

type Wrapper interface {
	// JSValue returns a JavaScript value associated with an object.
	JSValue() js.Value
}

Wrapper is implemented by types that are backed by a JavaScript value.

Jump to

Keyboard shortcuts

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