Coder Social home page Coder Social logo

sync-once's Introduction

sync-once

sync-once is similar to sync.Once of the standard library.

It also has struct Once but has two additional methods DoForce() and Reset().

why

While writing a web application I needed to reload configurations that were calculated using sync.Once.

But sync.Once provide a single method Do() that executes the function only once.

To get around this I wrote this package.

usage

import (
    sync "github.com/m-murad/sync-once"
)

func main() {
    o := new(sync.Once)
    
    // This will work similar to the Once.Do(f) method of the sync package. The function f is only called once
    o.Do(loadConfig())

    // To call the function either for the first time or again you need to use the sync.DoForce() method
    // This will execute irrespective of weather o.Do() was called earlier or not and mark o (Once) as done.
    // Call to o.Do() after o.DoForce() will not execute the function.
    o.DoForce(loadConfig())

    // To reset o (sync.Once) you need to call the Reset() method.
    // This will mark o as not executed but will no call the Do() method. You need to call Do() or DoForce() after this.
    // Calls to Do() and DoForce() after this will work as described above.
    o.Reset()

}

// load config from a static file or any other operation that is usually performed only once
func loadConfig() {
    // Do the work here
}

sync-once's People

Contributors

m-murad avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

admpub isgasho

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.