meros
meros copied to clipboard
Support callback/observable style API
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.
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?
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 😄
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.
@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.
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!
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.
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.
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?
Maybe in another life.