Comments (14)
It seems like Interval
shares logic with the Timer
from issue #92.
Interval
could be implemented as a Timer
call with dueTime == 0
.
from rxjava.
Sure, as soon as Timer is implemented or schedulers can do periodic scheduling, the implementation of Interval
will get a lot simpler.
from rxjava.
Is the interval
operator intended to work if there are several subscribers? It looks as if this was simply forgotten...
from rxjava.
Actually, I'm not sure whether interval
itself should or shouldn't support this. - However, you should always be able to use publish
/connect
. If that doesn't work, then there's definitely something wrong. I can go check that later. I probably didn't think of that when implementing it...
from rxjava.
I just tried it out in C#. This code:
static void Main() {
var oneNumberPerSecond = Observable.Interval(TimeSpan.FromSeconds(1)).Take(5);
oneNumberPerSecond.Subscribe(
x => Console.WriteLine("subscriber 1 got " + x)
);
oneNumberPerSecond.Subscribe(
x => Console.WriteLine("subscriber 2 got " + x)
);
Console.ReadLine();
}
produces this output:
subscriber 2 got 0
subscriber 1 got 0
subscriber 2 got 1
subscriber 1 got 1
subscriber 2 got 2
subscriber 1 got 2
subscriber 2 got 3
subscriber 1 got 3
subscriber 2 got 4
subscriber 1 got 4
So I think we should update the RxJava version to support multiple subscribers.
from rxjava.
Compare to Java. Code:
public static void main(String[] args) {
Observable<Long> oneNumberPerSecond = Observable.interval(1, TimeUnit.SECONDS).take(5);
oneNumberPerSecond.subscribe(new Action1<Long>() {
public void call(Long x) {
System.out.println("subscriber 1 got " + x);
}
});
oneNumberPerSecond.subscribe(new Action1<Long>() {
public void call(Long x) {
System.out.println("subscriber 2 got " + x);
}
});
}
Output:
subscriber 1 got 0
subscriber 2 got 1
subscriber 1 got 2
subscriber 2 got 3
subscriber 1 got 4
subscriber 2 got 5
subscriber 1 got 6
subscriber 2 got 7
subscriber 1 got 8
subscriber 2 got 9
I think this will be used a lot for small examples and so it should be fixed soon. @benjchristensen could you please reopen this issue?
from rxjava.
What does Rx.NET do when you wait a bit before subscribing the second time?
Will it do this?
subscriber 1 got 0
subscriber 1 got 1
subscriber 1 got 2
subscriber 2 got 2
subscriber 1 got 3
subscriber 2 got 3
subscriber 1 got 4
subscriber 2 got 4
Or will it still start at 0 with subscriber 2?
from rxjava.
I added a first test for that use case here: jmhofer@2fe6da7
It fails, as expected from your comment above.
from rxjava.
The Rx Design Guidelines (5.10) say:
As many observable sequences are cold (see cold vs. hot on Channel 9), each subscription will have a separate set of side-effects. Certain situations require that these side-effects occur only once. The Publish operator provides a mechanism to share subscriptions by broadcasting a single subscription to multiple subscribers.
So I guess it would be okay for the second subscriber to always start at 0 too when subscribing to the same observable later (and not using publish
/connect
)?
from rxjava.
There are probably other operators that are affected by this, too, because multiple subscribers are currently normally not getting tested by the unit tests...
from rxjava.
Here's another example from C#:
static void Main() {
var oneNumberPerSecond = Observable.Interval(TimeSpan.FromSeconds(1)).Take(5);
var watch = new Stopwatch();
watch.Start();
Thread.Sleep(2200);
Console.WriteLine("subscriber 1 subscribes at t=" + watch.ElapsedMilliseconds);
oneNumberPerSecond.Subscribe(
x => Console.WriteLine("subscriber 1 got " + x + " at t=" + watch.ElapsedMilliseconds)
);
Thread.Sleep(1300);
Console.WriteLine("subscriber 2 subscribes at t=" + watch.ElapsedMilliseconds);
oneNumberPerSecond.Subscribe(
x => Console.WriteLine("subscriber 2 got " + x + " at t=" + watch.ElapsedMilliseconds)
);
Console.ReadLine();
}
outputs:
subscriber 1 subscribes at t=2200
subscriber 1 got 0 at t=3322
subscriber 2 subscribes at t=3615
subscriber 1 got 1 at t=4319
subscriber 2 got 0 at t=4642
subscriber 1 got 2 at t=5329
subscriber 2 got 1 at t=5643
subscriber 1 got 3 at t=6331
subscriber 2 got 2 at t=6643
subscriber 1 got 4 at t=7344
subscriber 2 got 3 at t=7655
subscriber 2 got 4 at t=8656
So each subscriber starts at 0.
from rxjava.
Great, thanks. This means that my PR above should fix this.
from rxjava.
Yes, every new subscriber should start the Observable from the beginning. I have tried to make sure that's the case everywhere but apparently missed this one.
If an Observable does not want that behavior that is what the various multicast
options are for such as publish
, replay
, cache
etc.
from rxjava.
Merged in #379 so closing again.
from rxjava.
Related Issues (20)
- v3: The marble diagram for cast is incredibly misleading.
- No Development on RxKotlin HOT 5
- missing PNG files in the documentation HOT 1
- 3.x: Items dropped by observeOn without backpressure errors HOT 2
- Consumer buffer of flowable leaks memory HOT 3
- UndeliverableException when use zip compose 2 observable , send Exceptions at same time in the two Observable 。 HOT 3
- 2.x: Infinity loop on subscription cancel HOT 6
- Weird Flowable behavior when flatMapCompletable is being executed in a different thread HOT 2
- Adopt the Scorecard Action to monitor the project's security posture
- DeferredScalarDisposable throws NullPointerException HOT 5
- `3.x` In README.md, add a link to the mentioned JavaFX Scheduler HOT 2
- Broken link in docs/Getting-Started.md
- Marble diagram for `Observable.concatMapMaybe` is wrong HOT 8
- repeatWhen terminate HOT 3
- What does it mean due to lack of requests in MissingBackpressureException HOT 2
- FlowableObserveOn.BaseObserveOnSubscriber will ignore the next datas when cosumer throw exception HOT 3
- Flowable.firstOrError interrupts the thread downstream. HOT 4
- io.reactivex.rxjava3.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with HOT 1
- Some Schedulers along with lockingGet will make the application hang HOT 1
- LambdaObserver fails to push exception to error channel and fails with NPE HOT 4
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 rxjava.