js

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 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 = Type(iota + 1)
	TypeFunction
)

Variables

This section is empty.

Functions

func NewError added in v0.4.0

func NewError(e Wrapper) error

NewError creates a new Go error from JS error value.

func Set added in v0.4.0

func Set(name string, v interface{})

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

Types

type Arr added in v0.4.0

type Arr = []interface{}

Arr is an alias for []interface{}.

type Error added in v0.4.0

type Error struct {
	Value Ref
}

Error is an alias for syscall/js.Error.

func (Error) Error added in v0.4.0

func (e Error) Error() string

Error implements the error interface.

type Func added in v0.4.0

type Func struct {
	Value Ref
}

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

func AsyncCallbackOf added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 NewEventCallback added in v0.4.0

func NewEventCallback(fnc func(v Value)) Func

NewEventCallback is a shorthand for NewEventCallbackFlags with default flags.

func (Func) Release added in v0.4.0

func (f Func) Release()

Release frees up resources allocated for the function. The function must not be invoked after calling Release.

type FuncGroup added in v0.4.0

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

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

func (*FuncGroup) Add added in v0.4.0

func (g *FuncGroup) Add(cb Func)

func (*FuncGroup) AddEventListener added in v0.4.0

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

func (*FuncGroup) ErrorEvent added in v0.4.0

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

func (*FuncGroup) ErrorEventChan added in v0.4.0

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

func (*FuncGroup) OneTimeEvent added in v0.4.0

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

func (*FuncGroup) OneTimeEventChan added in v0.4.0

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

func (*FuncGroup) OneTimeTrigger added in v0.4.0

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

func (*FuncGroup) Release added in v0.4.0

func (g *FuncGroup) Release()

func (*FuncGroup) Set added in v0.4.0

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

type Memory added in v0.4.0

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

func MMap added in v0.4.0

func MMap(p []byte) *Memory

MMap exposes memory of p to JS.

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

func (*Memory) Bytes added in v0.4.0

func (m *Memory) Bytes() []byte

func (*Memory) CopyFrom added in v0.4.0

func (m *Memory) CopyFrom(v Wrapper) error

CopyFrom copies binary data from JS object into Go buffer.

func (*Memory) JSValue added in v0.4.0

func (m *Memory) JSValue() Ref

func (*Memory) Release added in v0.4.0

func (m *Memory) Release()

type Obj added in v0.4.0

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 added in v0.4.0

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

Await for the promise to resolve.

func (*Promise) AwaitContext added in v0.4.0

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

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

func (*Promise) JSValue added in v0.4.0

func (p *Promise) JSValue() Ref

JSValue implements Wrapper interface.

type Ref added in v0.4.0

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

Ref is an alias for syscall/js.Value.

func (Ref) Bool added in v0.4.0

func (v Ref) Bool() bool

Bool returns the value v as a bool. It panics if v is not a JavaScript boolean.

func (Ref) Call added in v0.4.0

func (v Ref) Call(m string, args ...interface{}) Ref

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 (Ref) Float added in v0.4.0

func (v Ref) Float() float64

Float returns the value v as a float64. It panics if v is not a JavaScript number.

func (Ref) Get added in v0.4.0

func (v Ref) Get(k string) Ref

Get returns the JavaScript property p of value v.

func (Ref) Index added in v0.4.0

func (v Ref) Index(i int) Ref

Index returns JavaScript index i of value v.

func (Ref) InstanceOf added in v0.4.0

func (v Ref) InstanceOf(t Ref) bool

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

func (Ref) Int added in v0.4.0

func (v Ref) Int() int

Int returns the value v truncated to an int. It panics if v is not a JavaScript number.

func (Ref) Invoke added in v0.4.0

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

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 (Ref) Length added in v0.4.0

func (v Ref) Length() int

Length returns the JavaScript property "length" of v.

func (Ref) New added in v0.4.0

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

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 (Ref) Set added in v0.4.0

