You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/migrations/v2.0.0-v3.0.0.md
+36-4Lines changed: 36 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,9 +84,9 @@ and `sink` properties, we now listen for `message` events and call `.send` with
84
84
`Uint8Array`/`Uint8ArrayList` values.
85
85
86
86
We can detect when write backpressure needs to be applied by `.send` returning
87
-
`false` (we should await a `drain` event when this happens). and read
88
-
backpressure can be explicitly applied by calling the new `.pause` and `.resume`
89
-
methods.
87
+
`false` (we should await a `drain` event or for the promise returned from
88
+
`onDrain()` to resolve when this happens). and read backpressure can be
89
+
explicitly applied by calling the new `.pause` and `.resume`methods.
90
90
91
91
> [!CAUTION]
92
92
> If no `message` event handler is added, streams will buffer incoming data
@@ -179,7 +179,8 @@ When streams close they emit a `close` event. This event has an `error: Error` p
179
179
180
180
### Write backpressure
181
181
182
-
You can use [p-event](https://www.npmjs.com/package/p-event) or [race-event](https://www.npmjs.com/package/race-event) to pause writing due to backpressure:
182
+
When `.send()` returns false, the promise returned from `onDrain` will resolve
183
+
when the stream can accept new data:
183
184
184
185
```ts
185
186
import { createLibp2p } from'libp2p'
@@ -198,6 +199,37 @@ const bufs = [
198
199
// a lot of data
199
200
]
200
201
202
+
for (const buf ofbufs) {
203
+
if (!stream.send(buf)) {
204
+
awaitstream.onDrain({
205
+
signal: AbortSignal.timeout(5_000)
206
+
})
207
+
}
208
+
}
209
+
```
210
+
211
+
Alternatively you can use [p-event](https://www.npmjs.com/package/p-event) or
212
+
[race-event](https://www.npmjs.com/package/race-event) to wait for the stream to
0 commit comments