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

nanoresource-pool

v0.3.1

Published

Manage a pool of nanoresource instances

Downloads

37

Readme

nanoresource-pool

Manage a pool of nanoresource instances.

Installation

$ npm install nanoresource-pool

Status

Stable

Usage

const { Pool } = require('nanoresource-pool')

const pool = new Pool()

// add a resource
pool.add(resource)

// query all 'active' resources with a
// property called 'filename' with a value that matches '*.js'
pool.query({ filename: '*.js' })

// close pool and all resources when all resources are inactive
pool.close(callbackA)

Example

Below is an example pool implementation of opened JavaScript and JSON files.

const Resource = require('nanoresource')
const Pool = require('nanoresource-pool')
const fs = require('fs')

class File extends Resource {
  constructor(filename) {
    super()
    this.fd = 0
    this.filename = filename
  }

  _open(callback) {
    fs.open(this.filename, (err, fd) => {
      this.fd = fd
      callback(err)
    })
  }

  _close(callback) {
    fs.close(this.fd, callback)
  }
}

// `js`and `json` resources are based on the `File` class
const js = new Pool(File)
const json = new Pool(File)
const files = new Pool()

files.add(js)
files.add(json)

json.resource('package-lock.json')
json.resource('package.json')

js.resource('test.js')
js.resource('index.js')
js.resource('example.js')

files.ready(() => {
  // `query()` will search for resources in pool and
  // in child pools (recursively) using
  // static values, regular expression, and function
  // predcates to find something
  let results = null
  results = files.query({ filename: '*.js' }))
  results = files.query({ filename: (filename) => /.*.json/.test(filename) }))

  // will close all resources waiting after waiting for
  // all resources to be inactive
  files.close((err) => {
  })
})

API

pool = new Pool([Factory[, opts]])

Creates a new Pool instance from Factory and opts where Factory is an optional nanoresource constructor function and opts can be an object like:

{
  guard: NanoGuard(),
  allowActive: false,
  autoOpen: true, // if `false` you must call `pool.open()`
}

pool.opened

A boolean to indicated if the Pool is opened.

pool.opening

A boolean to indicated if the Pool is opening.

pool.closed

A boolean to indicated if the Pool is closed.

pool.closing

A boolean to indicated if the Pool is closing.

pool.allowActive

A default boolean value passed to each resource.close() method call for resources when the pool closes.

pool.size

The number of open and active resources. Accumlates the size of any children pools added.

pool.actives

The number of active resource handles.

pool.list([opts])

Get a list of resources, optionally filtering out resources marked as "closed" or "closing" where opts can be:

{
  closed: true
}

pool.ready(callback)

Waits for pool to be ready and calls callback() when it is.

pool.open([callback])

Opens the pool. You only need to call this if opts.autoOpen was set to false in the Pool constructor.

pool.close([allowActive[, callback])

Close pool and all added resources and child pools. Passes allowActive directly to the all of the resource's close() methods. If you do not provide the allowActive value, pool.allowActive will be used by default.

pool.query([where[, opts[, callback]])

Query for resources added to the pool. This function will also query any added child pools on the instance.

pool.add(resource[, opts])

Add a resource to the pool. Will remove from pool when the resource successfully closes. Set opts.autoOpen = false to prevent the added resource from automatically opening.

pool.resource(...args)

Acquire a new resource based on the pool factory constructor. (calls new pool.Factory(...args)).

License

MIT