Skip to content

Commit

Permalink
add documentation for Typescript types
Browse files Browse the repository at this point in the history
  • Loading branch information
arilotter committed Dec 5, 2021
1 parent 794dee3 commit 441cd63
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions api-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ The P2PT class is defined and exposed by the `p2pt` module :
```javascript
const P2PT = require('p2pt')
```
In Typescript, you can use
```typescript
import P2PT from "p2pt";
```


This is the base class that needs to be instantiated to use this library. It provides the API to implement P2P connections, communicate messages (even large content!) using WebTorrent WebSocket Trackers as the signalling server.

Expand Down Expand Up @@ -75,6 +80,16 @@ var trackersAnnounceURLs = [
var p2pt = new P2PT(trackersAnnounceURLs, 'myApp')
```

In Typescript, the `P2PT` class accepts an optional type parameter to constrain the type of messages you can pass to the `send` function. It doesn't constrain the type of messages you recieve, since any peer *could* send anything.
```typescript
type Msg = 'hello' | { goodbye: boolean }
const p2pt = new P2PT<Msg>(trackersAnnounceURLs, 'myApp')
// ... find a peer ...
p2pt.send(peer, 'some_message') // TS typecheck error: Argument of type 'string' is not assignable to parameter of type 'Msg'.
p2pt.send(peer, 'hello') // ok!
p2pt.send(peer, { goodbye: true }) // ok!
```

### `setIdentifier(identifierString)`
Sets the identifier string used to discover peers in the network
* **Arguments:**
Expand Down

0 comments on commit 441cd63

Please sign in to comment.