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

trakt.tv-queued

v3.0.0

Published

Caching plugin for trakt.tv

Downloads

21

Readme

trakt.tv-queued

JavaScript Style Guide NPM NPM

By using this plugin you can forget about rate-limiting your requests. Simply set a concurrency and delay and the plugin will take care of everything.

Moreover, if you enable trakt.tv-cached the functionality of the two plugins can be combined.

At the moment trakt.tv-queued depends on any version of Node.js that supports async/await.

Basic usage

Install the plugin as a normal dependency:

$ npm i trakt.tv-queued --save

When you create a trakt.tv instance, pass this plugin to the constructor and specify any options:

let Trakt = require('trakt.tv')
let trakt = new Trakt({
  client_id: 'YYY',
  client_secret: 'ZZZ',
  plugins: {
    queued: require('trakt.tv-queued')
  },
  options: {
    queued: {
      concurrency: 2,
      delay: 1
    }
  }
})

Make sure the plugin itself and its options are in a field called queued.

concurrency sets how many calls can be in execution during the period defined by delay (in seconds). So, concurrency: 2 and delay: 1 means "no more than 2 calls at the same time in the space of 1 second". If an API call requires more than one second to complete, a new one will be fired as soon as it completes; if it requires only half a second, a half second delay will be added before firing the next call.

At this time there's no way to set a timeout. If a call hangs, the queue will hang indefinitely as well. This should never happen.

To use the plugin, simply add queued. before the method you would normally call directly on the main module:

let data = await trakt.queued.seasons.season({id: 'game-of-thrones', season: 4})
console.log('API CALL COMPLETED: ' + data.length + ' "Game of Thrones" episodes fetched'))

concurrency and delay can be reconfigured on the fly. This would increase concurrency to 4:

trakt.queued.reconfigure({concurrency: 4, delay: 1})

You can shut down everything, clearing the queue completely, by calling the shutdown function:

trakt.queued.shutdown()

This will empty the queue immediately. Any queued request will be rejected with a ShutdownError. You can tell it apart from any other Error from the name field, which is set to "ShutdownError", or by comparison with trakt.queued.ShutdownError. You app should probably just swallow it and maybe tell the user that a few pending requests have been canceled.

Combining queued with cached

trakt.tv-queued and trakt.tv-cached are loosely coupled. To combine their functionality, enable them both and pass the option cached (set to true) to queued:

let Trakt = require('trakt.tv')
let trakt = new Trakt({
  client_id: 'YYY',
  client_secret: 'ZZZ',
  plugins: {
    cached: require('trakt.tv-cached'),
    queued: require('trakt.tv-queued')
  },
  options: {
    queued: {
      concurrency: 2,
      delay: 1,
      cached: true // <-- this is important lol
    },
    cached: {
      defaultTTL: 20
    }
  }
})

If you do this, any API call will hit the cache first; if the data is not cached, it will go in the queue and it will be executed - eventually; if the data is cached, it will be returned immediately as a resolved Promise without going into the queue.

cached can be reconfigured on the fly just like concurrency and delay. This would turn the cache off and increase concurrency to 4:

trakt.queued.reconfigure({concurrency: 4, delay: 1, cached: false})

The system:ttl parameter that cached expects will have the intended effect in any call you make via either queued or cached.

trakt.cached.setDefaultTTL(40)
let data = await trakt.queued.seasons.season({id: 'game-of-thrones', season: 4})
console.log('API CALL COMPLETED: ' + data.length + ' "Game of Thrones" episodes fetched'))

// the following call will complete almost immediately without going into the queue
// because the data you need is already in the cache
data = await trakt.queued.seasons.season({id: 'game-of-thrones', season: 4})
console.log('API CALL COMPLETED: ' + data.length + ' "Game of Thrones" episodes fetched'))

// the following call will be queued and its result cached, but the TTL will be 0
data = await trakt.queued.seasons.season({id: 'penny-dreadful', season: 2, system:ttl: 0})
console.log('API CALL COMPLETED: ' + data.length + ' "Penny Dreadful" episodes fetched'))

// since the data fetched with the previous call has already expired, this
// call will hit the website again
data = await trakt.queued.seasons.season({id: 'penny-dreadful', season: 2})
console.log('API CALL COMPLETED: ' + data.length + ' "Penny Dreadful" episodes fetched'))

You can still use cached normally if you need to make calls that shouldn't be queued.

Debugging

If you enable debug mode...

trakt.queued.enableDebug()

...you'll get a truckload of messages of very dubious usefulness.

LICENSE

The MIT License (MIT) - author: MySidesTheyAreGone [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.