https://github.com/mtj0928/typed-notifications
type-safe NotificationCenter
https://github.com/mtj0928/typed-notifications
Last synced: 3 months ago
JSON representation
type-safe NotificationCenter
- Host: GitHub
- URL: https://github.com/mtj0928/typed-notifications
- Owner: mtj0928
- License: mit
- Created: 2023-09-09T04:55:29.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-06T13:43:05.000Z (3 months ago)
- Last Synced: 2025-04-06T14:37:06.615Z (3 months ago)
- Language: Swift
- Homepage: https://mtj0928.github.io/typed-notifications/documentation/typednotifications/
- Size: 61.5 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# typed-notifications
This library attaches type-information to NotificationCenter.## Example
You can post and observe notifications in a type-safe manner.
```swift
TypedNotificationCenter.default
.publisher(for: .userNameUpdate, object: user)
.sink { notification in
// Notifications can be received in a type safe manner.
let storage: UserNameUpdateNotificationStorage = notification.storage
let user: User? = notification.object
// ...
}extension TypedNotificationDefinition {
@Notification
static var userNameUpdate: TypedNotificationDefinition
}@UserInfoRepresentable
struct UserNameUpdateNotificationStorage {
let oldName: String
let newName: String
}
```## How to Use
Define a notification and how to encode/decode the userInfo in `TypedNotificationDefinition`.
```swift
extension TypedNotificationDefinition {
static var userNameWillUpdate: TypedNotificationDefinition {
.init(name: "userWillUpdate") { newName in
["newName": newName]
} decode: { userInfo in
userInfo?["newName"] as? String ?? ""
}
}
}
```And then, you can post/observe the notifications in type safe manner.
```swift
// [Post]
// Notifications can be posted in a type safe manner.
let newName: String = ...
let user: User = ...
TypedNotificationCenter.default.post(.userNameWillUpdate, storage: newName, object: user)// [Observation]
TypedNotificationCenter.default.publisher(for: .userNameWillUpdate, object: user)
.sink { notification in
// Notifications can be received in a type safe manner.
let newName = notification.storage
let user: User? = notification.object
// ...
}
```### Notification macro
You can use `@Notification` macro, if `@UserInforRepresentable` macro is attached to your type.
```swift
extension TypedNotificationDefinition {
@Notification
static var userNameUpdate: TypedNotificationDefinition
}@UserInfoRepresentable
struct UserNameUpdateNotificationStorage {
let oldName: String
let newName: String
}
```## Pre-defined Notifications
This repository contains frequent system notifications.
- UIKit
- [UIApplication](Sources/TypedNotifications/UIKit/UIApplication.swift)
- [UIScene](Sources/TypedNotifications/UIKit/UIScene.swift)
- [UIResponder](Sources/TypedNotifications/UIKit/UIResponder.swift)
- Core Data
- [NSManagedObjectContext](Sources/TypedNotifications/CoreData/NSManagedObjectContext.swift)Your PR adding new notifications is appreciated. Feel free to make a new PR.