1.0.4 • Published 7 years ago

limited-use v1.0.4

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

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.
1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago