GithubHelp home page GithubHelp logo

Operator: Interval about rxjava HOT 14 CLOSED

reactivex avatar reactivex commented on July 17, 2024
Operator: Interval

from rxjava.

Comments (14)

mairbek avatar mairbek commented on July 17, 2024

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.

jmhofer avatar jmhofer commented on July 17, 2024

Sure, as soon as Timer is implemented or schedulers can do periodic scheduling, the implementation of Interval will get a lot simpler.

from rxjava.

samuelgruetter avatar samuelgruetter commented on July 17, 2024

Is the interval operator intended to work if there are several subscribers? It looks as if this was simply forgotten...

from rxjava.

jmhofer avatar jmhofer commented on July 17, 2024

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.

samuelgruetter avatar samuelgruetter commented on July 17, 2024

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.

samuelgruetter avatar samuelgruetter commented on July 17, 2024

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.

jmhofer avatar jmhofer commented on July 17, 2024

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.

jmhofer avatar jmhofer commented on July 17, 2024

I added a first test for that use case here: jmhofer@2fe6da7

It fails, as expected from your comment above.

from rxjava.

jmhofer avatar jmhofer commented on July 17, 2024

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.

jmhofer avatar jmhofer commented on July 17, 2024

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.

samuelgruetter avatar samuelgruetter commented on July 17, 2024

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.

jmhofer avatar jmhofer commented on July 17, 2024

Great, thanks. This means that my PR above should fix this.

from rxjava.

benjchristensen avatar benjchristensen commented on July 17, 2024

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.

benjchristensen avatar benjchristensen commented on July 17, 2024

Merged in #379 so closing again.

from rxjava.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.