Skip to content

Commit

Permalink
Add getCachedMsgId()
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Dec 22, 2021
1 parent c38084f commit 1b77fe1
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1089,14 +1089,29 @@ class Gossipsub extends Pubsub {
}

/**
* Prefer the fast cache over this.getMsgId()
* @param {InMessage} msg
* Get from the application cache first, then from the fast cache, then from this.getMsgId()
* @param {InMessage} message
*/
async getCanonicalMessageIdStr (message: InMessage): Promise<string> {
const cachedMsgId = this.getCachedMsgId(message)
if (cachedMsgId) {
return messageIdToString(cachedMsgId)
}

const fastMsgIdStr = messageIdToString(SHA256.digest(message.data))
return this.fastMsgIdCache.get(fastMsgIdStr) ?? messageIdToString(await this.getMsgId(message))
}

/**
* Application should override this to return its cached message id without computing it.
* Return undefine if it does not have it
* @param {InMessage} message
* @returns
*/
getCachedMsgId (message: InMessage): Uint8Array | undefined {
return undefined
}

/**
* Publish messages
*
Expand Down

0 comments on commit 1b77fe1

Please sign in to comment.