ManualResume

main

A Swift package for asynchronously pausing and resuming code execution with support for cancellation, using tagged continuations.
mrtksn/ManualResume

ManualResume

A Swift package that provides a flexible way to manage manual pause and resume in asynchronous Swift code using tags. ManualResume allows for pausing execution until a condition is met or an operation is canceled. This is particularly useful in scenarios where asynchronous tasks need to be coordinated based on certain events or states.

This Swift Package was entirely created on ChatGPT, including this README(except for this note).

Here is the full chat

Features

  • Pause and resume asynchronous execution using tags.
  • Support for throwing continuations, allowing cancellation of tasks.
  • Thread-safe implementation using Swift's concurrency model.

Installation

Swift Package Manager

You can add ManualResume to your project via Swift Package Manager. Add the following to your Package.swift file:

    dependencies: [
        .package(url: "https://github.com/mrtksn/ManualResume.git", from: "1.0.0")
    ]

Usage

Basic Usage

Import ManualResume in your Swift file.

    import ManualResume

Use ManualResume.shared.isReady(_:) to pause execution, and ManualResume.shared.resume(_:) to resume it:

    Task {
        await ManualResume.shared.isReady("loadingData")
        // This code will execute after 'resume("loadingData")' is called.
        print("Data loading resumed")
    }
    
    // Resume the execution
    ManualResume.shared.resume("loadingData")

Cancellation

Use ManualResume.shared.cancel(_:) to cancel a task:

    Task {
        do {
            try await ManualResume.shared.isReady("loadingData")
            print("Data loading resumed")
        } catch {
            print("Data loading was cancelled")
        }
    }
    
    // Cancel the execution
    ManualResume.shared.cancel("loadingData")

License

ManualResume is released under the MIT License. See the LICENSE file for more details.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Oct 19 2024 06:14:22 GMT-0900 (Hawaii-Aleutian Daylight Time)