Rx跳过,直到几秒钟过去

时间:2017-12-14 17:22:02

标签: rx-java rx-java2 rx-android

我有一个每隔X秒发出一次信号的PublishSubject,我想只考虑Y秒后发出的第一个项目。

实施例

  • observable A每秒发出一次“tick”
  • 可观察B应该每隔5秒发出一次“滴答”,忽略 介于两者之间。

也就是说,跳过每个项目,直到经过一定的时间跨度。

这是一个去抖动,导致去抖动需要原始的Observable在没有发射的情况下保持5秒,但我的每秒都在发射。

2 个答案:

答案 0 :(得分:2)

请查看我的解决方案。 ' tickEverySecond'将每秒发出一个值。 '导致'将收集由' tickEverySecond'发出的所有项目。在5秒的时间窗口内。每隔5秒窗口,最后一次发出的值是来自' tickEverySecond'将推送给订户。

public class Startup
{
    string _testSecret = null;

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        _testSecret = Configuration["MySecret"];
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>(config =>
        {
            config.SignIn.RequireConfirmedEmail = true;
        })
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

        // Add application services.
        services.AddTransient<IEmailSender, EmailSender>();

        services.AddMvc();
        services.Configure<AuthMessageSenderOptions>(Configuration);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
            app.UseDatabaseErrorPage();
            builder.AddUserSecrets<Startup>();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

您可以使用以下运算符获得相同的效果:

@Test
  public void name() {
    TestScheduler testScheduler = new TestScheduler();
    Flowable<Long> tickEverySecond = Flowable.interval(0, 1, TimeUnit.SECONDS, testScheduler);

    Flowable<Long> result = tickEverySecond.window(5, TimeUnit.SECONDS, testScheduler).flatMap(longFlowable -> longFlowable.takeLast(1));

    TestSubscriber<Long> test = result.test();

    testScheduler.advanceTimeTo(4, TimeUnit.SECONDS);
    test.assertValueCount(0).assertNotComplete();

    testScheduler.advanceTimeTo(5, TimeUnit.SECONDS);
    test.assertValue(4L).assertNotComplete();

    testScheduler.advanceTimeTo(9, TimeUnit.SECONDS);
    test.assertValues(4L).assertNotComplete();

    testScheduler.advanceTimeTo(10, TimeUnit.SECONDS);
    test.assertValues(4L, 9L).assertNotComplete();

    testScheduler.advanceTimeTo(14_999, TimeUnit.MILLISECONDS);
    test.assertValues(4L, 9L).assertNotComplete();
  }

答案 1 :(得分:0)