Swiftpack.co - spencerkohan/Swift-EventEmitter as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by spencerkohan.
spencerkohan/Swift-EventEmitter 0.5.0
A minimal type-safe event implementation in Swift
⭐️ 0
🕓 2 weeks ago
.package(url: "https://github.com/spencerkohan/Swift-EventEmitter.git", from: "0.5.0")

EventEmitter

A minimal type-safe event implementation in Swift.

Usage:

Create and emit a typed event:

let event = Event<Sting>()

event.emit("hola")

Create and emit a void event:

let voidEvent = Event<Void>()

voidEvent.emit()

Add an observer:

let observer = event.on { message in
	// do something
}

Add a one-time observer:

let oneTimeObserver = event.once { message in
	// do something
}

Unregister an observer:

// from the observer
observer.unregister()

// through the event
event.unregister(oneTimeObserver)

Re-register an observer:

observer.register()

Observer Groups:

Observer groups allow for the block registration/unregistration of multiple observers at once.

// initialize a group of observers
let group = ObserverGroup([
		eventA.on { ... },
		eventB.on { ... },
		...
	])

// unregister all observers in a group
group.unregisterAll()

// re-register a group of observers
group.registerAll()

Observer groups unregister all their memebers on deinit.

Note

Observer blocks are executed synchronously on the thread where emit is called

GitHub

link
Stars: 0
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics