listeners

package
v0.0.0-...-f888fa7 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(iso *v8.Isolate, global *v8.ObjectTemplate, opts ...Opt) error

Add ...

Example
package main

import (
	"fmt"

	v8 "github.com/zeiss/v8go"
	"github.com/zeiss/v8go-polyfills/listeners"
)

func main() {
	iso := v8.NewIsolate()
	global := v8.NewObjectTemplate(iso)

	in := make(chan *v8.Object)
	out := make(chan *v8.Value)

	err := listeners.Add(iso, global, listeners.WithEvents("auth", in, out))
	if err != nil {
		panic(err)
	}

	ctx := v8.NewContext(iso, global)

	_, err = ctx.RunScript("addEventListener('auth', event => { return event.sourceIP === '127.0.0.1' })", "listener.js")
	if err != nil {
		panic(err)
	}

	obj, err := newContextObject(ctx)
	if err != nil {
		panic(err)
	}

	in <- obj
	v := <-out

	fmt.Println(v)
}

func newContextObject(ctx *v8.Context) (*v8.Object, error) {
	iso := ctx.Isolate()
	obj := v8.NewObjectTemplate(iso)

	resObj, err := obj.NewInstance(ctx)
	if err != nil {
		return nil, err
	}

	for _, v := range []struct {
		Key string
		Val interface{}
	}{
		{Key: "sourceIP", Val: "127.0.0.1"},
	} {
		if err := resObj.Set(v.Key, v.Val); err != nil {
			return nil, err
		}
	}

	return resObj, nil
}
Output:

true

Types

type Listener

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

Listener is a polyfill for the addEventListener method.

func New

func New(opt ...Opt) *Listener

New ...

func (*Listener) GetFunctionCallback

func (l *Listener) GetFunctionCallback() v8.FunctionCallback

GetFunctionCallback ...

func (*Listener) GetMethodName

func (l *Listener) GetMethodName() string

GetMethodName ...

type Opt

type Opt func(*Listener)

Opt is a functional option for configuring the listener.

func WithEvents

func WithEvents(name string, in chan *v8.Object, out chan *v8.Value) Opt

WithEvents ...

Jump to

Keyboard shortcuts

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