@libp2p/interfaces
Version:
Common code shared by the various libp2p interfaces
102 lines (67 loc) • 2.67 kB
Markdown
[](http://libp2p.io/)
[](https://discuss.libp2p.io)
[](https://codecov.io/gh/libp2p/js-libp2p-interfaces)
[](https://github.com/libp2p/js-libp2p-interfaces/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)
> Common code shared by the various libp2p interfaces
- [Install](
- [Usage](
- [AbortError](
- [Events](
- [AbortOptions](
- [Startable](
- [API Docs](
- [License](
- [Contribution](
```console
$ npm i @libp2p/interfaces
```
Throw an error with a `.code` property of `'ABORT_ERR'`:
```js
import { AbortError } from '@libp2p/interfaces/errors'
throw new AbortError()
```
Typed events:
```js
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
export interface MyEmitterEvents {
'some-event': CustomEvent<number>;
}
class MyEmitter extends EventEmitter<MyEmitterEvents> {
}
// later
const myEmitter = new MyEmitter()
myEmitter.addEventListener('some-event', (evt) => {
const num = evt.detail // <-- inferred as number
})
```
```js
import type { AbortOptions } from '@libp2p/interfaces'
```
Lifecycles for components
```js
import { start, stop, isStartable } from '@libp2p/interfaces/startable'
import type { Startable } from '@libp2p/interfaces/startable'
class MyStartable implements Startable {
// .. implementation methods
}
const myStartable = new MyStartable()
isStartable(myStartable) // returns true
await start(myStartable)
await stop(myStartable)
```
- <https://libp2p.github.io/js-libp2p-interfaces/modules/_libp2p_interfaces.html>
Licensed under either of
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.