func (v Ref) Set(p string, x interface{})

Set sets the JavaScript property p of value v to ValueOf(x).

func (Ref) SetIndex added in v0.4.0

func (v Ref) SetIndex(i int, x interface{})

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

func (Ref) String added in v0.4.0

func (v Ref) String() string

String returns the value v converted to string according to JavaScript type conversions.

func (Ref) Truthy added in v0.4.0

func (v Ref) Truthy() bool

Truthy returns the JavaScript "truthiness" of the value v. In JavaScript, false, 0, "", null, undefined, and NaN are "falsy", and everything else is "truthy". See https://developer.mozilla.org/en-US/docs/Glossary/Truthy.

func (Ref) Type added in v0.4.0

func (v Ref) Type() Type

Type returns the JavaScript type of the value v. It is similar to JavaScript's typeof operator, except that it returns TypeNull instead of TypeObject for null.

type Type added in v0.4.0

type Type int

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

type TypedArray added in v0.4.0

type TypedArray struct {
	Value
}

TypedArray represents a JavaScript typed array.

func TypedArrayOf added in v0.4.0

func TypedArrayOf(o interface{}) TypedArray

TypedArrayOf returns a JavaScript typed array backed by the slice's underlying array.

The supported types are []int8, []int16, []int32, []uint8, []uint16, []uint32, []float32 and []float64. Passing an unsupported value causes a panic.

TypedArray.Release must be called to free up resources when the typed array will not be used any more.

func (TypedArray) Release added in v0.4.0

func (v TypedArray) Release()

Release frees up resources allocated for the typed array. The typed array and its buffer must not be accessed after calling Release.

type Value added in v0.4.0

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 added in v0.4.0

func Array() Value

Array returns an Array JS class.

func Call added in v0.4.0

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

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

func Class added in v0.4.0

func Class(class string, path ...string) Value

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

func Get added in v0.4.0

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

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

func NativeFuncOf added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

func NewArray() Value

NewArray creates an empty JS array.

func NewObject added in v0.4.0

func NewObject() Value

NewObject creates an empty JS object.

func NewPromise added in v0.4.0

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 added in v0.4.0

func Object() Value

Object returns an Object JS class.

func ValueOf added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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

Get returns the JS property by name.

func (Value) Index added in v0.4.0

func (v Value) Index(i int) Value

Index returns JS index i of value v.

func (Value) InstanceOf added in v0.4.0

func (v Value) InstanceOf(class Wrapper) bool

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

func (Value) InstanceOfClass added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

func (v Value) IsNull() bool

IsNull checks if a value represents JS null object.

func (Value) IsUndefined added in v0.4.0

func (v Value) IsUndefined() bool

IsUndefined checks if a value represents JS undefined object.

func (Value) JSValue added in v0.4.0

func (v Value) JSValue() Ref

JSValue implements Wrapper interface.

func (Value) MarshalJSON added in v0.4.0

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

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

func (Value) New added in v0.4.0

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) NewFuncGroup added in v0.4.0

func (v Value) NewFuncGroup() *FuncGroup

NewFuncGroup creates a new function group on this object.

func (Value) Promised added in v0.4.0

func (v Value) Promised() *Promise

Promised returns converts the value into a Promise.

func (Value) Set added in v0.4.0

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

Set sets the JS property to ValueOf(x).

func (Value) SetIndex added in v0.4.0

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

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

func (Value) Slice added in v0.4.0

func (v Value) Slice() []Value

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

func (Value) String added in v0.4.0

func (v Value) String() string

String converts a value to a string.

func (Value) Type added in v0.4.0

func (v Value) Type() Type

Type is an analog for JS "typeof" operator.

func (*Value) UnmarshalJSON added in v0.4.0

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

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

func (Value) Valid added in v0.4.0

func (v Value) Valid() bool

Valid checks if object is defined and not null.

type Wrapper added in v0.4.0

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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