-
Notifications
You must be signed in to change notification settings - Fork 48
feat: add unwantBlocks method #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8bfbb20
6506f55
d21de6a
dd3efc6
9e7793b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,4 +71,4 @@ | |
"David Dias <[email protected]>", | ||
"Friedel Ziegelmayer <[email protected]>" | ||
] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,12 +138,11 @@ module.exports = class Bitwap { | |
cb(err, block) | ||
} | ||
|
||
this.getBlocks([key], (err, res) => { | ||
if (err) { | ||
return done(err) | ||
} | ||
this.getBlocks([key], (results) => { | ||
const err = results[key].error | ||
const block = results[key].block | ||
|
||
done(null, res[0]) | ||
done(err, block) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if I understand what is going on here, is this some es6 witchcraft? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No witchcraft, what do you not understand? If there is no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, nvm, I misread and thought this was a function declaration, I was wondering that a array argument was and why only return one block in a func that is called getBlocks Thank you :) |
||
}) | ||
} | ||
|
||
|
@@ -153,18 +152,51 @@ module.exports = class Bitwap { | |
} | ||
|
||
getBlocks (keys, cb) { | ||
const blocks = [] | ||
const finish = (block) => { | ||
blocks.push(block) | ||
log('finish: %s/%s', blocks.length, keys.length) | ||
if (blocks.length === keys.length) { | ||
cb(null, blocks) | ||
const results = {} | ||
const unwantListeners = {} | ||
const blockListeners = {} | ||
const unwantEvent = (key) => `unwant:${key.toString('hex')}` | ||
const blockEvent = (key) => `block:${key.toString('hex')}` | ||
|
||
const cleanupListeners = () => { | ||
keys.forEach((key) => { | ||
this.notifications.removeListener(unwantEvent(key), unwantListeners[key]) | ||
this.notifications.removeListener(blockEvent(key), blockListeners[key]) | ||
}) | ||
} | ||
|
||
const addListeners = () => { | ||
keys.forEach((key) => { | ||
unwantListeners[key] = () => { | ||
finish(key, new Error(`manual unwant: ${key.toString('hex')}`)) | ||
} | ||
|
||
blockListeners[key] = (block) => { | ||
finish(key, null, block) | ||
} | ||
|
||
this.notifications.once(unwantEvent(key), unwantListeners[key]) | ||
this.notifications.once(blockEvent(key), blockListeners[key]) | ||
}) | ||
} | ||
|
||
const finish = (key, err, block) => { | ||
results[key] = { | ||
error: err, | ||
block: block | ||
} | ||
|
||
if (Object.keys(results).length === keys.length) { | ||
cleanupListeners() | ||
cb(results) | ||
} | ||
} | ||
|
||
addListeners() | ||
|
||
keys.forEach((key) => { | ||
// Sanity check, we don't want to announce looking for blocks | ||
// when we might have them ourselves | ||
// We don't want to announce looking for blocks | ||
// when we might have them ourselves. | ||
this.datastore.has(key, (err, exists) => { | ||
if (err) { | ||
log('error in datastore.has: ', err.message) | ||
|
@@ -174,8 +206,8 @@ module.exports = class Bitwap { | |
if (exists) { | ||
this.datastore.get(key, (err, res) => { | ||
if (!err && res) { | ||
finish(key, null, res) | ||
this.wm.cancelWants([key]) | ||
finish(res) | ||
return | ||
} | ||
|
||
|
@@ -185,14 +217,19 @@ module.exports = class Bitwap { | |
}) | ||
} | ||
}) | ||
this.notifications.once(`block:${key.toString('hex')}`, (block) => { | ||
finish(block) | ||
}) | ||
}) | ||
|
||
this.wm.wantBlocks(keys) | ||
} | ||
|
||
// removes the given keys from the want list independent of any ref counts | ||
unwantBlocks (keys) { | ||
this.wm.unwantBlocks(keys) | ||
keys.forEach((key) => { | ||
this.notifications.emit(`unwant:${key.toString('hex')}`) | ||
}) | ||
} | ||
|
||
// removes the given keys from the want list | ||
cancelWants (keys) { | ||
this.wm.cancelWants(keys) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"key being the multihash of the block"