ReactiveX / RxRuby

Reactive Extensions for Ruby
Other
962 stars 75 forks source link

start_with bug #94

Open igneus opened 8 years ago

igneus commented 8 years ago

There seems to be a bug in the Ruby implementation of start_with. The two examples below, one written using RxRb, the other using RxPy, construct the same observable, emitting a number every two seconds. To the observable two observers are subscribed, which print the received value in colour. While the Python code prints a series of numbers in both colours, the Ruby code only produces yellow numbers - the blue observer never receives it's input.

Ruby example

require 'colorize'
require 'rx'

observable =
  Rx::Observable
  .interval(2)
  .start_with(-1)

observable.subscribe {|i| puts i.to_s.colorize(:yellow) }
observable.subscribe {|i| puts i.to_s.colorize(:blue) }

while Thread.list.size > 1
  (Thread.list - [Thread.current]).each &:join
end

Python example

import asyncio
import rx
import time
from termcolor import colored

class ColouredObserver:
    def __init__(self, colour):
        self.colour = colour

    def on_next(self, x):
        self._print("Got: %s" % x)

    def on_error(self, e):
        self._print("Got error: %s" % e)

    def on_completed(self):
        self._print("Sequence completed")

    def _print(self, what):
        print(colored(what, self.colour))

observable = rx.Observable.interval(2000).start_with(-1)

observable.subscribe(ColouredObserver('yellow'))
observable.subscribe(ColouredObserver('blue'))

time.sleep(20)
igneus commented 7 years ago

I should have added that when I remove the .start_with(-1) part in the Ruby example, it works as expected: it waits two seconds and then starts printing regularly in both colours.