eventloop

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package eventloop provides a thread-safe event loop for JavaScript execution.

The event loop ensures that all Goja runtime operations occur on a single goroutine, preventing race conditions. It also manages async operations through a wait group mechanism.

Basic Usage

el := eventloop.NewEventLoop(vm)

el.RunOnLoop(func() {
    vm.RunString(`console.log("Hello")`)
})

el.Start() // Blocks until all work is done

Async Operations

When starting an async operation (HTTP request, timer, etc.), use WGAdd to indicate pending work and WGDone when complete:

el.WGAdd(1)
go func() {
    time.Sleep(time.Second)
    el.RunOnLoop(func() {
        vm.RunString(`console.log("Delayed")`)
        el.WGDone()
    })
}()

The event loop automatically stops when all pending operations complete.

Promises

CreatePromise returns a JavaScript Promise along with resolve/reject functions that properly integrate with the event loop:

promise, resolve, reject := el.CreatePromise()
go func() {
    result, err := doAsyncWork()
    if err != nil {
        reject(err)
    } else {
        resolve(result)
    }
}()
return promise

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventLoop

type EventLoop struct {
	VM *sobek.Runtime

	OnUnhandledRejection RejectionHandler
	// contains filtered or unexported fields
}

func NewEventLoop

func NewEventLoop(vm *sobek.Runtime) *EventLoop

func (*EventLoop) Context

func (el *EventLoop) Context() context.Context

func (*EventLoop) CreatePromise

func (el *EventLoop) CreatePromise() (promise *sobek.Object, resolve func(interface{}), reject func(interface{}))

func (*EventLoop) RunOnLoop

func (el *EventLoop) RunOnLoop(f func())

RunOnLoop schedules a function to run on the JS thread. Safe for concurrent use.

func (*EventLoop) SetAutoStop

func (el *EventLoop) SetAutoStop(enable bool)

func (*EventLoop) Shutdown

func (el *EventLoop) Shutdown(timeout context.Context) error

func (*EventLoop) Start

func (el *EventLoop) Start()

Start runs the event loop. It blocks until the loop is stopped or all tasks are done.

func (*EventLoop) Stop

func (el *EventLoop) Stop()

func (*EventLoop) WGAdd

func (el *EventLoop) WGAdd(n int)

func (*EventLoop) WGDone

func (el *EventLoop) WGDone()

type RejectionHandler

type RejectionHandler func(err error)

Jump to

Keyboard shortcuts

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