meros icon indicating copy to clipboard operation
meros copied to clipboard

Support callback/observable style API

Open zth opened this issue 2 years ago • 8 comments

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.

zth avatar Apr 20 '22 07:04 zth

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?

maraisr avatar Apr 20 '22 08:04 maraisr

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 😄

zth avatar Apr 20 '22 13:04 zth

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 avatar Apr 20 '22 21:04 maraisr

@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.

maraisr avatar Apr 21 '22 05:04 maraisr

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 avatar Apr 21 '22 11:04 zth

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 avatar Apr 21 '22 18:04 zth

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 avatar Apr 21 '22 18:04 zth

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?

zth avatar Apr 28 '22 18:04 zth

Maybe in another life.

maraisr avatar Sep 03 '22 01:09 maraisr