Giter Club home page Giter Club logo

event's Introduction

event

A Go module providing an event system

Overview

This module provides a fairly classic event system. The core concepts are:

  • An engine: each engine has two event queues; one for events that can be handed to the calling application for usage; one of active event, that needs for the engine to be handled.
  • For each given engine, one can register a new event type. Event types are designed to guve the opportunity to applications to attach semantics to a given event.
  • For each event type, one can register callbacks. If multiple callbacks are registered, they are called in order of the registration, it is the responsability of the calling applications to know that order when relevant (by controling how callbacks are registered). If a callback returns an error, the engine will stop.
  • The calling application gets an event from the engine before being able to use it. Two modes are available, blocking and non-blocking. In blocking mode, the call to get the event blocks until an event is available; while in non-blocking, the call will return nil if no event is available.
  • Once the application is done with an event, the event must be returned so it can be reused.

Usage

For more details about usage, please refer to tests.

Create a new engine

queueCfg := QueueCfg{
	Size: <initial number of available events>,
}
engine := queueCfg.Init()
if engine == nil {
	return fmt.Errorf("unable to create event engine")
}

Termination of an engine

engine.Fini()

Create of a new event type and registration of a callback

func callback(ctx context.Context, engine *Engine, evt *Event) error {
    // A simple example of a callback returning an event
	err := engine.Return(evt)
	if err != nil {
		return fmt.Errorf("unable to return event: %w", err)
	}
	return nil
}
eventType, err := engine.NewType("myEventType")
if err != nil {
	return fmt.Errorf("unable to create new event type: %w", err)
}
err := engine.RegisterCallback(&eventType, callback)
if err != nil {
	return fmt.Errorf("unable to register callback: %w", err)
}

Get an event and emit it

evt1 := engine.GetEvent(true)
if evt1 == nil {
	return fmt.Errorf("unable to get first event")
}
err = evt1.SetType(eventType)
if err != nil {
	return fmt.Errorf("failed to set event type")
}
err = evt1.Emit(nil)
if err != nil {
	return fmt.Errorf("unable to emit event: %w", err)
}

event's People

Contributors

gvallee avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.