js

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2019 License: Apache-2.0 Imports: 6 Imported by: 4

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

func NewError(e Wrapper) error

NewError creates a new Go error from JS error value.

func Set added in v0.2.3

func Set(name string, v interface{})

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

Types

type Arr added in v0.2.3

type Arr = []interface{}

Arr is an alias for []interface{}.

type Callback deprecated added in v0.2.3

type Callback = Func

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

Deprecated: use Func

type CallbackGroup deprecated added in v0.2.3

type CallbackGroup = FuncGroup

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

Deprecated: use FuncGroup

type Error added in v0.2.3

type Error = js.Error

Error is an alias for syscall/js.Error.

type Func added in v0.2.3

type Func = js.Func

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

func AsyncCallbackOf added in v0.2.3

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.2.3

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.2.3

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

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

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

func NewEventCallback(fnc func(v Value)) Func

NewEventCallback is a shorthand for NewEventCallbackFlags with default flags.

func NewFunc deprecated added in v0.2.3

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

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.2.3

func (g *FuncGroup) Add(cb Func)

func (*FuncGroup) AddEventListener added in v0.2.3

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

func (*FuncGroup) ErrorEvent added in v0.2.3

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

func (*FuncGroup) ErrorEventChan added in v0.2.3

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

func (*FuncGroup) OneTimeEvent added in v0.2.3

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

func (*FuncGroup) OneTimeEventChan added in v0.2.3

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

func (*FuncGroup) OneTimeTrigger added in v0.2.3

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

func (*FuncGroup) Release added in v0.2.3

func (g *FuncGroup) Release()

func (*FuncGroup) Set added in v0.2.3

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

type JSRef deprecated added in v0.2.3

type JSRef = Wrapper

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

Deprecated: see Wrapper

type Obj added in v0.2.3

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.2.3

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

Await for the promise to resolve.

func (*Promise) AwaitContext added in v0.2.3

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.2.3

func (p *Promise) JSValue() Ref

JSValue implements Wrapper interface.

type Ref added in v0.2.3

type Ref = js.Value

Ref is an alias for syscall/js.Value.

type Type added in v0.2.3

type Type = js.Type

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

type Value added in v0.2.3

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.2.3

func Array() Value

Array returns an Array JS class.

func Call added in v0.2.3

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

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

func Class added in v0.2.3

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

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

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

func NativeFuncOf added in v0.2.3

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.2.3

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.2.3

func NewArray() Value

NewArray creates an empty JS array.

func NewFuncJS deprecated added in v0.2.3

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

func NewObject() Value

NewObject creates an empty JS object.

func NewPromise added in v0.2.3

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.2.3

func Object() Value

Object returns an Object JS class.

func ValueOf added in v0.2.3

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.2.3

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.2.3

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.2.3

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

Get returns the JS property by name.

func (Value) Index added in v0.2.3

func (v Value) Index(i int) Value

Index returns JS index i of value v.

func (Value) InstanceOf added in v0.2.3

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

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.2.3

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.2.3

func (v Value) IsNull() bool

IsNull checks if a value represents JS null object.

func (Value) IsUndefined added in v0.2.3

func (v Value) IsUndefined() bool

IsUndefined checks if a value represents JS undefined object.

func (Value) JSRef deprecated added in v0.2.3

func (v Value) JSRef() Ref

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

Deprecated: use JSValue

func (Value) JSValue added in v0.2.3

func (v Value) JSValue() Ref

JSValue implements Wrapper interface.

func (Value) MarshalJSON added in v0.2.3

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.2.3

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

func (v Value) NewCallbackGroup() *FuncGroup

NewCallbackGroup creates a new function group on this object.

Deprecated: use NewFuncGroup

func (Value) NewFuncGroup added in v0.2.3

func (v Value) NewFuncGroup() *FuncGroup

NewFuncGroup creates a new function group on this object.

func (Value) Promised added in v0.2.3

func (v Value) Promised() *Promise

Promised returns converts the value into a Promise.

func (Value) Set added in v0.2.3

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

Set sets the JS property to ValueOf(x).

func (Value) SetIndex added in v0.2.3

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.2.3

func (v Value) Slice() []Value

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

func (Value) String added in v0.2.3

func (v Value) String() string

String converts a value to a string.

func (Value) Type added in v0.2.3

func (v Value) Type() Type

Type is an analog for JS "typeof" operator.

func (*Value) UnmarshalJSON added in v0.2.3

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.2.3

func (v Value) Valid() bool

Valid checks if object is defined and not null.

type Wrapper added in v0.2.3

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