Documentation ¶
Overview ¶
Package js provides additional functionality on top of syscall/js package for WASM.
Index ¶
- Constants
- func NewError(e Wrapper) error
- func Set(name string, v interface{})
- type Arr
- type Callbackdeprecated
- type CallbackGroupdeprecated
- type Error
- type Func
- func AsyncCallbackOf(fnc func(v []Value)) Func
- func CallbackOf(fnc func(v []Value)) Func
- func FuncOf(fnc func(this Value, args []Value) interface{}) Func
- func NewCallback(fnc func(v []Value)) Funcdeprecated
- func NewCallbackAsync(fnc func(v []Value)) Funcdeprecated
- func NewEventCallback(fnc func(v Value)) Func
- func NewFunc(fnc func(this Value, args []Value) interface{}) Funcdeprecated
- type FuncGroup
- func (g *FuncGroup) Add(cb Func)
- func (g *FuncGroup) AddEventListener(event string, fnc func(Value))
- func (g *FuncGroup) ErrorEvent(fnc func(error))
- func (g *FuncGroup) ErrorEventChan() <-chan error
- func (g *FuncGroup) OneTimeEvent(event string, fnc func(Value))
- func (g *FuncGroup) OneTimeEventChan(event string) <-chan Value
- func (g *FuncGroup) OneTimeTrigger(event string) <-chan struct{}
- func (g *FuncGroup) Release()
- func (g *FuncGroup) Set(name string, fnc func([]Value))
- type JSRefdeprecated
- type Obj
- type Promise
- type Ref
- type Type
- type Value
- func Array() Value
- func Call(name string, args ...interface{}) Value
- func Class(class string) Value
- func Get(name string, path ...string) Value
- func NativeFuncOf(argsAndCode ...string) Value
- func New(class string, args ...interface{}) Value
- func NewArray() Value
- func NewFuncJS(argsAndCode ...string) Valuedeprecated
- func NewObject() Value
- func NewPromise(fnc func() ([]interface{}, error)) Value
- func Object() Value
- func ValueOf(o interface{}) Value
- func (v Value) Await() ([]Value, error)
- func (v Value) Call(name string, args ...interface{}) Value
- func (v Value) Get(name string, path ...string) Value
- func (v Value) Index(i int) Value
- func (v Value) InstanceOf(class Value) bool
- func (v Value) InstanceOfClass(class string) bool
- func (v Value) Invoke(args ...interface{}) Value
- func (v Value) IsNull() bool
- func (v Value) IsUndefined() bool
- func (v Value) JSRef() Refdeprecated
- func (v Value) JSValue() Ref
- func (v Value) MarshalJSON() ([]byte, error)
- func (v Value) New(args ...interface{}) Value
- func (v Value) NewCallbackGroup() *FuncGroupdeprecated
- func (v Value) NewFuncGroup() *FuncGroup
- func (v Value) Promised() *Promise
- func (v Value) Set(name string, val interface{})
- func (v Value) SetIndex(i int, val interface{})
- func (v Value) Slice() []Value
- func (v Value) String() string
- func (v Value) Type() Type
- func (v *Value) UnmarshalJSON(p []byte) (err error)
- func (v Value) Valid() bool
- type Wrapper
Constants ¶
const ( TypeObject = js.TypeObject TypeFunction = js.TypeFunction )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CallbackGroup
deprecated
type CallbackGroup = FuncGroup
CallbackGroup is a list of Go functions attached to an object.
Deprecated: use FuncGroup
type Func ¶
Func is a wrapped Go function to be called by JavaScript.
func AsyncCallbackOf ¶
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 ¶
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 ¶
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
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
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 ¶
NewEventCallback is a shorthand for NewEventCallbackFlags with default flags.
type FuncGroup ¶
type FuncGroup struct {
// contains filtered or unexported fields
}
FuncGroup is a list of Go functions attached to an object.
func (*FuncGroup) AddEventListener ¶
func (*FuncGroup) ErrorEvent ¶
func (*FuncGroup) ErrorEventChan ¶
func (*FuncGroup) OneTimeEvent ¶
func (*FuncGroup) OneTimeEventChan ¶
func (*FuncGroup) OneTimeTrigger ¶
type Promise ¶
type Promise struct {
// contains filtered or unexported fields
}
Promise represents a JavaScript Promise.
func (*Promise) AwaitContext ¶
AwaitContext for the promise to resolve or context to be canceled.
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 Class ¶
Class searches for a class in global scope. It caches results, so the lookup should be faster than calling Get.
func NativeFuncOf ¶
NativeFuncOf creates a function from a JS code string.
Example:
NativeFuncOf("a", "b", "return a+b").Call(a, b)
func NewPromise ¶
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 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 ¶
Await wait for the promise to be resolved or rejected. A shorthand for calling Await on the promise returned by Promised.
func (Value) Call ¶
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) InstanceOf ¶
InstanceOf reports whether v is an instance of type t according to JavaScript's instanceof operator.
func (Value) InstanceOfClass ¶
InstanceOfClass reports whether v is an instance of named type according to JavaScript's instanceof operator.
func (Value) Invoke ¶
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) IsUndefined ¶
IsUndefined checks if a value represents JS undefined object.
func (Value) MarshalJSON ¶
MarshalJSON encodes a value into JSON by using native JavaScript function (JSON.stringify).
func (Value) New ¶
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 (Value) NewFuncGroup ¶
NewFuncGroup creates a new function group on this object.
func (*Value) UnmarshalJSON ¶
UnmarshalJSON decodes a value from JSON by using native JavaScript functions (JSON.parse).