0.16.19 • Published 24 days ago

@graffy/stream v0.16.19

Weekly downloads
177
License
Apache-2.0
Repository
github
Last release
24 days ago

Graffy Stream

Utility for creating AsyncIterables (streams) from any callback-based event source.

Why?

Many JS APIs provide callback-based event emitters that must be subscribed to and unsubscribed from. The async generator syntax in JS does not provide an easy mechanism to do this.

Imagine the event emitter:

websocket.addEventListener('message', callback);

We would like to feed this stream of messages into an AsyncIterable, so we can do:

for await (const message of messages) {
  // Do stuff with message.
  if (done) break;
}

and do stuff. We might also want to call removeEventListener when we break (or return or throw) out of the loop.

Usage

The default export is a factory function.

import { makeStream } from '@graffy/stream';

Minimal Example

This handles subscribing and unsubscribing from the event emitter.

const stream = makeStream((push) => {
  eventSource.on('event', push);
  return () => eventSource.off('event', push);
});

The return value, stream, is an AsyncIterable that can be used in a for-await-of loop.

Handle upstream end

This handles situations where the "upstream" event emitter has an "end" event, which should make the "downstream" for-await-of loop end as well.

const stream = makeStream((push, end) => {
  eventSource.on('event', (event) => push(event));
  eventSource.on('end', () => end());
  return () => eventSource.close();
});

Handle errors

This handles errors in both directions. The "upstream" might close with an error, which should be communicated downstream as the for-await-of loop throwing. Less often, if the code inside the loop throws, we might want to communicate that upstream.

Both are handled by the "end" functions being called with an error object as an argument.

const stream = makeStream((push, end) => {
  eventSource.on('event', (event) => push(event));
  eventSource.on('end', () => end());
  eventSource.on('error', (error) => end(error));
  return (error) => {
    if (error) {
      eventSource.closeWithError();
      return;
    }
    eventSource.close();
  };
});

Backpressure

When streams are used to pipe IO events, we might run into a situation where the downstream consumer is slower than the upstream producer. If left unchecked, we might run out of memory - we need to maintain a buffer of unconsumed events.

To handle this situation, the producer might expose methods to pause and resume the stream of events; if not, in some situations, it might also be appropriate to unsubscribe and resubscribe.

Graffy stream exposes an interface to do these, with configurable high and low water marks.

const stream = makeStream(
  (push) => {
    eventSource.on('event', (event) => {
      const wait = push(event);
      if (wait) {
        eventSource.pause();
        wait.then(() => eventSource.resume());
      }
    });

    return () => eventSource.close();
  },
  { highWatermark: 255, lowWatermark: 4 },
);

If there are more than highWatermark unconsumed events, push() will return a Promise wait. As the consumer catches up and the number of unconsumed events fall below lowWatermark, this promise will resolve.

0.16.20-alpha.9

24 days ago

0.16.20-alpha.8

24 days ago

0.16.20-alpha.11

24 days ago

0.16.20-alpha.10

24 days ago

0.16.20-alpha.7

25 days ago

0.16.20-alpha.6

25 days ago

0.16.20-alpha.5

25 days ago

0.16.20-alpha.4

25 days ago

0.16.20-alpha.3

26 days ago

0.16.20-alpha.2

26 days ago

0.16.20-alpha.1

28 days ago

0.16.19-alpha.1

6 months ago

0.16.18

6 months ago

0.16.19

6 months ago

0.16.16

6 months ago

0.16.15

7 months ago

0.16.15-alpha.5

7 months ago

0.16.15-alpha.4

7 months ago

0.16.15-alpha.3

7 months ago

0.16.15-alpha.2

7 months ago

0.16.15-alpha.1

7 months ago

0.16.14

8 months ago

0.16.13

9 months ago

0.16.13-alpha.1

9 months ago

0.16.12

9 months ago

0.16.12-alpha.1

9 months ago

0.16.12-alpha.2

9 months ago

0.16.13-NaN.2

9 months ago

0.16.11

9 months ago

0.16.10-alpha.2

10 months ago

0.16.10-alpha.1

10 months ago

0.16.9-alpha.2

1 year ago

0.16.9-alpha.1

1 year ago

0.16.8

1 year ago

0.16.9

1 year ago

0.16.5

1 year ago

0.16.6

1 year ago

0.16.7

1 year ago

0.16.4

2 years ago

0.16.3-alpha.1

2 years ago

0.16.3-alpha.2

2 years ago

0.16.3-alpha.3

2 years ago

0.16.3-alpha.4

2 years ago

0.16.4-alpha.1

2 years ago

0.16.4-alpha.2

2 years ago

0.16.3

2 years ago

0.16.3-alpha.5

2 years ago

0.16.3-alpha.6

2 years ago

0.16.3-alpha.7

2 years ago

0.16.3-alpha.8

2 years ago

0.16.0-alpha.1

2 years ago

0.16.0-alpha.3

2 years ago

0.16.0-alpha.2

2 years ago

0.16.0-alpha.5

2 years ago

0.16.0-alpha.4

2 years ago

0.16.0-alpha.7

2 years ago

0.16.0-alpha.6

2 years ago

0.16.0-alpha.9

2 years ago

0.16.0-alpha.8

2 years ago

0.16.0-alpha.10

2 years ago

0.16.0-alpha.14

2 years ago

0.16.0-alpha.13

2 years ago

0.16.0-alpha.12

2 years ago

0.16.0-alpha.11

2 years ago

0.16.0

2 years ago

0.16.1

2 years ago

0.16.2

2 years ago

0.16.2-alpha.1

2 years ago

0.15.24

2 years ago

0.15.25

2 years ago

0.15.25-alpha.1

2 years ago

0.15.25-alpha.5

