SwiftEventCenter 1.1.1

SwiftEventCenter 1.1.1

Maintained by Ladislas de Toldi.



  • By
  • Ladislas de Toldi

Swift Event Center

swift-version spm CocoaPods Build Status SonarCloud Coverage SwiftEventCenter

About

Swift Event Center to create custom events and respond to them.

The idea was to build a simple event center from scratch to:

  • better understand how a notification/event center would work
  • have simple event manager for simple tasks
  • in pure Swift
  • and not use Apple's Notification Center

The interesting thing about the process is that while building it, I got a deeper understanding of how Notification Center.

To make it easier for new comers, I decided to keep Apple's way of naming things.

Note: EventCenter is not meant to be a full replacement of Notification Center. It is a simpler alternative (as in "simpler to use, but also simpler in terms of functionalities") that should be good enough for simple projects.

Features

EventCenter allows you to:

  • - add observer for Event.Name (a.k.a. Notification.Name)
  • - specify a name for added observers so that they can be removed independently
  • - remove all observers for a specific Event.Name
  • - remove one observer with a specific name for a specific Event.Name
  • - post events and pass object:Any? & userInfo:[AnyHashable:Any]?
  • - specify priority for each observer (not implemented yet)

Installation

With SPM, add the following to the dependencies of your Package.swift

.package(url: "https://github.com/ladislas/SwiftEventCenter", from: "1.1.1")

Usage

Event is just a typealias for Notification.

See docs, Examples & Tests for more information.

import EventCenter

let ec = EventCenter()

...

ec.addObserver(forEvent: Event.Name("event1"), name: "obs1", callback: { event: Event in 

	if let obj = event.object {
		print("Hello, \(obj)!")	
	}

})

...

ec.post(event: Event.Name("event1"), object: "World")

Authors

Made with ❤️ by:

License

Apache 2.0 @ Ladislas de Toldi

Original Work

This project is based on great people's ideas. The original idea was created by Stephen Haney. It was then improved by Robin Walter's Swift-Events. First I wanted to fork Robin's repo but after spending a couple of hours on it, I decided to rewrite most of it. Original work from Stephen Haney & Robin Walter is under MIT.