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

@livelybone/singleton

v1.3.6

Published

A util of singleton wrapping

Downloads

73

Readme

@livelybone/singleton

NPM Version Download Month gzip with dependencies: 1kb typescript pkg.module

pkg.module supported, which means that you can apply tree-shaking in you project

A util of singleton wrapping

repository

https://github.com/livelybone/singleton.git

Demo

https://github.com/livelybone/singleton#readme

Installation

npm i -S @livelybone/singleton

Global name

Singleton

Usage

import * as Singleton from '@livelybone/singleton'

Use in html, see what your can use in CDN: unpkg

<-- use what you want -->
<script src="https://unpkg.com/@livelybone/singleton/lib/umd/<--module-->.js"></script>

Interface

declare type Fn<T = any> = () => T
/**
 * 每个单例的 id
 *
 * The id of a singleton
 * */
declare type ID = string | number

export interface PromiseOnPendingOptions {
  id?: ID
  /**
   * 用于延迟 Promise 实例删除,
   * 如果为 undefined,则在 promise 状态改变之后立即删除
   *
   * Used to delay the deletion of Promise instance,
   * if it is undefined, the promise will be deleted immediately after the state changed
   *
   * This option will be deprecated in next version
   * */
  delayTime?: number
  /**
   * 同 delayTime
   * 推荐使用这个属性
   *
   * As same as delayTime
   * */
  cacheTime?: number
}

/**
 * @deprecated 这个方法使用多了会导致内存泄漏,建议使用 singleton 方法代替
 * @desc 返回 id 对应的一个单例对象
 *
 *       Return a singleton of an object(such as Promise, Function, Object...) corresponding to the id.
 * */
declare function singletonObj<T extends any>(id: ID, defaultValue?: () => T): T

/**
 * @desc 返回 id 对应的一个单例对象,这个方法应当配合返回的 delete 方法一起使用,否则使用多了会导致内存泄漏
 *
 *       Return a singleton of an object(such as Promise, Function, Object...) corresponding to the id.
 *       This method will cause OOM if it's used too much without calling `delete`.
 * */
declare function singleton<T extends any>(
  id: ID,
  defaultValue?: () => T,
): {
  value: T
  delete(): void
}

/**
 * @desc 保证一个 id 对应的 promise 在同一时间只存在一个,
 *       并且生成 promise 的函数在 promise pending 状态的时间段内只执行一次,
 *       这个方法可以用来减少同一时间的多余请求
 *
 *       Ensure that only one promise corresponding to the id exists at the same time,
 *       and the function that generates the promise executes only once
 *       during the period of promise pending.
 *       This method can be used to reduce redundant requests at the same time
 * */
declare function promiseOnPending<P extends PromiseLike<any>>(
  proFn: () => P,
  options: PromiseOnPendingOptions,
): P

/**
 * @desc 封装 setInterval 函数,
 *       保证同一个 id 对应的计时器只有一个在运行,
 *       并且返回一个清除计时器的函数,方便随时终止计时器
 *
 *       A wrapper of the setInterval function,
 *       make sure only one timer for the same id is running,
 *       and returns a function to clear the timer so it can be terminated at any time
 * */
declare function runInterval(id: ID, createFn: Fn): () => void

/**
 * @desc 保证传入的函数在程序的运行期间只运行一次
 *
 *       Ensure that the incoming function runs only once during the run time of the program
 * */
declare function onceRun(fn: Fn, id?: any): void

export { onceRun, promiseOnPending, runInterval, singleton, singletonObj }