Skip to content

Commit 460dafb

Browse files
jacobheundaviddias
authored andcommitted
docs: update docs to be clearer
Ensure locks have a close method when creating the lock
1 parent d1457cd commit 460dafb

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Arguments:
142142

143143
* `path` (string, mandatory): the path for this repo
144144
* `options` (object, optional): may contain the following values
145-
* `lock` (string *Deprecated* or [Lock](#lock)), string can be `"fs"` or `"memory"`: what type of lock to use. Lock has to be acquired when opening.
145+
* `lock` ([Lock](#lock) or string *Deprecated*): what type of lock to use. Lock has to be acquired when opening. string can be `"fs"` or `"memory"`.
146146
* `storageBackends` (object, optional): may contain the following values, which should each be a class implementing the [datastore interface](https://github.com/ipfs/interface-datastore#readme):
147147
* `root` (defaults to [`datastore-fs`](https://github.com/ipfs/js-datastore-fs#readme) in Node.js and [`datastore-level`](https://github.com/ipfs/js-datastore-level#readme) in the browser). Defines the back-end type used for gets and puts of values at the root (`repo.set()`, `repo.get()`)
148148
* `blocks` (defaults to [`datastore-fs`](https://github.com/ipfs/js-datastore-fs#readme) in Node.js and [`datastore-level`](https://github.com/ipfs/js-datastore-level#readme) in the browser). Defines the back-end type used for gets and puts of values at `repo.blocks`.
@@ -302,12 +302,14 @@ IPFS Repo comes with two built in locks: memory and fs. These can be imported vi
302302

303303
```js
304304
const fsLock = require('ipfs-repo/src/lock') // Default in Node.js
305-
const memLock = require('ipfs-repo/src/lock-memory') // Default in browser
305+
const memoryLock = require('ipfs-repo/src/lock-memory') // Default in browser
306306
```
307307

308-
#### `lock.open (dir, callback)`
308+
You can also provide your own custom Lock. It must be an object with the following interface:
309309

310-
Sets the lock if one does not already exist.
310+
#### `lock.lock (dir, callback)`
311+
312+
Sets the lock if one does not already exist. If a lock already exists, `callback` should be called with an error.
311313

312314
`dir` is a string to the directory the lock should be created at. The repo typically creates the lock at its root.
313315

src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,14 @@ class IpfsRepo {
159159
* @returns {void}
160160
*/
161161
_openLock (path, callback) {
162-
this._locker.lock(path, callback)
162+
this._locker.lock(path, (err, lockfile) => {
163+
if (err) {
164+
return callback(err, null)
165+
}
166+
167+
assert.equal(typeof lockfile.close, 'function', 'Locks must have a close method')
168+
callback(null, lockfile)
169+
})
163170
}
164171

165172
/**

test/options-test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('custom options tests', () => {
2929
expect(repo.options).to.deep.equal(expectedRepoOptions())
3030
})
3131

32-
it('allows for a custom locker', () => {
32+
it('allows for a custom lock', () => {
3333
const lock = {
3434
lock: (path, callback) => { },
3535
locked: (path, callback) => { }
@@ -41,6 +41,23 @@ describe('custom options tests', () => {
4141

4242
expect(repo._getLocker()).to.deep.equal(lock)
4343
})
44+
45+
it('ensures a custom lock has a .close method', (done) => {
46+
const lock = {
47+
lock: (path, callback) => {
48+
callback(null, {})
49+
}
50+
}
51+
52+
const repo = new Repo(repoPath, {
53+
lock
54+
})
55+
56+
expect(
57+
() => repo._openLock(repo.path)
58+
).to.throw('Locks must have a close method')
59+
done()
60+
})
4461
})
4562

4663
function noop () {}

0 commit comments

Comments
 (0)