Giter Site home page Giter Site logo

once's Introduction

Build Status release install platform license

Once allows you to manage the number of executions of a task using an intuitive API.

Highlight

  • Safe
  • Efficient
  • Persistent

Usage

Token

Token records the number of times the task is executed in memory, which allows the task to be executed only once during the entire lifetime of the app.

You can think of it as an alternative to dispatch_once in OC:

static dispatch_once_t token;
dispatch_once(&token, ^{
    // do something only once
});

The swift code using Token is as follows:

let token = Token.makeStatic()
token.do {
    // do something only once
}

Or, more simple:

Token.do {
    // do something only once
}

You can also don't use static:

class Manager {
    let loadToken = Token.make()

    func ensureLoad() {
        loadToken.do {
            // do something only once per manager.
        }
    }
}

PersistentToken

Unlike run, do will persist the execution history of the task (using UserDefault).

PersistentToken determines whether this task should be executed based on Scope and TimesPredicate.

Scope

Scope represents a time range, it is an enum:

  • .install: from app installation
  • .version: from app update
  • .session: from app launch
  • .since(let since): from since(Date)
  • .until(let until): to until(Date)

TimesPredicate

TimesPredicate represents a range of times.

let p0 = TimesPredicate.equalTo(1)
let p1 = TimesPredicate.lessThan(1)
let p2 = TimesPredicate.moreThan(1)
let p3 = TimesPredicate.lessThanOrEqualTo(1)
let p4 = TimesPredicate.moreThanOrEqualTo(1)

do

You can use Scope and TimesPredicate to make any plan you want, and, yes, it is thread-safe.

let token = PersistentToken.make("showTutorial")
token.do(in: .version, if: .equalTo(0)) {
    app.showTutorial()
}

// or
let later = 2.days.later
token.do(in: .until(later), if: .lessThan(5)) {
    app.showTutorial()
}

done

Sometimes your asynchronous task may fail. You don't want to mark the failed task as done. You can:

let token = PersistentToken.make("showAD")
token.do(in: .install, if: .equalTo(0)) { task in
    networkService.fetchAD { result in
        if result.isSuccess {
            showAD(result)
            task.done()
        }
    }
}

But at this time, the judgment is no longer absolutely safe - if there are multiple threads checking the token at the same time, but it should rarely happen, 😉.

reset

You can also clear the execution history of a task:

token.reset()

It is also permissible to clear the execution history of all tasks, but at your own risk:

PersistentToken.resetAll()

Installation

CocoaPods

pod 'Once', '~> 1.0.0'

Carthage

github "luoxiu/Once" ~> 1.0.0

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/luoxiu/Once", .upToNextMinor(from: "1.0.0"))
]

Contributing

Encounter a bug? want more features? Feel free to open an issue or submit a pr directly!

once's People

Contributors

luoxiu avatar

Stargazers

 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

Watchers

 avatar  avatar  avatar

once's Issues

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.