{"_id":"nanoresource","_rev":"234855","name":"nanoresource","description":"Small module that helps you maintain state around resources","dist-tags":{"latest":"1.3.0"},"maintainers":[{"name":"mafintosh","email":""}],"time":{"modified":"2023-03-24T16:19:42.000Z","created":"2018-11-15T14:08:34.742Z","1.3.0":"2019-11-04T13:21:02.576Z","1.2.0":"2019-06-14T13:15:46.034Z","1.1.0":"2019-06-14T12:37:15.874Z","1.0.0":"2018-11-15T14:09:41.803Z","0.0.0":"2018-11-15T14:08:34.742Z"},"users":{},"author":{"name":"Mathias Buus","url":"@mafintosh"},"repository":{"type":"git","url":"git+https://github.com/mafintosh/nanoresource.git"},"versions":{"1.3.0":{"name":"nanoresource","version":"1.3.0","description":"Small module that helps you maintain state around resources","main":"index.js","scripts":{"test":"standard && tape test/*.js"},"author":{"name":"Mathias Buus","url":"@mafintosh"},"license":"MIT","devDependencies":{"standard":"^14.3.1","tape":"^4.11.0"},"repository":{"type":"git","url":"git+https://github.com/mafintosh/nanoresource.git"},"bugs":{"url":"https://github.com/mafintosh/nanoresource/issues"},"homepage":"https://github.com/mafintosh/nanoresource#readme","dependencies":{"inherits":"^2.0.4"},"gitHead":"83148acb18353e079d2222f489d8b10d151472f1","_id":"nanoresource@1.3.0","_nodeVersion":"12.13.0","_npmVersion":"6.12.0","dist":{"shasum":"823945d9667ab3e81a8b2591ab8d734552878cd0","size":4333,"noattachment":false,"key":"/nanoresource/-/nanoresource-1.3.0.tgz","tarball":"http://name.csiicloud.com:7001/nanoresource/download/nanoresource-1.3.0.tgz"},"maintainers":[{"name":"mafintosh","email":""}],"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nanoresource_1.3.0_1572873662474_0.4424740030776544"},"_hasShrinkwrap":false,"publish_time":1572873662576,"_cnpm_publish_time":1572873662576,"_cnpmcore_publish_time":"2021-12-16T23:46:25.690Z"},"1.2.0":{"name":"nanoresource","version":"1.2.0","description":"Small module that helps you maintain state around resources","main":"index.js","scripts":{"test":"standard && tape test/*.js"},"author":{"name":"Mathias Buus","url":"@mafintosh"},"license":"MIT","devDependencies":{"standard":"^12.0.1","tape":"^4.9.1"},"repository":{"type":"git","url":"git+https://github.com/mafintosh/nanoresource.git"},"bugs":{"url":"https://github.com/mafintosh/nanoresource/issues"},"homepage":"https://github.com/mafintosh/nanoresource#readme","gitHead":"4cc9c1f6332e745b7fc07d8e8481ec2c47ee157d","_id":"nanoresource@1.2.0","_npmVersion":"6.4.1","_nodeVersion":"10.15.1","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"dist":{"shasum":"59a8bac28a1302179cdce49d95fd3393aa74f5bd","size":4080,"noattachment":false,"key":"/nanoresource/-/nanoresource-1.2.0.tgz","tarball":"http://name.csiicloud.com:7001/nanoresource/download/nanoresource-1.2.0.tgz"},"maintainers":[{"name":"mafintosh","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nanoresource_1.2.0_1560518145905_0.9619393725926315"},"_hasShrinkwrap":false,"publish_time":1560518146034,"_cnpm_publish_time":1560518146034,"_cnpmcore_publish_time":"2021-12-16T23:46:25.895Z"},"1.1.0":{"name":"nanoresource","version":"1.1.0","description":"Small module that helps you maintain state around resources","main":"index.js","scripts":{"test":"standard && tape test/*.js"},"author":{"name":"Mathias Buus","url":"@mafintosh"},"license":"MIT","devDependencies":{"standard":"^12.0.1","tape":"^4.9.1"},"repository":{"type":"git","url":"git+https://github.com/mafintosh/nanoresource.git"},"bugs":{"url":"https://github.com/mafintosh/nanoresource/issues"},"homepage":"https://github.com/mafintosh/nanoresource#readme","gitHead":"14a1cbadf4184086d0a231dde452966dd08b9e32","_id":"nanoresource@1.1.0","_npmVersion":"6.4.1","_nodeVersion":"10.15.1","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"dist":{"shasum":"2fe110d3aedf608cfdfc866c113ea021968a948a","size":4015,"noattachment":false,"key":"/nanoresource/-/nanoresource-1.1.0.tgz","tarball":"http://name.csiicloud.com:7001/nanoresource/download/nanoresource-1.1.0.tgz"},"maintainers":[{"name":"mafintosh","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nanoresource_1.1.0_1560515835713_0.651516559285281"},"_hasShrinkwrap":false,"publish_time":1560515835874,"_cnpm_publish_time":1560515835874,"_cnpmcore_publish_time":"2021-12-16T23:46:26.100Z"},"1.0.0":{"name":"nanoresource","version":"1.0.0","description":"Small module that helps you maintain state around resources","main":"index.js","scripts":{"test":"standard && tape test/*.js"},"author":{"name":"Mathias Buus","url":"@mafintosh"},"license":"MIT","devDependencies":{"standard":"^12.0.1","tape":"^4.9.1"},"repository":{"type":"git","url":"git+https://github.com/mafintosh/nanoresource.git"},"bugs":{"url":"https://github.com/mafintosh/nanoresource/issues"},"homepage":"https://github.com/mafintosh/nanoresource#readme","gitHead":"697ab99b1de8c8ee37e91cb09519d63697caf66d","_id":"nanoresource@1.0.0","_npmVersion":"6.4.1","_nodeVersion":"10.13.0","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"dist":{"shasum":"246d853dc23d23ef800fb8db55c6eef0462ca0e9","size":3972,"noattachment":false,"key":"/nanoresource/-/nanoresource-1.0.0.tgz","tarball":"http://name.csiicloud.com:7001/nanoresource/download/nanoresource-1.0.0.tgz"},"maintainers":[{"name":"mafintosh","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nanoresource_1.0.0_1542290981698_0.15188953485050205"},"_hasShrinkwrap":false,"publish_time":1542290981803,"_cnpm_publish_time":1542290981803,"_cnpmcore_publish_time":"2021-12-16T23:46:26.311Z"},"0.0.0":{"name":"nanoresource","version":"0.0.0","description":"Small module that helps you maintain state around resources","main":"index.js","scripts":{"test":"standard && tape test/*.js"},"author":{"name":"Mathias Buus","url":"@mafintosh"},"license":"MIT","devDependencies":{"standard":"^12.0.1","tape":"^4.9.1"},"repository":{"type":"git","url":"git+https://github.com/mafintosh/nanoresource.git"},"bugs":{"url":"https://github.com/mafintosh/nanoresource/issues"},"homepage":"https://github.com/mafintosh/nanoresource#readme","gitHead":"f9951e95f485642aa72eddd8189247ee7fdf661b","_id":"nanoresource@0.0.0","_npmVersion":"6.4.1","_nodeVersion":"10.13.0","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"dist":{"shasum":"3ac8781afc2a2df159c6e1762e079008ed892350","size":3971,"noattachment":false,"key":"/nanoresource/-/nanoresource-0.0.0.tgz","tarball":"http://name.csiicloud.com:7001/nanoresource/download/nanoresource-0.0.0.tgz"},"maintainers":[{"name":"mafintosh","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nanoresource_0.0.0_1542290914589_0.32341637010775726"},"_hasShrinkwrap":false,"publish_time":1542290914742,"_cnpm_publish_time":1542290914742,"_cnpmcore_publish_time":"2021-12-16T23:46:26.524Z"}},"readme":"# nanoresource\n\nSmall module that helps you maintain state around resources\n\n```sh\nnpm install nanoresource\n```\n\nAllows you to easily implement open/close functionality for a resource\nand having a way to mark the resource as active/inactive to avoid it being closed\nwhile it is in middle of something.\n\n## Usage\n\nWe can use this module to implement a simple resource that keep a file descriptor\naround behind the scene to keep stating the same file.\n\n```js\nconst nanoresource = require('nanoresource')\nconst fs = require('fs')\n\nclass FileSize extends nanoresource {\n  constructor (name) {\n    super()\n    this.filename = name\n    this.fd = 0\n  }\n\n  _open (cb) {\n    console.log('Now opening file ...')\n    fs.open(this.filename, 'r', (err, fd) => {\n      if (err) return cb(err)\n      this.fd = fd\n      cb(null)\n    })\n  }\n\n  _close (cb) {\n    console.log('Now closing file ...')\n    fs.close(this.fd, cb)\n  }\n\n  size (cb) {\n    this.open((err) => {\n      if (err) return cb(err)\n      if (!this.active(cb)) return\n      fs.fstat(this.fd, (err, st) => {\n        if (err) return this.inactive(cb, err)\n        this.inactive(cb, null, st.size)\n      })\n    })\n  }\n}\n\nconst f = new FileSize('index.js')\n\nf.size((err, size) => {\n  if (err) throw err\n  console.log('size is:', size)\n})\n\n// size a couple of times\nf.size((err, size) => {\n  if (err) throw err\n  console.log('size is:', size)\n})\n\n\n// after a bit when we are done with the resource we close it ...\nsetTimeout(() => f.close(), 1000)\n```\n\nWhen running it you should see that the file is only being opened and closed\nonce.\n\n## API\n\n#### `const r = nanoresource(options)`\n\nCreate a new nanoresource. You can also extend from this prototype if you prefer.\n\nOptions include:\n\n```js\n{\n  open: function (cb) { ... },\n  close: function (cb) { ... }\n}\n```\n\nIf you specify open or close they are used to populate `r._open` and `r._close` for you.\n\nThe open should open the resource and the close one should close it.\n\nThe close method is guaranteed to run *after* open. If no open has been called and close is called the close method is *not* called.\n\n#### `r.open(cb)`\n\nOpen the resource. Will call `r._open` behind the scenes once. If multiple calls to `r.open(cb)` the callbacks will be pushed to an internal queue and executed after the one call to `_open` has completed. If the resource was opened in the past the callback will be called on the next tick.\n\n* Check `r.opened` to see if the resource is fully opened.\n* Check `r.opening` to see if the resource is in the process of being opened.\n\nIf the `_open` method fails and calls it callback with an error this error is forwarded to the pending callbacks and if `r.open` is called again `_open` will be re-run.\n\n#### `r.close(cb)`\n\nSame semantics as `r.open`, except it only runs `_close` if the resource has been opened. If the resouce is in the middle of opening, `r.close` will wait for the open to finish and then try to close it.\n\nIf the resource is active (see the `r.active()` docs) then close will wait for the the resource to become inactive before closing it. However is a call `r.active()` happens after `r.close()` has been called it will fail immediately.\n\n* Check `r.closed` to see if the resource is fully closed.\n* Check `r.closing` to see if the resource is in the process of being closed.\n\nOnce a resource has been closed it can not be re-opened.\n\n#### `const valid = r.active()`\n\nMark the resource as active. By marking a resource as active you have to call `r.inactive()` once at a later stage to indicate that it is no longer active from your point of view.\n\nIf the resource is not in a valid active state (for example if it is being closed), the `r.active` method will return falls and you should return an error to the caller.\n\nAs a conveinience you can pass in a callback to `r.active(callback)` and the active method will call that callback immediately with an error if it's not in a valid active state in addition to returning false.\n\nIt is same to have multiple methods call this method in parallel.\n\n#### `r.inactive()`\n\nThe counter-part to `r.active()`. You must call this if you called `r.active` previously. It is a good idea to call this as the last thing you do in your \"action\" method of your resource.\n\nAs a conveinience you can pass in a callback, error, and value to `r.inactive(callback, error, value)` to call a callback after marking your view of the resource as inactive.\n\nIt is same to have multiple methods call this method in parallel.\n\n## EventEmitter\n\nIf you need a nanoresource that is also an EventEmitter do `const Nanoresource = require('./emitter')` which returns an implementation that inherits from Node.js's EventEmitter prototype.\n\n## License\n\nMIT\n","_attachments":{},"homepage":"https://github.com/mafintosh/nanoresource#readme","bugs":{"url":"https://github.com/mafintosh/nanoresource/issues"},"license":"MIT"}