Giter Site home page Giter Site logo

tasksequencer's Introduction

TaskSequencer

TaskSequencer is a Swift package designed to manage and execute a sequence of tasks, both synchronous and asynchronous, with specified delays. This tool is ideal for scenarios where tasks need to be performed in a specific order, with controlled timing โ€” such as UI animations, sequential network requests, or chained operations with dependencies.

The code in this package(including this README.md with exception to this note) is entirely generated by ChatGPT 4

Here is the full prompting process

Features

  • Manage a sequence of synchronous and asynchronous tasks.
  • Specify delays for each task individually.
  • Control task execution with pause, resume, and cancel functionalities.
  • Add, remove, and reset tasks dynamically.

Installation

Install like any other Swift package.

Usage

Basic Example

    import TaskSequencer
    
    let sequencer = TaskSequencer()
    
    // Adding a synchronous task
    sequencer.addTaskWithDelay(id: "task1", delay: 2) {
        print("This is a synchronous task")
    }
    
    // Adding an asynchronous task
    sequencer.addTaskWithDelay(id: "task2", delay: 1) {
        await someAsyncOperation()
    }
    
    // Start executing tasks
    sequencer.start()

Removing a Task

    sequencer.removeTaskByID(id: "task1")

Controlling Execution

    // Pause the sequence
    sequencer.pause()
    
    // Resume the sequence
    sequencer.resume()
    
    // Cancel all tasks
    sequencer.cancel()
    
    // Reset the sequencer
    sequencer.reset()

API Reference

  • addTaskWithDelay(id:delay:task:): Add a synchronous task with a specified delay.
  • addTaskWithDelay(id:delay:task:): Add an asynchronous task with a specified delay.
  • removeTaskByID(id:): Remove a task from the sequence using its ID.
  • start(): Start or resume executing the tasks.
  • pause(): Pause the task execution.
  • resume(): Resume the task execution.
  • cancel(): Cancel all tasks in the sequence.
  • reset(): Cancel all tasks and clear the sequence.

Considerations and Gotchas

  • Thread Blocking: DelayedOperation uses Thread.sleep, which blocks the thread on which it is running. Ensure this aligns with your performance and concurrency needs.
  • Main Thread: Operations are executed on background threads and do not block the main thread. However, ensure tasks added to the sequencer, especially UI updates, are dispatched on the main thread if necessary.
  • Error Handling: The current implementation does not explicitly handle errors in task execution. Consider implementing error handling based on your specific requirements.
  • Resource Management: Be mindful of resource usage, especially with AsyncDelayedOperation, when scheduling a large number of tasks or tasks with long-running operations.

License

MIT License

tasksequencer's People

Contributors

mrtksn avatar

Stargazers

Tim Kersey avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.