Introduction
EmitterKit is a powerful event handling and observation framework for iOS and macOS development. It provides a clean and efficient way to manage events, notifications, and data changes in your app’s architecture. With EmitterKit, you can easily handle user interactions, network responses, and data updates with a consistent and scalable approach.
Features
- Simple and intuitive API for event handling and observation
- Supports multiple event types and custom event payload
- Flexible event dispatching with synchronous or asynchronous options
- Efficient observers management with automatic memory management
- Thread-safe design for concurrent operations
- Extensive documentation and examples for easy integration
- Compatible with Swift and Objective-C
Installation
EmitterKit can be easily installed using the following options:
- Using Cocoapods:
- Using Carthage:
- Manually:
- Download the latest release from the GitHub repository.
- Drag and drop the
EmitterKit.xcodeproj
file into your project. - In your project’s settings, select your target, go to the General tab, and add EmitterKit.framework in the Frameworks, Libraries, and Embedded Content section.
pod 'EmitterKit'
github "gmertk/EmitterKit"
Usage
EmitterKit provides a straightforward API with a variety of options to handle events and observations efficiently in your application. Here are some common use cases:
Basic Event Handling
To handle events, you start by creating an instance of Event
or one of its subclasses:
let event = Event()
You can then add an event handler closure for the desired event:
event.on { parameter in
// Handle the event here
}
When you no longer need the event handler, you can remove it as follows:
event.off(handler: eventHandler)
Dispatching Events
EmitterKit allows you to dispatch events manually or automatically. Here’s how you can dispatch an event manually:
event.emit(parameter: eventParameter)
If you prefer automatic dispatching, you can create a DispatchSource
property that triggers the event whenever its value changes:
let dispatchSource = DispatchSource.makeTimerSource()
// Configure the dispatch source...
dispatchSource.setEventHandler { [weak event] in
event?.emit(parameter: eventParameter)
}
dispatchSource.resume()
Custom Event Types
You can define custom event types by subclassing Event
. This allows you to attach custom properties or methods to your events:
class CustomEvent: Event {
var customProperty: String = ""
}
Observation
You can observe event changes using the EventSource
class:
let eventSource = EventSource()
eventSource.register(event) // Register the event to be observed
eventSource.observe { event in
// Handle the observed event here
}
Billboard Observers
Billboard observers allow you to capture events as they occur, and execute a corresponding action. Here’s an example of how to use a billboard observer:
let observer = event.respond { parameter in
// Handle the event occurrence here
}
You can remove the observer as follows:
observer.unsubscribe()
Advanced Topics
EmitterKit provides more advanced features and options to suit complex use cases. Here are some additional topics you may find useful:
- Throttling events: Control the frequency of event dispatch using throttling techniques to improve performance and avoid overload.
- Event priorities: Assign priorities to events, ensuring that higher priority events are handled first.
- Thread safety: Safely handle events across multiple threads or queues to prevent race conditions and data inconsistencies.
- Event propagation: Implement event hierarchies and allow events to propagate to parent or child events as needed.
Conclusion
EmitterKit provides a comprehensive event handling and observation framework for iOS and macOS development. Its simplicity and efficiency make it an ideal choice for managing events, notifications, and data changes in your application. With EmitterKit, you can streamline your app’s architecture and improve its maintainability. Start using EmitterKit today and experience the power of event-driven programming.