Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
fix!: remove @libp2p/components (#192)
Browse files Browse the repository at this point in the history
`@libp2p/components` is a choke-point for our dependency graph as it depends on every interface, meaning when one interface revs a major
`@libp2p/components` major has to change too which means every module depending on it also needs a major.

Switch instead to constructor injection of simple objects that let modules declare their dependencies on interfaces directly instead of
indirectly via `@libp2p/components`

Refs libp2p/js-libp2p-components#6

BREAKING CHANGE: modules no longer implement `Initializable` instead switching to constructor injection
  • Loading branch information
achingbrain authored Oct 12, 2022
1 parent 3631ad4 commit 9916896
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 123 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# @libp2p/floodsub <!-- omit in toc -->

[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![IRC](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-floodsub.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-floodsub)
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-interfaces/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/libp2p/js-libp2p-floodsub/actions/workflows/js-test-and-release.yml)
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-floodsub/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/libp2p/js-libp2p-floodsub/actions/workflows/js-test-and-release.yml)

> libp2p-floodsub, also known as pubsub-flood or just dumbsub, this implementation of pubsub focused on delivering an API for Publish/Subscribe, but with no CastTree Forming (it just floods the network).
Expand Down Expand Up @@ -33,18 +32,21 @@ Instead please use [gossipsub](https://www.npmjs.com/package/@chainsafe/libp2p-g
## Usage

```JavaScript
import { FloodSub } from '@libp2p/floodsub'
import { createLibp2pNode } from 'libp2p'
import { floodsub } from '@libp2p/floodsub'

const fsub = new FloodSub()

await fsub.start()
const node = await createLibp2pNode({
pubsub: floodsub()
//... other options
})
await node.start()

fsub.addEventListener('message', (data) => {
console.log(data)
node.pubsub.subscribe('fruit')
node.pubsub.addEventListener('message', (evt) => {
console.log(evt)
})
fsub.subscribe('fruit')

fsub.publish('fruit', new TextEncoder().encode('banana'))
node.pubsub.publish('fruit', new TextEncoder().encode('banana'))
```

## License
Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,22 @@
"@libp2p/interface-peer-id": "^1.0.2",
"@libp2p/interface-pubsub": "^3.0.0",
"@libp2p/logger": "^2.0.0",
"@libp2p/pubsub": "^4.0.0",
"protons-runtime": "^3.1.0",
"@libp2p/pubsub": "^5.0.0",
"protons-runtime": "^4.0.1",
"uint8arraylist": "^2.1.1",
"uint8arrays": "^3.0.0"
"uint8arrays": "^4.0.2"
},
"devDependencies": {
"@libp2p/components": "^3.0.1",
"@libp2p/interface-mocks": "^6.0.1",
"@libp2p/interface-pubsub-compliance-tests": "^2.0.1",
"@libp2p/interface-mocks": "^7.0.1",
"@libp2p/interface-pubsub-compliance-tests": "^4.0.0",
"@libp2p/peer-collections": "^2.0.0",
"@libp2p/peer-id": "^1.1.10",
"@libp2p/peer-id-factory": "^1.0.9",
"@multiformats/multiaddr": "^11.0.3",
"aegir": "^37.2.0",
"multiformats": "^9.4.5",
"multiformats": "^10.0.0",
"p-wait-for": "^5.0.0",
"protons": "^5.1.0",
"protons": "^6.0.0",
"sinon": "^14.0.0",
"wherearewe": "^2.0.1"
}
Expand Down
18 changes: 13 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { toString } from 'uint8arrays/to-string'
import { PubSubBaseProtocol } from '@libp2p/pubsub'
import { PubSubBaseProtocol, PubSubComponents } from '@libp2p/pubsub'
import { multicodec } from './config.js'
import { SimpleTimeCache } from './cache.js'
import type { PubSubInit, Message, PubSubRPC, PubSubRPCMessage, PublishResult } from '@libp2p/interface-pubsub'
import type { PubSubInit, Message, PubSubRPC, PubSubRPCMessage, PublishResult, PubSub } from '@libp2p/interface-pubsub'
import type { PeerId } from '@libp2p/interface-peer-id'
import { logger } from '@libp2p/logger'
import { RPC } from './message/rpc.js'
Expand All @@ -16,6 +16,10 @@ export interface FloodSubInit extends PubSubInit {
seenTTL?: number
}

export interface FloodSubComponents extends PubSubComponents {

}

/**
* FloodSub (aka dumbsub is an implementation of pubsub focused on
* delivering an API for Publish/Subscribe, but with no CastTree Forming
Expand All @@ -24,8 +28,8 @@ export interface FloodSubInit extends PubSubInit {
export class FloodSub extends PubSubBaseProtocol {
public seenCache: SimpleTimeCache<boolean>

constructor (init?: FloodSubInit) {
super({
constructor (components: FloodSubComponents, init?: FloodSubInit) {
super(components, {
...init,
canRelayMessage: true,
multicodecs: [multicodec]
Expand Down Expand Up @@ -94,7 +98,7 @@ export class FloodSub extends PubSubBaseProtocol {
}

peers.forEach(id => {
if (this.components.getPeerId().equals(id)) {
if (this.components.peerId.equals(id)) {
log('not sending message on topic %s to myself', message.topic)
return
}
Expand All @@ -113,3 +117,7 @@ export class FloodSub extends PubSubBaseProtocol {
return { recipients }
}
}

export function floodsub (init: FloodSubInit = {}): (components: FloodSubComponents) => PubSub {
return (components: FloodSubComponents) => new FloodSub(components, init)
}
Loading

0 comments on commit 9916896

Please sign in to comment.