Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Easily monitor async sequences using Swift concurrency

License

NotificationsYou must be signed in to change notification settings

samsonjs/AsyncMonitor

Repository files navigation

0 dependencies!

Overview

AsyncMonitor is a Swift library that provides a simple and easy-to-use way to manage Swift concurrencyTasks that observe async sequences. TheAsyncMonitor class allows you to create tasks that observe streams and call the given closure with each new value, and optionally also with a context parameter so you don't have to manage its lifetime.

It uses a SwiftTask to ensure that all resources are properly cleaned up when theAsyncMonitor is cancelled or deallocated.

That's it. It's pretty trivial. I just got tired of writing it over and over, mainly for notifications. You still have to map yourNotifications to something sendable, which brings me to another point. This package pairs nicely withNotificationSmuggler for a complete notification handling system in the Swift 6 concurrency world.

Usage

The simplest example uses a closure that receives the notification. The closure is async so you can await in there if you need to.

import AsyncMonitorclassSimplestVersion{letcancellable=NotificationCenter.default.notifications(named:.NSCalendarDayChanged).map(\.name).monitor{ _inprint("The date is now\(Date.now)")}}

This example uses the context parameter to avoid reference cycles withself.

finalclassWithContext:Sendable{nonisolated(unsafe)var cancellables=Set<AnyAsyncCancellable>()init(){NotificationCenter.default.notifications(named:.NSCalendarDayChanged).map(\.name).monitor(context:self){ _self, _in                _self.dayChanged()}.store(in:&cancellables)}func dayChanged(){print("The date is now\(Date.now)")}}

Combine

Working with Combine publishers is trivial thanks toAnyPublisher.values.

@preconcurrencyimport CombineclassCombineExample{varcancellables:Set<AnyAsyncCancellable>=[]init(){Timer.publish(every:1.0, on:.main, in:.common).autoconnect().values.monitor{ dateinprint("Timer fired at\(date)")}.store(in:&cancellables)}}

Key-Value Observing (KVO) extension

When you need to observe an object that usesKVO there's an extension method you can use to monitor it:

classKVOExample{varcancellables:Set<AnyAsyncCancellable>=[]init(){letprogress=Progress(totalUnitCount:42)        progress.monitorValues(for: \.fractionCompleted, options:[.initial,.new]){ fractioninprint("Progress is\(fraction.formatted(.percent))%")}.store(in:&cancellables)}}

Installation

The only way to install this package is with Swift Package Manager (SPM). Pleasefile a new issue or submit a pull-request if you want to use something else.

Supported Platforms

This package is supported on iOS 17.0+ and macOS 14.0+.

Xcode

When you're integrating this into an app with Xcode then go to your project's Package Dependencies and enter the URLhttps://github.com/samsonjs/AsyncMonitor and then go through the usual flow for adding packages.

Swift Package Manager (SPM)

When you're integrating this using SPM on its own then add this to the list of dependencies your Package.swift file:

.package(url:"https://github.com/samsonjs/AsyncMonitor.git",.upToNextMajor(from:"0.3.1"))

and then add"AsyncMonitor" to the list of dependencies in your target as well.

License

Copyright © 2025Sami Samhurisami@samhuri.net. Released under the terms of theMIT License.

About

Easily monitor async sequences using Swift concurrency

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp