Giter VIP home page Giter VIP logo

sevents's Introduction

SEvents

Swift events dispatching made easy.

It adds a convenient way for your objects to declare events that those objects send, and let listeners register easily to those events to know when they happen.

How it works

An Event object is a way for an object instance to declare an event that can happen within this particular object lifecycle. Other objects listen to this event and the object that manages this event decides when to propagate the event to all the listeners.

An event listener is called a handler. c To add a handler, you can either call the "add()" method on the Event object or use the overloaded operator "+=". Both methods return an integer id that can be used for removing the added handler later.

To notify the handlers, you call "notify()" that will notify every handlers that this event occured.

Usage

// Declaring an event
// It declares "onTextEntered" has an event than can be produced
// Objects can register to it to find out when this event happens
let onTextEntered = Event<String>()

// Registering to an event
// It adds an handler on this event. When this event
// is notified, the added callback is called
onTextEntered += { (text) in 
  println("User entered: \(text)")
}

// Or without using the operator overload
onTextEntered.add({ (text) in
  println("User entered: \(text)")
})

// Notifying an event.
// This calls every callback registered on this event.
// On this example, this should print two times "User entered: wololo"
onTextEntered.notify("wololo")

// Declaring an event that takes multiple parameter.
// Just adds the number of parameter after the "Event" class, it goes up to 8.
let onTextEntered = Event2<String, Int>()
onTextEntered.notify("Hey", 42)

// Adding a handler and keeping a reference to it
let id = onTextEntered += ({ (text, id) in
  println("\(id) - \(text)")
})

// Removing a handler by using the reference
onTextEntered -= id

Multithreading

Events can be either thread safe or not. Making them thread safe will allow adding, removing and notifying handlers to be done on any queue/thread.

Events can also have a dispatchQueue. This will make every notify() calls to be done asynchronously on this given queue.

Each added handler can also specify a queue to which they want to be called. This makes the handler to be notified in the thread they specified. This override the dispatchQueue set when creating the event.

Multithreading usage

// Declaring a thread safe Event
let onBufferAppended = Event<Double>(threadSafe: true)

// Add a handler that will be called on the main queue
onBufferedAppended.add({ (time) in
  println("Added buffer at \(time)")
}, dispatchQueue: dispatch_get_main_queue())

// Declaring a thread safe event where every handlers are called on the main thread
let onBufferAppended = Event<Double>(threadSafe: true, dispatchQueue: dispatch_get_main_queue())

Installation

It's just one file. Until there is a proper way to handle project dependencies on Swift, you can easily take this file and copy it to your project.

sevents's People

Contributors

rflex avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

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