Skip to content

Commit

Permalink
chore: improve types for MapeoProject's kProjectReplicate (#900)
Browse files Browse the repository at this point in the history
*This change should have no user impact.*

*I recommend reviewing this with whitespace changes disabled.*

`MapeoProject`'s `kProjectReplicate` member will give type errors if
called with a boolean. We aren't doing this today, but [we will in our
upcoming server work][0].

This happened because TypeScript's `Parameters` utility type only takes
the *last* signature of an overloaded function. To quote [the docs][1]:

> When inferring from a type with multiple call signatures (such as the
> type of an overloaded function), inferences are made from the last
> signature (which, presumably, is the most permissive catch-all case).
> It is not possible to perform overload resolution based on a list of
> argument types.

To fix this, we specify the types manually rather than pull them out of
`Parameters`. Doing this fixes the potential type error.

I also did a small amount of cleanup on the types in this function.

(This change is primarily to fix an error in our upcoming server branch,
but I think it's useful on its own.)

[0]: https://github.com/digidem/comapeo-core/blob/9aaeb37910821a6779ec8eef88af995e94448ba3/src/mapeo-project.js#L140-L141
[1]: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html
  • Loading branch information
EvanHahn authored Oct 23, 2024
1 parent 8b0bdb1 commit 100a973
Showing 1 changed file with 24 additions and 14 deletions.
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 @@ -587,20 +587,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

0 comments on commit 100a973

Please sign in to comment.