Comments (5)
I haven’t used observables in a while but the following code should work:
function fromObservable(observable) {
return new Repeater(async (push, stop) => {
const subscription = observable.subscribe({
next(value) {
push(value);
},
error(err) {
stop(err);
},
complete() {
stop();
},
});
await stop;
subscription.unsubscribe();
});
}
I haven’t run the code to check for errors but you should get the basic idea. Let me know if this works or you have any other questions! Maybe this could be added to the examples in quickstart if this is helpful.
from repeater.
As for converting an async iterator to an observable, here’s a rough sketch:
function toObservable(iterator) {
const subject = new Subject();
(async function() {
try {
for await (const value of iterator) {
subject.next(value);
if (subject.isStopped) {
break;
}
}
} catch (err) {
subject.error(err);
} finally {
subject.complete();
}
})();
return subject;
}
I honestly don’t know which “subject” to use here, you probably know better. Also I’m not 100% sure how to correctly close the async iterator when the subject is unsubscribed.
from repeater.
Thanks! It would be super helpful to add it as examples. Ideally, I would love to even have some project on npm, to do something like
npm install @repeaterjs/repeater-rxjs
And just use repeater altogether with my rjx observables.
I really see the parallel with Promise here. Sometimes in my code I want to express with creating new Promise() and chaininig it, but sometimes using async/await is much more comfortable for me. And I can always convert it back and forth if I need, so I ideally would like to have the same possibilities with repeater
from repeater.
Ideally, I would love to even have some project on npm, to do something like
npm install @repeaterjs/repeater-rxjs
Maybe. I’m hesitant to write such a package because:
-
Going from observables to repeaters (
fromObservable
) is trivial. I haven’t written utility functions for turning other callback-based APIs like mutation observers, event targets or node streams into repeaters because this is the 95% use-case for repeaters: setup callbacks, awaitstop
and teardown callbacks. It’s so easy and straightforward that I’d rather people just do it themselves. Additionally, you may want to patch the[Symbol.asyncIterator]
method of observables to make themfor…await
-able out of the box, without the need for a utility function. -
Going from async iterators to observables (
toObservable
) doesn’t require the repeater class and is much more involved. You can use the async iterator protocol andfor…await
exclusively to translate async iterators to observables and there is probably no need for repeaters. However, there are additional considerations you need to make which I am unsure of, like which kind of rxjs subject you want to use, whether you want the observable to buffer values, and when to break out of thefor…await
iterator loop.
I have no objection to people using the “repeater” name however, and I encourage you to create a package called repeater-rxjs
if you so desire! This is the nice thing about using a namespaced package scope; I don’t have to be protective about package names in the wild.
As far as actionability of this issue, I think I will definitely add some version of the fromObservable
function to the examples, insofar as this will help observable folks map the concept of observables to repeaters.
Again, thanks for the feedback, and I hope you have success with async iterators. If you have any other questions, do not hesitate to reply here or in further issues.
from repeater.
Observable example added to docs and README
from repeater.
Related Issues (20)
- Redesign Repeater.prototype.return and the stop promise.
- Missing documentation for packages HOT 2
- Not that great for implementing transducers? HOT 31
- Should the Repeater automatically stop when its executor fulfills? HOT 5
- Doesn't quite match AsyncIterable interface? HOT 3
- push*(repeater) type API HOT 3
- Some of the more fragile tests are failing in newer versions of node HOT 1
- Question about the abilities of async iteration HOT 2
- Repeater.race typings are incorrect HOT 1
- Aggressive memory leak in Repeater.race HOT 2
- 4.0 changes HOT 4
- TypeError: Invalid attempt to iterate non-iterable instance. HOT 5
- WeakMap error at Repeater.return HOT 8
- RepeatOrNot HOT 1
- Question about merging two Repeaters HOT 2
- Help with map transducer? HOT 13
- Stop promise not entirely compatible with Promise.race
- Event loop can die if stop() not called
- TS errors when using "module": "NodeNext" and module type is commonjs
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from repeater.