Code Monkey home page Code Monkey logo

go-events's Introduction

go-events

go get github.com/fdanismaz/go-events/event

Initialization

The event listener starts as the github.com/fdanismaz/go-events/event is imported because the package has an init function which starts the listener until you explicitly call event.Stop() function.

Subscribing to Events

To subscribe to an event you can use the Subscribe or SubscribeMultiple functions. The Subscribe function subscribe your callback function to a specific event, SubscribeMultiple function, on the other hand, subscribe your callback function to multiple events, meaning that whenever any of the subscribed event is emitted, your callback function will be executed.

var UserCreated event.Type = "user-created"

handlerId := event.Subscribe(UserCreated, func(args ...interface{}) {
    id := args[0].(int)
    name := args[1].(string)
    
    fmt.Printf("A new user is created. ID: %d, Name: %s\n", id, name)
})

Unsubscribing from Events

Both the Subscribe and the SubscribeMultiple creates returns a handler id for your callback function. When you want to unsubscribe from a specific event or multiple events you can use Unsubscribe or UnsubscribeMultiple.

event.Unsubscribe(UserCreated, handlerId)

Emitting Events

When you emit an event, an event object including the event type, and the arguments is created. That event object is written to the event channel which is catched by the event listener. The event listener executes each registered handler function for that event type in a separate go routine.

event.Emit(UserCreated, userId, userName)

go-events's People

Contributors

fdanismaz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.