ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - aleclarson/emitter-kit: Type-safe event handling for Swift
Type-safe event handling for Swift. Contribute to aleclarson/emitter-kit development by creating an account on GitHub.
Visit Site

GitHub - aleclarson/emitter-kit: Type-safe event handling for Swift

GitHub - aleclarson/emitter-kit: Type-safe event handling for Swift

emitter-kit v5.2.2

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 { (notif: Notification) in
  print(notif.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)
}

 

Thread Safety

⚠️ None of the classes in EmitterKit are thread-safe!

The following actions must be done on the same thread, or you need manual locking:

  • Emit an event
  • Add/remove a listener
  • Set the isListening property of a listener

 

v5.2.2 changelog

  • Fixed protocol casting (#60)

v5.2.1 changelog

  • Fix Carthage compatibility for non iOS platforms

v5.2.0 changelog

  • Added the Event.getListeners method
  • Listeners are now always called in the order they were added
  • Event.emit() can be called without an argument
  • Carthage support has been improved

v5.1.0 changelog

  • The NotificationListener class now takes a Notification instead of an NSDictionary.

  • A NotificationListener without a target will now receive every Notification with its name, regardless of the value of notif.object.

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.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory