RXJS:如何从Promise中扼杀Observable

时间:2018-05-22 09:32:18

标签: rxjs

我正在使用RxJS 5.5.10。

我试着每隔5秒就瞄准一个观察者。

此Observable基于Promise。

public void ConfigureAuth(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

    app.UseCookieAuthentication(new CookieAuthenticationOptions() {
        ExpireTimeSpan=TimeSpan.MaxValue,
        Provider = new CookieAuthenticationProvider()
        {
            OnValidateIdentity = ctx => {

                var loggedClaim=ctx.Identity.FindFirst("loggedTicks")?.Value;
                if (loggedClaim != null)
                {
                    var loggedDateTime = new DateTime(long.Parse(loggedClaim), DateTimeKind.Utc);

                    if (loggedDateTime.AddHours(1) < DateTime.UtcNow)
                    {
                        ctx.RejectIdentity();
                        ctx.OwinContext.Authentication.SignOut(OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
                    }
                }
                return Task.FromResult(0);
            }
            }
    });

    app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            ClientId = clientId,
            Authority = authority,
            PostLogoutRedirectUri = postLogoutRedirectUri,
            RedirectUri = postLogoutRedirectUri,
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthenticationFailed = context => 
                {
                    context.HandleResponse();
                    context.Response.Redirect("/Error?message=" + context.Exception.Message);
                    return Task.FromResult(0);
                },
                    SecurityTokenValidated = async (x) =>
                    {
                        var identity = x.AuthenticationTicket.Identity;

                        //add a additional claim which represents the current user logged UTC time ticks
                        identity.AddClaim(new System.Security.Claims.Claim("loggedTicks", DateTime.UtcNow.Ticks.ToString()));

                        await Task.FromResult(0);
                    }
            }
        });
} 

据我所知,我可以使用油门操作符仅在给定时间后发出值

  Rx.Observable.fromPromise(mongo.AllWishes)
    .flatMap(array => Rx.Observable.from(array))
    .pluck('url')
    .filter(s => s !== undefined)
    .subscribe(m => console.log(m))

但是当我尝试像

这样的东西时
Rx.Observable.interval(1000)
  .throttle(val => Rx.Observable.interval(5000)
  .subscribe(m => console.log('ping'))

我收到错误

  Rx.Observable.fromPromise(mongo.AllWishes)
    .throttle(val => Rx.Observable.interval(5000))
    .flatMap(array => Rx.Observable.from(array))
    .pluck('url')
    .filter(s => s !== undefined)
    .subscribe(m => console.log(m))

我错过了什么? 感谢您的帮助

1 个答案:

答案 0 :(得分:1)

我对你的期望并不完全清楚。看起来你正在从一个承诺中获取一个数组,然后想要在每个项目之间按顺序发出每个值5秒。

如果是这样,我认为这应该做你想要的。至于你的错误,很难说不能运行你的代码。我假设它与你的承诺有关,因为我可以用自己的承诺替换mongo.AllWishes并且它没有错误。

const data = [
  { url: 'https://something.com/1' },
  { url: 'https://something.com/2' },
  { url: 'https://something.com/3' },
  { url: 'https://something.com/4' },
  { url: 'https://something.com/5' }
];
const myPromise = new Promise((resolve) => {
	setTimeout(() => { resolve(data); }, 1000);
});


Rx.Observable.fromPromise(myPromise)
	.flatMap(x => {
  	return Rx.Observable.timer(0, 1000)
  		.takeWhile(i => i < x.length)
  		.map(i => x[i]);
	})
	.pluck('url')
  .subscribe((url) => { console.log(url); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.10/Rx.min.js"></script>