maraisr / meros

🪢 A fast utility that makes reading multipart responses simple
MIT License
187 stars 10 forks source link

Support callback/observable style API #16

Closed zth closed 2 years ago

zth commented 2 years ago

As per discussions on Discord, it'd be nice to have a callback/observable style API in meros, for cases where using async iterators etc aren't possible/desirable. Adding a ticket here to track that.

maraisr commented 2 years ago

Hi 👋🏻 @zth

Yeah good moment, thank you for this! Just trying to make sure the api feels right. It'll probably end up something like;

import { meros } from 'meros/observable';

const stream = await fetch('..').then(meros);

stream.subscribe((part) => {
   // ...
});

how does that feel? Naturally there would be an unsubscribe method as well, and for sure will not have a single generator at play.

Can I just why? async generators look like they are well supported?

zth commented 2 years ago

I think that looks great! How would errors/completion be handled?

Re why: this particular project targets old(er) TVs, where async iterators aren't supported, and where the code base itself has certain restrictions on the JS features it uses overall (even transpiled).

But outside of that, I think an observable/callback based API is more approachable in general, especially to some segments of developers. That's obviously my highly personal opinion, you'll of course need to decide whether you think those things are worth implementing and supporting that alternative API 😄

maraisr commented 2 years ago

How would errors/completion be handled?

yeah! Great point, how does rxjs do it? its a second argument iirc .subscribe(onNext, onError) 🤔

and ya re the why point. Makes perfect sense. I'll see what I can muster up.

maraisr commented 2 years ago

@zth I've had a play and pushed to this branch https://github.com/maraisr/meros/tree/feat/observable

the node variant still needs some work, to read the stream is just so easy with async iterables as node handles all the listeners about cleaning up etc...

Need to still have play in that space. could be as simple as .on('data'), .on('error') and so on.

zth commented 2 years ago

That's really cool! Let me see if I can try it out in my project this evening, I'll get back to you in ~6-7hrs or so. Thank you!

zth commented 2 years ago

Just tested it and it seems to work well, nice work! 😄 I look forward to trying the Node parts too! Will see if I can get some SSR going.

zth commented 2 years ago

Kind of a separate question - have you considered running non-multipart responses through the same API (just that it'd complete directly after a single part)?

That way one could always do subscribe etc in the observable case, and not worry about whether it is a multipart response or not. One could set up one flow that handles them both.

zth commented 2 years ago

Sorry to ping you @maraisr , just very excited about trying out the SSR 😆 Can I do anything to help move this forward? Maybe if you guide me I could try to implement what's left?

maraisr commented 2 years ago

Maybe in another life.