npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

limited-use

1.0.4 • Public • Published

limited-use

Build Status Coverage Status Inline Docs dependencies Status npm Version

A lightweight abstraction for functions that should only be invoked a limited number of times.

Usage

import {LimitedUse} from 'limited-use'
 
const usable = new LimitedUse(() => console.log('take a nap'), 1)
 
usable.use() // 'take a nap'
usable.use() // no effect

With multiple LimitedUses, use them as a single group with CollectiveUse:

import {LimitedUse, CollectiveUse} from 'limited-use'
 
const usables = new CollectiveUse()
usables.add(
  new LimitedUse(() => console.log('eat a donut')),
  new LimitedUse(x => console.log(`think about her ${x}`), 365)
)
 
usables.use('smile') // 'eat a donut', 'think about her smile'
usables.use('touch') // 'think about her touch'
usables.disuse()
usables.use('desperately') // no effect

Installation

Install using yarn:

$ yarn add limited-use

Install using npm:

$ npm i limited-use

API

LimitedUse

  • constructor(callback, limit)
    • callback - The function to limit in use. Any arguments passed to use() will be passed to this function.
    • limit - The maximum number of times the call can be called. Optional, default 1.
  • .use(...args)
    • Calls callback(...args) and returns an immediately resolved promise of its return value.
  • .disuse()
    • After this function is called, any future calls to use() will have no effect.
  • .isUsable
    • True if the number of uses so far is less than limit.
  • .isDisused
    • True if the number of uses so far is at least equal to limit.

CollectiveUse

  • constructor(...usables)
    • usables - Any number of objects that have .use() and .disuse() methods.
  • .use(...args)
    • Calls use(...args) (to be executed asynchronously) on all usables added to this collection. Returns a promise that resolves to an array of each call's return value.
  • .useSync(...args)
    • Calls use(...args) synchronously on all usables added to this collection. Returns an array of each call's return value.
  • .disuse()
    • After this function is called, this collection is marked as disused and disuse() is called on all usables that had been added.
  • .isUsable
    • True if disuse() has not yet been called on this collection and if at least one member is usable.
  • .isDisused
    • True if disuse() has been called on this collection.
  • .add(...usables)
    • Adds usables (Any number of objects with .use() and .disuse()) to the collection.
  • .remove(usable)
    • Removes usable from the collection. Returns true upon success.
  • .clear()
    • Empties the collection.

install

npm i limited-use

Downloadsweekly downloads

2

version

1.0.4

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability