Giter Site home page Giter Site logo

thumbtack / swiftrunonce Goto Github PK

View Code? Open in Web Editor NEW
12.0 72.0 0.0 44 KB

A declarative, thread safe, and reentrant way to define code that should only execute at most once over the lifetime of an object.

License: Apache License 2.0

Swift 100.00%
swift one-time utility

swiftrunonce's Introduction

SwiftRunOnce

SwiftRunOnce allows a developer to mark a block of logic as "one-time" code – code that will execute at most once over the lifetime of another object, no matter how many times that block of logic gets invoked.

SwiftRunOnce was designed to satisfy five requirements:

  • Robust implementation: A block of code to declared to be one-time code must be just that. There may be no circumstance in which the block of code is allowed to execute a second time over the lifetime of the controlling object.
  • Declarative usage: One must be able to make a block of preexisting code into one-time code simply by decorating with the attribute.
  • Thread safety: Even in multithreaded environments, a block of code marked with the one-time attribute must execute at most once.
  • Reentrancy: A block of one-time code must be able to invoke another block of one-time code without deadlocking or invalidating the one-time constraint.
  • Minimalist implementation: Adding a block of one-time code requires no additional storage or maintenance of state within the calling code. All state management is hidden and protected.

Installation

Swift Package Manager

Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

Xcode 11+ is required to build SwiftRunOnce using Swift Package Manager.

To integrate SwiftRunOnce into your Xcode project using Swift Package Manager, add it to the dependencies value of your Package.swift:

dependencies: [
    .package(url: "https://github.com/thumbtack/SwiftRunOnce.git", .upToNextMajor(from: "0.2.2"))
]

Usage

SwiftRunOnce exposes a single function:

forLifetime(
    of object: AnyObject,
    _ closure: () -> Void,
    line: Int = #line,
    column: Int = #column,
    fileId: String = #fileID
)

In actual usage, one should always use the default arguments for line, column, and fileId (the behavior when arguments are passed into these parameters is undefined). An example usage might look something like:

func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated: animated)

    // Only fire on initial view, not on back navigation
    RunOnce.forLifetime(of: self) {
        trackView()
    }
}

Since most usages of forLifetime(of::) will pass self as the object, SwiftRunOnce adds a convenience extension to NSObject that adds a little more syntactic sugar to this common case. Using that extension, the above code simply becomes:

func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated: animated)

    // Only fire on initial view, not on back navigation
    runOnce {
        trackView()
    }
}

License

SwiftRunOnce is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

swiftrunonce's People

Contributors

dwroth avatar muhieddine-el-kaissi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.