-
Notifications
You must be signed in to change notification settings - Fork 44
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
feat: added allowPublishToZeroPeers as optional param to publish function #395
feat: added allowPublishToZeroPeers as optional param to publish function #395
Conversation
src/index.ts
Outdated
async publish( | ||
topic: TopicStr, | ||
data: Uint8Array, | ||
publishOptions: Partial<GossipsubOpts> = { |
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.
Using GossipsubOpts here is extremely broad, just allow allowPublishToZeroPeers
and nothing else. You don't need to default to false, that what will happen anyway
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.
Understood. I had made it more generic in case we may want to pass other params such as emitSelf
in the future. I set the default to false
just for readability to be more explicit.
src/index.ts
Outdated
@@ -1995,7 +1995,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G | |||
* | |||
* For messages not from us, this class uses `forwardMessage`. | |||
*/ | |||
async publish(topic: TopicStr, data: Uint8Array): Promise<PublishResult> { | |||
async publish(topic: TopicStr, data: Uint8Array, allowPublishToZeroPeers = false): Promise<PublishResult> { |
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.
async publish(topic: TopicStr, data: Uint8Array, allowPublishToZeroPeers = false): Promise<PublishResult> { | |
type PublishOpts { | |
allowPublishToZeroPeers?: boolean | |
} | |
async publish(topic: TopicStr, data: Uint8Array, opts?: PublishOpts): Promise<PublishResult> { | |
// Current publish opt takes precedence global opts, while preserving false value | |
const allowPublishToZeroPeers = opts?.allowPublishToZeroPeers ?? this.opts.allowPublishToZeroPeers; | |
... | |
if ( | |
tosend.size === 0 && | |
!allowPublishToZeroPeers | |
!willSendToSelf | |
) { | |
throw Error('PublishError.InsufficientPeers') | |
} |
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 added the PublishOpts
type to the types file.
3c5d6ff
to
c4b1bea
Compare
Added a test |
Codecov ReportBase: 83.41% // Head: 83.42% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## master #395 +/- ##
=======================================
Coverage 83.41% 83.42%
=======================================
Files 48 48
Lines 11766 11771 +5
Branches 1266 1267 +1
=======================================
+ Hits 9815 9820 +5
Misses 1951 1951
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
ce30c04
to
d067b9a
Compare
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.
LGTM thanks @maschad
Closes #367
The default for allowPublishToZeroPeers will be false but a user can optionally call with allowPublishToZeroPeers set to true to suppress InsufficientPeers errors