1.0.0 • Published 8 years ago

throttle-mru v1.0.0

Weekly downloads
1
License
MIT
Repository
-
Last release
8 years ago

throttle-mru

js-standard-style

Throttles the most recently used value

Install

$ npm install throttle-mru

Usage

'use strict'

var Throttle = require('throttle-mru')

var t = Throttle(10000) // Throttle for maximum of 10 seconds
var count = 0
var value = 'foo'
var handle

var f = function () {
  if (count >= 2) {
    clearInterval(handle)
    console.log('Value is %s', !t.put(value) ? value : 'still blocked')
  } else if(t.put(value)) {
    count += 1
    console.error('message blocked')
  } else {
    console.log(value) // will only call the first time
  }
}

handle = setInterval(f, 4000) // Query every 4 seconds

Ideal for instances where you want to throttle spam

API

Throttle(maxAge, maxSize)

maxAge refers to the amount of milliseconds a value should be sitting in the cache, default is 25 seconds. maxSize refers to the amount of values that can be stored in the cache, defaults to 100 values.

.put(key)

key refers to any value needing to be throttled. Returns true if value exists and is being to set to most recently used and false if it no longer exists.

.exists(key)

key refers to any value queried for existance in cache. Returns true if value exists and false if not.

.remove(key)

key refers to any value needing to be completely removed from the cache.

License

MIT