makesingleton
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

makeSingleton

allow you to make a single instance of your function or class. It's a good practice to instanciate for module when you call it instead of instanciate when you import it.

function makeSingleton(createInstance) {
  let instance = undefined;

  return () => {
    if (!instance) {
      instance = createInstance();
    }
    return instance;
  };
}

How to use

Bad Practice

// logger.js
module.exports = createLogger("my-api-name");

// in other file
const logger = require("./logger.js"); // create instance of logger

function main() {
  logger.log("hello world");
  logger.log("hello world 2"); // same instance of logger
}

Good Practice Practice

// logger.js
module.exports = () => createLogger("my-api-name");

// in other file
const logger = require("./logger.js"); // no instance created - no side effect on import :)

function main() {
  logger().log("hello world"); // create instance of logger
  logger().log("hello world 2"); // create a other instance of logger
}

Best Practice Practice

// logger.js
module.exports = makeSingleton(() => createLogger("my-api-name"));

// in other file
const logger = require("./logger.js"); // no instance created - no side effect on import :)

function main() {
  logger().log("hello world"); // create instance of logger
  logger().log("hello world 2"); // same instance of logger
}

Readme

Keywords

none

Package Sidebar

Install

npm i makesingleton

Weekly Downloads

0

Version

1.0.1

License

ISC

Unpacked Size

3.94 kB

Total Files

7

Last publish

Collaborators

  • superchauzette