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 all commits
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ Create a new multidrive instance. `db` should be a valid `toiletdb` instance.
`createArchive` has an api of `createArchive(data, done)` where `data` is passed in
by `drive.create()` and `done(err, archive)` expects a valid archive.

`closeArchive` has an api of `closeArchive(archive, done)` where `archive` was
created by `createArchive` and `done(err)` is expected to be called when the
archive has been properly closed. `closeArchive` is called when a specific
archive is closed through `.close` or when through `.disconnect` all archives get
disconnected.

### archives = drive.list()
List all `archives` in the `multidrive`.

Expand All @@ -77,6 +83,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
32 changes: 30 additions & 2 deletions 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 setImmediate(cb.bind(null, new Error('disconnected')))
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 setImmediate(cb.bind(null, new Error('disconnected')))
_disconnected = true
store = null
if (archives.length === 0) return setImmediate(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 setImmediate(cb.bind(null, new Error('disconnected')))
if (Buffer.isBuffer(key)) key = key.toString('hex')
debug('close archive key=%s', key)
var i = 0
Expand All @@ -98,7 +126,7 @@ function multidrive (store, createArchive, closeArchive, cb) {
i = j
return true
})
if (!archive) return cb(new Error('could not find archive ' + key))
if (!archive) return setImmediate(cb.bind(null, new Error('could not find archive ' + key)))
if (archive instanceof Error) next()
else closeArchive(archive, next)

Expand Down
Loading