Effect CommunityEC
Effect Community3y ago
33 replies
Nomadin

Understanding the Concept of Stream Throttling in TypeScript

Hi team, can you please help me understand the interface of
Stream.throttle
?

I have looked at both documentation and unit tests of
Stream.throttle
but am still not able to understand how it works (the concept of cost, bandwidth and burst are unfamiliar to me).

declare const response: Http.ClientResponse;

pipe(
  response.stream,
  Stream.broadcast(2, 1),
  Effect.map(Tuple.mapSecond(
    flow(
      Stream.map(u8arr => u8arr.length),
      Stream.scan(0, Number.sum),
      Stream.throttle(Duration.seconds(1)), // emit the latest value every 1 sec, drop other intermediate values
      Stream.tap(x => Ef.log(`Downloaded ${x} bytes`))
    )
  ),
  Ef.map(([responseStream, logStream]) => Stream.merge(responseStream, logStream)),
  Ef.flatMap(Stream.run(fileSystem.sink(outputPath, { flag: 'w' })))
);
Was this page helpful?