Skip to content
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

chore: improve types for MapeoProject's kProjectReplicate #900

Merged
merged 3 commits into from
Oct 23, 2024
Merged
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
38 changes: 24 additions & 14 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { IconApi } from './icon-api.js'
import { readConfig } from './config-import.js'
import TranslationApi from './translation-api.js'
/** @import { ProjectSettingsValue } from '@comapeo/schema' */
/** @import { CoreStorage, KeyPair, Namespace } from './types.js' */
/** @import { CoreStorage, KeyPair, Namespace, ReplicationStream } from './types.js' */

/** @typedef {Omit<ProjectSettingsValue, 'schemaName'>} EditableProjectSettings */
/** @typedef {ProjectSettingsValue['configMetadata']} ConfigMetadata */
Expand Down Expand Up @@ -581,20 +581,30 @@ export class MapeoProject extends TypedEmitter {
* and only this project will replicate (to replicate multiple projects you
* need to replicate the manager instance via manager[kManagerReplicate])
*
* @param {Parameters<import('hypercore')['replicate']>[0]} stream A duplex stream, a @hyperswarm/secret-stream, or a Protomux instance
* @param {(
* boolean |
* import('stream').Duplex |
* import('streamx').Duplex
* )} isInitiatorOrStream
* @returns {ReplicationStream}
*/
[kProjectReplicate](stream) {
// @ts-expect-error - hypercore types need updating
const replicationStream = this.#coreManager.creatorCore.replicate(stream, {
// @ts-ignore - hypercore types do not currently include this option
ondiscoverykey: async (discoveryKey) => {
const protomux =
/** @type {import('protomux')<import('@hyperswarm/secret-stream')>} */ (
replicationStream.noiseStream.userData
)
this.#syncApi[kHandleDiscoveryKey](discoveryKey, protomux)
},
})
[kProjectReplicate](isInitiatorOrStream) {
const replicationStream = this.#coreManager.creatorCore.replicate(
isInitiatorOrStream,
/**
* Hypercore types need updating.
* @type {any}
*/ ({
/** @param {Buffer} discoveryKey */
ondiscoverykey: async (discoveryKey) => {
const protomux =
/** @type {import('protomux')<import('@hyperswarm/secret-stream')>} */ (
replicationStream.noiseStream.userData
)
this.#syncApi[kHandleDiscoveryKey](discoveryKey, protomux)
},
})
)
return replicationStream
}

Expand Down
Loading