Await .onFirstEmitted() and .onEmitted(n); of observable values and then continue
See original GitHub issueFirst off, huge fan of observer-spy! What a life-saver!
Is your feature request related to a problem? Please describe.
Comparable to .onComplete()
and .onError()
, it would be very useful to have something that awaits the first or n value emitted by an observable before continuing. This is because not all observables should be completing and need to stay alive. Observable Store is a good example of this where I would like a (Vue) component to keep being updated when ever the store is updated, which means I don’t want to complete the observable.
Describe the solution you’d like
Something like this maybe:
const test$ = of([0,1,2,3,4,5,6,7,8,9,10]);
const _test= subscribeSpyTo(test$);
await _test.onFirstEmitted();
expect(_test.getFirstValue()).toEqual(0);
await _test.onEmitted(5);
expect(_test.getValues()).toEqual([0,1,2,3,4]);
Describe alternatives you’ve considered
Continuing from the example above:
const test$ = of([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
const testTakeOne$ = test$.pipe(take(1));
const _testTakeOne = subscribeSpyTo(testTakeOne$);
await _testTakeOne.onComplete();
expect(_testTakeOne.getFirstValue()).toEqual(0);
const testTakeFive$ = test$.pipe(take(5));
const _testTakeFive = subscribeSpyTo(testTakeFive$);
await _testTakeFive.onComplete();
expect(_testTakeFive.getValues()).toEqual([0, 1, 2, 3, 4]);
Additional context So before making this post I thought this couldn’t be done but by rubber ducking I realized that it could be done, albeit in a convoluted way. If nothing else, I could most likely write an extension method for it.
Thanks for reading and please like and subscribe if you want more feature requests!
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
Haha I will keep it in mind, thanks for the consideration!
Thanks @JasonLandbridge ! lol, I subscribed 😃
I like the idea I think I’d change
onEmitted
toonCount
oronValuesCount
to make it more clear… becauseonEmitted
might also be referring to the values themselves (like when the number 5 arrives, stop)Mind creating a PR?