https://github.com/ladislas/swifteventcenter
A simple and easy to use event manager
https://github.com/ladislas/swifteventcenter
cocoapods event notification nsnotificationcenter observer-pattern spm swift swift-package-manager
Last synced: 7 months ago
JSON representation
A simple and easy to use event manager
- Host: GitHub
- URL: https://github.com/ladislas/swifteventcenter
- Owner: ladislas
- License: apache-2.0
- Created: 2018-09-25T13:09:08.000Z (about 7 years ago)
- Default Branch: develop
- Last Pushed: 2018-10-17T11:21:27.000Z (about 7 years ago)
- Last Synced: 2025-02-28T15:11:07.938Z (8 months ago)
- Topics: cocoapods, event, notification, nsnotificationcenter, observer-pattern, spm, swift, swift-package-manager
- Language: Swift
- Homepage: https://ladislas.github.io/SwiftEventCenter/
- Size: 181 KB
- Stars: 3
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Swift Event Center
[](swift.org)
[](https://github.com/apple/swift-package-manager)
[](https://cocoapods.org/pods/SwiftEventCenter)
[](https://travis-ci.org/ladislas/SwiftEventCenter)
[](https://sonarcloud.io/dashboard?id=ladislas_SwiftEventCenter)
[](https://sonarcloud.io/dashboard?id=ladislas_SwiftEventCenter)
[](https://github.com/ladislas/SwiftEventCenter/blob/master/LICENSE)## 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 CenterThe 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:
- [x] - add observer for `Event.Name` (a.k.a. `Notification.Name`)
- [x] - specify a name for added observers so that they can be removed independently
- [x] - remove all observers for a specific `Event.Name`
- [x] - remove one observer with a specific `name` for a specific `Event.Name`
- [x] - 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`
```swift
.package(url: "https://github.com/ladislas/SwiftEventCenter", from: "1.1.1")
```## Usage
`Event` is just a `typealias` for `Notification`.
See [docs](./docs), [Examples](./Examples) & [Tests](./Tests) for more information.
```swift
import EventCenterlet 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:
- **Ladislas de Toldi** - [ladislas](https://github.com/ladislas)
## 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](https://github.com/StephenHaney/Swift-Custom-Events). It was then improved by [Robin Walter](https://github.com/robinwalterfit)'s [Swift-Events](https://github.com/robinwalterfit/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](https://github.com/StephenHaney) & [Robin Walter](https://github.com/robinwalterfit) is under MIT.*