npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lazy-singleton

v1.0.0

Published

Create lazy initialized singletons

Downloads

724

Readme

lazy-singleton Build Status codecov

Create lazy initialized singletons

Install

$ npm install lazy-singleton

Usage

const lazySingleton = require('lazy-singleton');

const lazyDependency = lazySingleton(someExpensiveFunction)(...argsToPass);

// later - the expensive function won't be called until here
lazyDependency().doSomething();


// wrapped function will be called with `new`
const lazyInstance = new lazySingleton.Sync(SomeConstructor)(...argsToPass);
lazyInstance().memberFunction();

// it can be used as a lazy require wrapper - note we aren't chaining (...args) on the initial call.
const lazyRequire = require('lazy-singleton')(require);
const _ = lazyRequire('lodash');
const lazyFoo = lazyRequire('foo');
_().isNumber(3);

This is similar to, but different from, the popular once package, in that both ensure your wrapped function is only called once. With once the first caller determines which arguments are passed, and all future arguments are ignored. With this library, args are determined at creation time.

API

Note: All the API examples below show the common usage of two chained function calls. This is so you can create a generator that creates multiple lazy singletons with different args passed to the function (see the lazyRequire example above).

lazySingleton(fn)(...args)

fn

Type: function

The function to be called.

args

Type: function

The args for to be passed to the lazily called function.

&npbsp;

Note: If lazySingleton(fn) is called with new, then fn will also be called with new if/when it is invoked. Useful for classes.

lazySingleton.sync(fn)(...args) and lazySingleton.Sync(fn)(...args)

These are both just an alias for the main function, the capitalized .Sync is useful when using new to prevent your linter from complaining.

lazySingleton.promise(fn)(...args)

This is just a wrapper allowing easy promise creation. fn will be called resolve, and reject as it's first two arguments.

lazySingleton.promise((resolve, reject) => resolve('foo'))();

// is equivalent to:

lazySingleton(() => new Promise((resolve, reject) => resolve('foo')))();

Note that you can still pass additional ...args to your promise executor. Any extra args are just appended after resolve and reject.

lazySingleton.promise(fn)('foo', 'bar');
// fn will be called with the extra args:
fn(resolve, reject, 'foo', 'bar');

Finally, the returned function will also accept a node style callback as it's first argument, allowing flexibility:

const lazy = lazySingleton.promise(promiseExecutor)();

lazy().then(doSomething);
//or
lazy((err, result) => {/* doSomething */});

lazySingleton.callback(fn)(...args)

Another sugar function. When lazily initialized, a node style callback will be appended to ..args, allowing you to leverage node style libraries:

const fileContents = lazySingleton.callback(fs.readFile)('some/path/to/file.txt', 'utf8');

// can be used as a promise
fileContents().then(contents => console.log(contents));
// or a node style callback
fileContents((err, contents) => console.log(contents))

License

MIT © James Talmage