event

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package event contains core browser event behavior

Index

Constants

View Source
const DefaultBuf = 16

DefaultBuf is the default buffer size for event channels created by EventSource when buffer size not specified explicitly. Buffer size affects event ordering guarantees.

Variables

This section is empty.

Functions

func Capture

func Capture(o *EventListener)

func NoError

func NoError[T func(U), U any](f T) func(U) error

NoError takes a function accepting a single argument and has no return value, and transforms it into a function that can be used where an error return value is expected.

func Once

func Once(o *EventListener)

func SetEventTargetSelf

func SetEventTargetSelf(t EventTarget)

Types

type CustomEventInit

type CustomEventInit struct {
	Detail interface{}
}

type Entity added in v0.5.0

type Entity interface {
	ObjectId() entity.ObjectId
}

type ErrorEventInit

type ErrorEventInit struct {
	Err error
}

type Event

type Event struct {
	entity.Entity
	Type       string
	Bubbles    bool
	Cancelable bool
	Data       any

	EventPhase       EventPhase
	DefaultPrevented bool

	Target        EventTarget
	CurrentTarget EventTarget
	// contains filtered or unexported fields
}

Event corresponds to a DOM Event dispatched by a DOM EventTarget. Different types of events carry different data. The event-specific data exist in the Data property, which must be a valid "EventInit" type. The data contains the event-specific values of the 2nd constructor argument.

// JS:
const event = new CustomEvent("my-custom", { bubbles: true, details: "Something else" })

The corresponding Go event would be:

event := Event {
  Type: "My-custom",
  Bubbles: true,
  Data: CustomEventInit{ Details: "Something else" },
}

The "EventInit" postfix reflect naming in IDL specifications.

The Go Event representation stores the value for Bubbles on the event itself. The other properties on the event options are data communicated between the event dispatcher and the event listener, which Gost doesn't care about, and as such is stored as an interface{} type.

In JavaScript, events are represented by concrete subclasses of the base Event class. The concrete class used will be determined by the Data property.

func NewCustomEvent

func NewCustomEvent(eventType string, init CustomEventInit) *Event

func NewErrorEvent

func NewErrorEvent(err error) *Event

func (*Event) PreventDefault

func (e *Event) PreventDefault()

func (*Event) StopPropagation

func (e *Event) StopPropagation()

type EventHandler

type EventHandler interface {
	// HandleEvent is called when the the event occurrs.
	//
	// An non-nil error return value will dispatch an error event on the global
	// object in a normally configured environment.
	HandleEvent(event *Event) error
	// Equals must return true, if the underlying event handler of the other
	// handler is the same as this handler.
	Equals(other EventHandler) bool
}

EventHandler is the interface for an event handler. In JavaScript; an event handler can be a function; or an object with a `handleEvent` function. In Go code, you can provide your own implementation, or use NewEventHandlerFunc to create a valid handler from a function.

Multiple EventHandler instances can represent the same underlying event handler. E.g., when JavaScript code calls RemoveEventListener, a new Go struct is created wrapping the same underlying handler.

The Equals function must return true when the other event handler is the same as the current value, so event handlers can properly be removed, and avoiding duplicates are added by AddEventListener.

func NewEventHandlerFunc

func NewEventHandlerFunc(handler HandlerFunc) EventHandler

NewEventHandlerFunc creates an EventHandler implementation from a compatible function.

Note: Calling this twice for the same Go-function will be treated as different event handlers. Be sure to use the same instance returned from this function when removing.

func NewEventHandlerFuncWithoutError

func NewEventHandlerFuncWithoutError(handler HandlerFuncWithoutError) EventHandler

type EventListener

type EventListener struct {
	Handler EventHandler
	Capture bool
	Once    bool
}

type EventListenerOption added in v0.7.2

type EventListenerOption = func(*EventListener)

type EventPhase

type EventPhase int
const (
	EventPhaseNone     EventPhase = 0
	EventPhaseCapture  EventPhase = 1
	EventPhaseAtTarget EventPhase = 2
	EventPhaseBubbling EventPhase = 3
)

type EventSource added in v0.8.0

type EventSource struct {
	EventTarget
}

EventSource embeds an EventTarget and provides events in a channel, simplifying Go code consuming events.

func NewEventSource added in v0.8.0

func NewEventSource(tgt EventTarget) EventSource

func (EventSource) Listen added in v0.8.0

func (s EventSource) Listen(
	ctx context.Context,
	t string,
	opts ...EventSourceOption,
) <-chan *Event

Listen adds an event listener for events of type t and returns a channel of events containing all the events. Cancelling context ctx will remove the event listener and close the channel. If no context is passed, the event listener will never be removed.

Ordering of events is guaranteed when the channel buffer is not full and all events are dispatched from the same goroutine. The channel buffer size is controlled with the BufSize option. Default value is DefaultBuf.

type EventSourceOption added in v0.8.0

type EventSourceOption func(*eventSourceOptions)

func BufSize added in v0.8.0

func BufSize(buf int) EventSourceOption

BufSize is an option to EventSource.Listen, indicating the buffer size of the event channel returned.

type EventTarget

type EventTarget interface {
	AddEventListener(eventType string, listener EventHandler, options ...EventListenerOption)
	RemoveEventListener(eventType string, listener EventHandler, options ...EventListenerOption)
	DispatchEvent(event *Event) bool
	// Adds a listener that will receive _all_ dispatched event. This listener
	// will not be removed from the window when navigating. This makes it useful
	// for a test to setup event listeners _before_ navigating, as by the time the
	// Navigate function returns, the DOMContentLoaded event _has_ fired, and
	// subscribed listeners have been called.
	SetCatchAllHandler(listener EventHandler)
	SetParentTarget(EventTarget)
	RemoveAll()
	// contains filtered or unexported methods
}

func NewEventTarget

func NewEventTarget() EventTarget

type HandlerFunc

type HandlerFunc = func(*Event) error

type HandlerFuncWithoutError

type HandlerFuncWithoutError = func(*Event)

Jump to

Keyboard shortcuts

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