Skip to content
This repository was archived by the owner on Jan 7, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ If an archive with the same key already exists, returns that instead and sets
### drive.close(key, callback(err))
Remove an archive by its public key. Calls `closeArchive()`

### drive.disconnect(callback(err))
Disconnects the drive from the store and closes all archives (without removing them).

## Installation
```sh
$ npm install multidrive
Expand Down
30 changes: 29 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ function multidrive (store, createArchive, closeArchive, cb) {
assert.equal(typeof cb, 'function', 'multidrive: cb should be type function')

var archives = []
var _disconnected = false
var drive = {
list: list,
create: create,
close: close
close: close,
disconnect: disconnect
}

debug('initialize')
Expand Down Expand Up @@ -58,6 +60,7 @@ function multidrive (store, createArchive, closeArchive, cb) {
}

function create (data, cb) {
if (_disconnected) return cb(new Error('disconnected'))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those callbacks should be called after setImmediate(), a callback shouldn't be called in the same tick as its function is invoked

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so too but on other places it also immediately returned (when in rome...). I took the liberty to fix all occurrences: c621c09

debug('create archive data=%j', data)
createArchive(data, function (err, archive) {
if (err) return cb(err)
Expand Down Expand Up @@ -88,7 +91,32 @@ function multidrive (store, createArchive, closeArchive, cb) {
})
}

function disconnect (cb) {
if (_disconnected) return cb(new Error('disconnected'))
_disconnected = true
store = null
if (archives.length === 0) return cb()
var _archives = archives
var count = _archives.length
var _err
_archives.forEach(function (archive) {
closeArchive(archive, next)
})
archives = []

function next (err) {
count--
if (err && !_err) {
_err = err
}
if (count === 0) {
cb(_err)
}
}
}

function close (key, cb) {
if (_disconnected) return cb(new Error('disconnected'))
if (Buffer.isBuffer(key)) key = key.toString('hex')
debug('close archive key=%s', key)
var i = 0
Expand Down
Loading