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
added in
v0.2.3
type CallbackGroup = FuncGroup
CallbackGroup is a list of Go functions attached to an object.
Deprecated: use FuncGroup
type Func ¶ added in v0.2.3
Func is a wrapped Go function to be called by JavaScript.
func AsyncCallbackOf ¶ added in v0.2.3
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
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
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
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
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
NewEventCallback is a shorthand for NewEventCallbackFlags with default flags.
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) AddEventListener ¶ added in v0.2.3
func (*FuncGroup) ErrorEvent ¶ added in v0.2.3
func (*FuncGroup) ErrorEventChan ¶ added in v0.2.3
func (*FuncGroup) OneTimeEvent ¶ added in v0.2.3
func (*FuncGroup) OneTimeEventChan ¶ added in v0.2.3
func (*FuncGroup) OneTimeTrigger ¶ added in v0.2.3
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) AwaitContext ¶ added in v0.2.3
AwaitContext for the promise to resolve or context to be canceled.
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 Class ¶ added in v0.2.3
Class searches for a class in global scope. It caches results, so the lookup should be faster than calling Get.
func NativeFuncOf ¶ added in v0.2.3
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
New searches for a class in global scope and creates a new instance of that class.
func NewPromise ¶ added in v0.2.3
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 ¶ 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
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
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 ¶ added in v0.2.3
InstanceOf reports whether v is an instance of type t according to JavaScript's instanceof operator.
func (Value) InstanceOfClass ¶ added in v0.2.3
InstanceOfClass reports whether v is an instance of named type according to JavaScript's instanceof operator.
func (Value) Invoke ¶ added in v0.2.3
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 ¶ added in v0.2.3
IsUndefined checks if a value represents JS undefined object.
func (Value) MarshalJSON ¶ added in v0.2.3
MarshalJSON encodes a value into JSON by using native JavaScript function (JSON.stringify).
func (Value) New ¶ added in v0.2.3
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 (Value) NewFuncGroup ¶ added in v0.2.3
NewFuncGroup creates a new function group on this object.
func (Value) SetIndex ¶ added in v0.2.3
SetIndex sets the JavaScript index i of value v to ValueOf(x).
func (*Value) UnmarshalJSON ¶ added in v0.2.3
UnmarshalJSON decodes a value from JSON by using native JavaScript functions (JSON.parse).