Giter VIP home page Giter VIP logo

event2's Introduction

Event II

Build Status GoDoc License Go Report Card

A simple event system, for triggering events at certain times. This is the successor of event, which was needlessly complex.

Example use

Leet o'clock

package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/xyproto/event2"
)

func main() {
	// Create a new event system, with a loop iteration delay of 1 second
	eventSys := event2.NewSystem(1 * time.Second)
	// Add an event that will trigger every day at 13:37
	eventSys.ClockEvent(13, 37, func() error {
		fmt.Println("It's leet o'clock")
		return nil
	})
	// Run the event system (not verbose)
	eventSys.Run(false)
	// Wait endlessly
	var wg sync.WaitGroup
	wg.Add(1)
	wg.Wait()
}

Clock

package main

import (
	"fmt"
	"time"

	"github.com/xyproto/event2"
)

func clockSystem() *event2.EventSys {
	sys := event2.NewSystem(1 * time.Second)
	for hour := 0; hour < 24; hour++ {
		for minute := 0; minute < 60; minute++ {
			// Create new variables that can be closed over by the new function below
			hour := hour
			minute := minute
			// Create a new event that will trigger at the specified hour and minute
			sys.ClockEvent(hour, minute, func() error {
				fmt.Printf("The clock is %02d:%02d\n", hour, minute)
				return nil
			})
		}
	}
	return sys
}

func main() {
	// Start the event system that will trigger an event at every minute
	clockSystem().Run(false)
	// Wait endlessly while saying "tick" and "tock" every second
	for {
		fmt.Println("tick")
		time.Sleep(1 * time.Second)
		fmt.Println("tock")
		time.Sleep(1 * time.Second)
	}
}

General info

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.