https://github.com/vweevers/nanoresource-collection
Open and close multiple nanoresources as one.
https://github.com/vweevers/nanoresource-collection
collection nano nanoresource nodejs npm-package
Last synced: about 1 year ago
JSON representation
Open and close multiple nanoresources as one.
- Host: GitHub
- URL: https://github.com/vweevers/nanoresource-collection
- Owner: vweevers
- License: mit
- Created: 2020-07-25T09:58:37.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-25T10:02:36.000Z (almost 6 years ago)
- Last Synced: 2025-04-25T01:58:21.413Z (about 1 year ago)
- Topics: collection, nano, nanoresource, nodejs, npm-package
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# nanoresource-collection
**Open and close multiple [nanoresources](https://github.com/mafintosh/nanoresource) as one.**
[](https://www.npmjs.org/package/nanoresource-collection)
[](https://www.npmjs.org/package/nanoresource-collection)
[](http://travis-ci.com/vweevers/nanoresource-collection)
[](https://standardjs.com)
## Usage
```js
const Collection = require('nanoresource-collection')
const collection = new Collection([resource1, resource2])
collection.open(function (err) {
// All resources have been opened
})
collection.close(function (err) {
// All resources have been closed
})
```
## API
### `new Collection([options, ][resources])`
Both arguments are optional. The `resources` argument can be an array and should contain `nanoresource` instances. Alternatively use `collection.push(...resources)`.
Resources in the collection are opened sequentially. If one of the resources fails to open, other resources that did open successfully will be closed again. Resources are closed in reverse order: the last to be opened is the first to be closed.
Options:
- `opened` (boolean): default `false`. If `true`, assume the resources are already open and adjust collection state accordingly. Useful for partially compliant resources (like servers) that only have a `.close()` method.
Because `nanoresource-collection` is itself a `nanoresource`, the API is the same and collections are composable. In addition `nanoresource-collection` ships a variant that (also) supports promises:
```js
const Collection = require('nanoresource-collection').promises
const collection = new Collection()
await collection.open()
await collection.close()
```
## Install
With [npm](https://npmjs.org) do:
```
npm install nanoresource-collection
```
## License
[MIT](LICENSE.md) © 2020-present Vincent Weevers