2 years ago

0.15.25-alpha.4

2 years ago

0.15.25-alpha.3

2 years ago

0.15.25-alpha.2

2 years ago

0.15.25-alpha.7

2 years ago

0.15.25-alpha.6

2 years ago

0.15.24-alpha.1

2 years ago

0.15.23

2 years ago

0.15.22-alpha.1

3 years ago

0.15.23-alpha.1

3 years ago

0.15.21

3 years ago

0.15.22

3 years ago

0.15.21-alpha.3

3 years ago

0.15.21-alpha.2

3 years ago

0.15.21-alpha.4

3 years ago

0.15.13-alpha.2

3 years ago

0.15.13-alpha.1

3 years ago

0.15.14-alpha.1

3 years ago

0.15.14-alpha.3

3 years ago

0.15.13

3 years ago

0.15.14

3 years ago

0.15.14-alpha.2

3 years ago

0.15.14-alpha.4

3 years ago

0.15.12

3 years ago

0.15.17

3 years ago

0.15.18

3 years ago

0.15.15

3 years ago

0.15.16

3 years ago

0.15.19

3 years ago

0.15.20

3 years ago

0.15.19-alpha.2

3 years ago

0.15.19-alpha.1

3 years ago

0.15.15-alpha.1

3 years ago

0.15.15-alpha.2

3 years ago

0.15.15-alpha.3

3 years ago

0.15.20-alpha.1

3 years ago

0.15.12-alpha.2

3 years ago

0.15.12-alpha.1

3 years ago

0.15.21-alpha.1

3 years ago

0.15.11

3 years ago

0.15.11-alpha.1

3 years ago

0.15.11-alpha.2

3 years ago

0.15.10-alpha.1

3 years ago

0.15.10

3 years ago

0.15.10-alpha.2

3 years ago

0.15.9

3 years ago

0.15.8

3 years ago

0.15.8-alpha.4

3 years ago

0.15.8-alpha.3

3 years ago

0.15.8-alpha.2

3 years ago

0.15.8-alpha.1

3 years ago

0.15.8-alpha.5

3 years ago

0.15.7

3 years ago

0.15.6

3 years ago

0.15.6-alpha.1

3 years ago

0.15.5

3 years ago

0.15.4

3 years ago

0.15.4-alpha.5

3 years ago

0.15.4-alpha.4

3 years ago

0.15.4-alpha.3

3 years ago

0.15.4-alpha.2

3 years ago

0.15.4-alpha.1

3 years ago

0.15.3

3 years ago

0.15.3-beta.6

3 years ago

0.15.3-beta.5

3 years ago

0.15.3-beta.4

3 years ago

0.14.8-beta.1

3 years ago

0.15.3-beta.3

4 years ago

0.15.3-beta.2

4 years ago

0.14.8-alpha.10

3 years ago

0.15.3-beta.1

4 years ago

0.14.8-alpha.9

4 years ago

0.14.8-alpha.8

4 years ago

0.14.8-alpha.6

4 years ago

0.14.8-alpha.5

4 years ago

0.14.8-alpha.4

4 years ago

0.14.8-alpha.3

4 years ago

0.14.8-alpha.2

4 years ago

0.14.8-alpha.1

4 years ago

0.15.2-alpha.3

4 years ago

0.15.2-alpha.2

4 years ago

0.15.1-alpha.1

4 years ago

0.15.0-alpha.1

4 years ago

0.15.0-alpha2

4 years ago

0.15.0

4 years ago

0.14.7-alpha.13

4 years ago

0.14.7-alpha.10

4 years ago

0.14.7-alpha.11

4 years ago

0.14.7-alpha.12

4 years ago

0.14.7-alpha.9

4 years ago

0.14.7-alpha.7

4 years ago

0.14.7-alpha.8

4 years ago

0.14.7-alpha.6

4 years ago

0.14.7-alpha.5

4 years ago

0.14.7-alpha4

5 years ago

0.14.7-alpha3

5 years ago

0.14.7-alpha1

5 years ago

0.14.7-alpha2

5 years ago

0.14.6

5 years ago

0.14.6-beta5

5 years ago

0.14.6-beta4

5 years ago

0.14.6-beta3

5 years ago

0.14.6-beta2

5 years ago

0.14.6-alpha10

5 years ago

0.14.6-alpha9

5 years ago

0.14.6-beta1

5 years ago

0.14.6-alpha8

5 years ago

0.14.6-alpha7

5 years ago

0.14.6-alpha5

5 years ago

0.14.6-alpha6

5 years ago

0.14.6-alpha4

5 years ago

0.14.6-alpha3

5 years ago

0.14.6-alpha2

5 years ago

0.14.5

5 years ago

0.14.6-alpha1

5 years ago

0.14.4

5 years ago

0.14.3

5 years ago

0.14.2

5 years ago

0.14.2-alpha3

5 years ago

0.14.2-alpha2

5 years ago

0.14.2-alpha1

5 years ago

0.14.1

5 years ago

0.14.1-alpha4

5 years ago

0.14.1-alpha3

5 years ago

0.14.1-alpha2

5 years ago

0.14.1-alpha1

5 years ago

0.14.0

5 years ago

0.13.12

5 years ago

0.13.11

5 years ago

0.13.10

5 years ago

0.13.8

5 years ago

0.13.9

5 years ago

0.13.6

5 years ago

0.13.7

5 years ago

0.13.4

5 years ago

0.13.5

5 years ago

0.13.2

5 years ago

0.13.3

5 years ago

0.13.1

5 years ago

0.13.0

5 years ago

0.12.3

5 years ago

0.12.2

5 years ago

0.12.1

5 years ago

0.12.0

5 years ago

0.11.0

5 years ago