GLPubSub is a lightweight framework for implementing a publish-subscribe pattern in your iOS and macOS applications. It provides a simple and efficient way to decouple components and promote loose coupling and high cohesion within your codebase.
Features
- Lightweight: GLPubSub has a minimal footprint and is designed to have negligible impact on the performance of your application.
- Simple Interface: The framework offers a straightforward API for publishing and subscribing to events, making it easy to integrate into your project.
- Thread-Safe: GLPubSub ensures thread safety by default, allowing you to safely publish and subscribe to events from multiple threads.
- Flexibility: The framework supports using custom objects as event payloads, giving you the freedom to transmit any data you need within your application.
- Decoupling: By adopting a publish-subscribe pattern, GLPubSub helps reduce dependencies between components, making your code more maintainable and testable.
- Documentation: Detailed documentation and comprehensive examples are provided to facilitate easy integration and usage of the library in your projects.
Installation
GLPubSub can be easily installed using the CocoaPods dependency manager. Simply add the following line to your project’s Podfile:
pod 'GLPubSub', '~> 2.0'
Make sure to run pod install
to fetch the latest version of the framework.
Usage
To start using GLPubSub in your project, you need to follow a few simple steps:
Step 1: Import Framework
Import the GLPubSub framework into your source code file:
import GLPubSub
Step 2: Publish Events
To publish an event, use the GLPubSub.publish(_:payload:)
method. Specify the event name and an optional payload object:
GLPubSub.publish("eventName", payload: dataObject)
Step 3: Subscribe to Events
To subscribe to an event, use the GLPubSub.subscribe(_:queue:_:)
method. Provide the event name, a queue on which to execute the callback, and the closure to be executed when the event occurs:
GLPubSub.subscribe("eventName", queue: .main) { payload in
// Handle event with the provided payload
}
Example
Here’s a simple example demonstrating the usage of GLPubSub:
// Publish an event
GLPubSub.publish("userLoggedIn", payload: currentUser)
// Subscribe to the event
GLPubSub.subscribe("userLoggedIn", queue: .main) { user in
// Update UI or perform any action upon user login
}
Documentation
For more detailed usage instructions and comprehensive documentation, refer to the official GLPubSub GitHub repository.
License
GLPubSub is released under the MIT License. See the LICENSE file for more details.