-
Notifications
You must be signed in to change notification settings - Fork 1
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
fix: update pre-haves as they change #594
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,7 +164,7 @@ export class CoreManager extends TypedEmitter { | |
}) | ||
|
||
this.#creatorCore.on('peer-add', (peer) => { | ||
this.#sendHaves(peer) | ||
this.#sendHaves(peer, this.#coreIndex) | ||
}) | ||
this.#creatorCore.on('peer-remove', (peer) => { | ||
// When a peer is removed we clean up any unanswered key requests, so that | ||
|
@@ -304,12 +304,29 @@ export class CoreManager extends TypedEmitter { | |
peer._maybeWant(0, core.length) | ||
}) | ||
|
||
// A non-writer core will emit 'append' when its length is updated from the | ||
// initial sync with a peer, and we will not have sent a "maybe want" for | ||
// this range, so we need to do it now. Subsequent appends are propogated | ||
// (more efficiently) via range broadcasts, so we only need to listen to the | ||
// first append. | ||
if (!writer) { | ||
if (writer) { | ||
const sendHaves = () => { | ||
for (const peer of this.#creatorCore.peers) { | ||
this.#sendHaves(peer, [{ core, namespace }]) | ||
} | ||
} | ||
|
||
// Tell connected peers, who we aren't necessarily syncing with, about | ||
// what we just added or cleared. Hypercore doesn't emit anything when | ||
// clearing, so we patch it in. | ||
core.on('append', sendHaves) | ||
const originalClear = core.clear | ||
core.clear = function clear() { | ||
const result = originalClear.apply(this, /** @type {any} */ (arguments)) | ||
result.then(sendHaves) | ||
return result | ||
} | ||
Comment on lines
+318
to
+323
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. I considered patching Hypercore itself, which doesn't feel meaningfully different. I don't think Hypercore offers a way to address this in a better way, even on newer versions. |
||
} else { | ||
// A non-writer core will emit 'append' when its length is updated from | ||
// the initial sync with a peer, and we will not have sent a "maybe want" | ||
// for this range, so we need to do it now. Subsequent appends are | ||
// propagated (more efficiently) via range broadcasts, so we only need to | ||
// listen to the first append. | ||
core.once('append', () => { | ||
for (const peer of core.peers) { | ||
// TODO: It would be more efficient (in terms of network traffic) to | ||
|
@@ -422,8 +439,9 @@ export class CoreManager extends TypedEmitter { | |
/** | ||
* | ||
* @param {any} peer | ||
* @param {Iterable<{ core: Hypercore<Hypercore.ValueEncoding, Buffer>, namespace: Namespace }>} cores | ||
*/ | ||
async #sendHaves(peer) { | ||
async #sendHaves(peer, cores) { | ||
if (!peer) { | ||
console.warn('sendHaves no peer', peer.remotePublicKey) | ||
// TODO: How to handle this and when does it happen? | ||
|
@@ -432,7 +450,7 @@ export class CoreManager extends TypedEmitter { | |
|
||
peer.protomux.cork() | ||
|
||
for (const { core, namespace } of this.#coreIndex) { | ||
for (const { core, namespace } of cores) { | ||
// We want ready() rather than update() because we are only interested in local data | ||
await core.ready() | ||
const { discoveryKey } = core | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I moved this comment down and re-formatted it so no line was longer than 80 characters; I didn't change it other than fixing a typo.