-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: emit peer-have messages don't persist Fixes #309, removes responsibility from CoreManager to track peer-have messages - will be handled by SyncState class * feat: Add NamespaceSyncState This combines all core sync states for a namespace. It listens to CoreManager for new cores and pre-have messages. * feat: add namespace to peer pre-have messages * fix param name * WIP tests * add tests * don't use eventEmitter Since these are internal modules and we don't attach a "listener" other than in the constructor, switching to a pattern that passes an 'onUpdate' constructor param, that avoids needing to track when event listeners are removed
- Loading branch information
1 parent
37dbd22
commit 05b0830
Showing
8 changed files
with
326 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { CoreSyncState } from './core-sync-state.js' | ||
import { discoveryKey } from 'hypercore-crypto' | ||
|
||
/** | ||
* @typedef {object} PeerSyncState | ||
* @property {number} have | ||
* @property {number} want | ||
* @property {number} wanted | ||
* @property {number} missing | ||
*/ | ||
|
||
/** | ||
* @typedef {object} SyncState | ||
* @property {PeerSyncState} localState | ||
*/ | ||
|
||
/** | ||
* @template {import('../core-manager/index.js').Namespace} [TNamespace=import('../core-manager/index.js').Namespace] | ||
*/ | ||
export class NamespaceSyncState { | ||
/** @type {Map<string, CoreSyncState>} */ | ||
#coreStates = new Map() | ||
#handleUpdate | ||
#namespace | ||
|
||
/** | ||
* @param {object} opts | ||
* @param {TNamespace} opts.namespace | ||
* @param {import('../core-manager/index.js').CoreManager} opts.coreManager | ||
* @param {() => void} opts.onUpdate Called when a state update is available (via getState()) | ||
*/ | ||
constructor({ namespace, coreManager, onUpdate }) { | ||
this.#namespace = namespace | ||
this.#handleUpdate = onUpdate | ||
|
||
for (const { core, key } of coreManager.getCores(namespace)) { | ||
this.#addCore(core, key) | ||
} | ||
|
||
coreManager.on('add-core', ({ core, namespace, key }) => { | ||
if (namespace !== this.#namespace) return | ||
this.#addCore(core, key) | ||
}) | ||
|
||
coreManager.on('peer-have', (namespace, msg) => { | ||
if (namespace !== this.#namespace) return | ||
this.#insertPreHaves(msg) | ||
}) | ||
} | ||
|
||
get namespace() { | ||
return this.#namespace | ||
} | ||
|
||
/** @returns {SyncState} */ | ||
getState() { | ||
const state = { | ||
localState: { have: 0, want: 0, wanted: 0, missing: 0 }, | ||
} | ||
for (const crs of this.#coreStates.values()) { | ||
const { localState } = crs.getState() | ||
state.localState.have += localState.have | ||
state.localState.want += localState.want | ||
state.localState.wanted += localState.wanted | ||
state.localState.missing += localState.missing | ||
} | ||
return state | ||
} | ||
|
||
/** | ||
* @param {import('hypercore')<"binary", Buffer>} core | ||
* @param {Buffer} coreKey | ||
*/ | ||
#addCore(core, coreKey) { | ||
const discoveryId = discoveryKey(coreKey).toString('hex') | ||
this.#getCoreState(discoveryId).attachCore(core) | ||
} | ||
|
||
/** | ||
* @param {{ | ||
* peerId: string, | ||
* start: number, | ||
* coreDiscoveryId: string, | ||
* bitfield: Uint32Array | ||
* }} opts | ||
*/ | ||
#insertPreHaves({ peerId, start, coreDiscoveryId, bitfield }) { | ||
this.#getCoreState(coreDiscoveryId).insertPreHaves(peerId, start, bitfield) | ||
} | ||
|
||
/** | ||
* @param {string} discoveryId | ||
*/ | ||
#getCoreState(discoveryId) { | ||
let coreState = this.#coreStates.get(discoveryId) | ||
if (!coreState) { | ||
coreState = new CoreSyncState(this.#handleUpdate) | ||
this.#coreStates.set(discoveryId, coreState) | ||
} | ||
return coreState | ||
} | ||
} |
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
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
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.