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

create-singleton

v0.0.4

Published

An abstracted-away best practice way to construct singletons

Downloads

49

Readme

createSingleton

An abstracted-away best practice way to construct singletons.

The Singleton pattern is thus known because it restricts instantiation of a class to a single object. Classically, the Singleton pattern can be implemented by creating a class with a method that creates a new instance of the class if one doesn't exist. In the event of an instance already existing, it simply returns a reference to that object.

Singletons differ from static classes (or objects) as we can delay their initialization, generally because they require some information that may not be available during initialization time. They don't provide a way for code that is unaware of a previous reference to them to easily retrieve them. This is because it is neither the object or "class" that's returned by a Singleton, it's a structure. Think of how closured variables aren't actually closures - the function scope that provides the closure is the closure.

In JavaScript, Singletons serve as a shared resource namespace which isolate implementation code from the global namespace so as to provide a single point of access for functions.

function createSingleton(cb) {
  var args = Array.prototype.slice.call(arguments, 1);
  var name = '__' + (cb.name || Math.random());

  return function () {
    if (arguments.callee[name]) {
      return arguments.callee[name];
    }
    arguments.callee[name] = this;

    cb.apply(this, args.length ? args : arguments);
  };
};

That's all! Feel free to incorporate it back into any of your JavaScript programs.

Installation

NodeJS

npm install --save create-singleton

or

Browser

Just include the createSingleton.js or createSingleton.min.js. It's umdjs compatible :)

Usage

var createSingleton = require('create-singleton');

var mySingleton = createSingleton(function mySingleton() {
  // Describes my singleton class
  var myPrivateVariable = 5;

  this.myPublicFunction = function() {
    // something cool happens
  };
});

// Contrust the singleton class
var myInstance1 = new mySingleton();

// ~ Later on ~

var myInstance2 = new mySingleton();
// This doesn't create a new instance of mySingleton but instead returns the same one
// So myInstance1 === myInstance2 is true

Another Example

// File: a.js
var createSingleton = require('create-singleton');

function Singleton() {
  // Do something
}

Singleton.prototype.myPublicFunction = function () {};

module.exports = createSingleton(Singleton);

// .....

// File b.js
var mySingleton = require('./a.js');

// Contrust the singleton class
var myInstance1 = new mySingleton();

// ~ Later on ~

var myInstance2 = new mySingleton();
// This doesn't create a new instance of mySingleton but instead returns the same one
// So myInstance1 === myInstance2 is true

License

(The MIT License)

Copyright (c) 2013-2016 Tarun Chaudhry <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.