Coder Social home page Coder Social logo

emitter-kit's Introduction

emitter-kit v5.0.0

stable CocoaPods Compatible Carthage Compatible Platform

A replacement for NSNotificationCenter#addObserver and NSObject#addObserver that is type-safe and not verbose.

import EmitterKit

// A generic event emitter (but type-safe)!
var event = Event<T>()

// Any emitted data must be the correct type.
event.emit(data)

// This listener will only be called once.
// You are *not* required to retain it.
event.once { data: T in
  print(data)
}

// This listener won't stop listening;
// unless you stop it manually,
// or its Event<T> is deallocated.
// You *are* required to retain it.
var listener = event.on { data: T in
  print(data)
}

// Stop the listener manually.
listener.isListening = false

// Restart the listener (if it was stopped).
listener.isListening = true

Targeting

A target allows you to associate a specific AnyObject with an emit call. This is useful when emitting events associated with classes you can't add properties to (like UIView).

When calling emit with a target, you must also call on or once with the same target in order to receive the emitted event.

let myView = UIView()
let didTouch = Event<UITouch>()

didTouch.once(myView) { touch in
  print(touch)
}

didTouch.emit(myView, touch)

NSNotification

The Notifier class helps when you are forced to use NSNotificationCenter (for example, if you want to know when the keyboard has appeared).

// You are **not** required to retain this after creating your listener.
var event = Notifier(UIKeyboardWillShowNotification)

// Handle NSNotifications with style!
listener = event.on { (userInfo: NSDictionary) in
  print(userInfo)
}

Key-Value Observation (KVO)

// Any NSObject descendant will work.
var view = UIView()

// "Make KVO great again!" - Donald Trump
listener = view.on("bounds") { (change: Change<CGRect>) in
  print(change)
}

v5.0.0 changelog

  • Swift 3.0 + Xcode 8.0 beta 6 support

  • The Signal class was removed. (use Event<Void> instead)

  • The Emitter abstract class was removed.

  • The EmitterListener class was renamed EventListener<T>.

  • The Event<T> class no longer has a superclass.

  • The Notification class was renamed Notifier (to prevent collision with Foundation.Notification).

  • The on and once methods of Event<T> now return an EventListener<T> (instead of just a Listener)

  • The on and once methods of Notifier now return an NotificationListener (instead of just a Listener)

  • The on and once methods of NSObject now return an ChangeListener<T> (instead of just a Listener)

  • The keyPath, options, and object properties of ChangeListener<T> are now public.

  • A listenerCount: Int computed property was added to the Event<T> class.

  • An event: Event<T> property was added to the EventListener<T> class.

The changelog for older versions can be found here.

Known bugs

  • The ChangeListener class may throw an exception (looking for help):
An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.```

emitter-kit's People

Contributors

aleclarson avatar kostickm avatar markshiz avatar phimage avatar yonaskolb 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.