Giter Club home page Giter Club logo

gate's Introduction

Gate

GitHub code size in bytes GitHub build results

Swift Concurrency 'gate' type to control forward progress of async tasks.

A gate can be opened and closed, and while closed it cannot be entered. Async tasks can pass through the gate at appropriate point(s) in their execution (chosen by them, like checking for task cancellation). If the gate is closed at the time they try to enter it, they will pause and wait for it to open before proceeding.

The gate can be opened or closed from any code (not just async contexts).

An example use is in SwiftUI where you have background task(s) (e.g. from the task view modifier) that you want to pause & resume in response to state changes:

struct Example: View {
    let frames: NSImage

    @Binding var animate: Bool {
        didSet {
            guard animate != oldValue else { return }

            if animate {
                self.animationGate.open()
            } else {
                self.animationGate.close()
            }
        }
    }
    
    @State private var animationGate = Gate(initiallyOpen: true)

    @State var currentFrameIndex: Int = 0
    
    var body: some View {
        Image(nsImage: self.frames[self.currentFrameIndex])
            .task {
                while let _ = try? await self.animationGate.enter() {
                    self.currentFrameIndex = (self.currentFrameIndex + 1) % self.frames.count
                    try? await Task.sleep(for: .seconds(1) / 60)
                }
            }
    }
}

Note that enter will always throw if the task is cancelled, so you don't need to manually check for cancellation (e.g. Task.checkCancellation / Task.isCancelled) if you are using Gate entry like in the above example.

Thanks to…

This was inspired by (and based in part on) Gwendal Roué's Semaphore package.

gate's People

Contributors

wadetregaskis avatar

Stargazers

harry lachenmayer avatar Ricardo Magalhães avatar Dan Murfin avatar Sunghyun Kim avatar Bob Edmonston avatar An Tran avatar Gwendal Roué avatar Laszlo Teveli avatar

Watchers

 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.