首页 \ 问答 \ 仅在事件发生后才发出?(Emitting only after set of events?)

仅在事件发生后才发出?(Emitting only after set of events?)

我正在尝试做类似以下的事情 - B是独立于A发射的,但我想在A之后发生B(按此顺序)时发射。

----A--------A-->
B--B--B--B----B->

------B-------B->

谢谢!


I'm trying to do something like the following - B is being emitted independently of A, but I want to emit when there is a B that happens after an A (in that order).

----A--------A-->
B--B--B--B----B->

------B-------B->

Thanks!


原文:https://stackoverflow.com/questions/43188180
更新时间:2021-06-07 10:06

最满意答案

如果你有一个热的observable,并且想要从B发出值,那么你可能想要将switchMaptake(1)结合使用:(基本上只有3个最后一行是相关的,上面部分只是嘲笑一些数据流)

// Mocking A and B
const streamA$ = Rx.Observable
  .interval(2500)
  .do(() => console.log("Emitting on A => TAKE NEXT B!!!"))
  .share();
const streamB$ = Rx.Observable
  .interval(1200)
  .do(data => console.log("Emitting on B: " + data))
  .publish();
streamB$.connect();
// \End of mocking

streamA$
  .switchMap(() => streamB$.take(1))
  .subscribe(data => console.info("Took value: " + data));
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>


如果你有冷可观察量,你可以结合map使用window

// Mocking A and B
const streamA$ = Rx.Observable
  .interval(4000)
  .do(() => console.log("Emitting on A => TAKE NEXT B!!!"));
const streamB$ = Rx.Observable
  .interval(900)
  .do(data => console.log("Emitting on B: " + data));
// \End of mocking

streamB$.window(streamA$)
  .skip(1)                 // skip the first window, since this will be emitted before A emitted the first time
  .mergeMap(win => win.take(1)) // each window should have at most 1 emission
  
  .subscribe(data => console.info("Took value: " + data));
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>


In case you have a hot observable and you want to emit the values from B, then you might want to use a switchMap in combination with a take(1): (basically just the 3 last lines are relevant, the upper part is just mocking some data-stream)

// Mocking A and B
const streamA$ = Rx.Observable
  .interval(2500)
  .do(() => console.log("Emitting on A => TAKE NEXT B!!!"))
  .share();
const streamB$ = Rx.Observable
  .interval(1200)
  .do(data => console.log("Emitting on B: " + data))
  .publish();
streamB$.connect();
// \End of mocking

streamA$
  .switchMap(() => streamB$.take(1))
  .subscribe(data => console.info("Took value: " + data));
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>


If you have cold observables you could use window in combination with map:

// Mocking A and B
const streamA$ = Rx.Observable
  .interval(4000)
  .do(() => console.log("Emitting on A => TAKE NEXT B!!!"));
const streamB$ = Rx.Observable
  .interval(900)
  .do(data => console.log("Emitting on B: " + data));
// \End of mocking

streamB$.window(streamA$)
  .skip(1)                 // skip the first window, since this will be emitted before A emitted the first time
  .mergeMap(win => win.take(1)) // each window should have at most 1 emission
  
  .subscribe(data => console.info("Took value: " + data));
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>

相关问答

更多

一个div的孩子是否有可能设置指针事件:没有指针事件?(Is it possible for the child of a div set to pointer-events: none to have pointer events?)

是的,这是可能的,而你基本上只是描述了如何。 禁用它为父项并为其启用它: CSS: .parent { pointer-events:none; } .child { pointer-events:all; } HTML: <div class="parent"> <a href="#">Parent</a> <div class="child"> <a href="#">Child</a> </div> ​ ...

在所有事件发生后(或组合事件),C#都会进行一些工作,(C# do some work after all events are raised (or combine events))

如果我正确地理解了这个问题,当你开始你的A任务并且每个任务完成时你增加一个计数器,你在事件处理程序中递减计数器。 同样在事件处理程序中,如果计数器为零,则检查(在递减计数器之后)。 如果是这样,你做任务B. 我建议你看看任务 ( 又名“任务并行库(TPL)” ),它允许你做这样的事情: Task.WhenAll( new Task[] { Task.Run(()=> { //... do work A1... }, Task.Run(()=> { //... do work A2. ...

DataGridView事件(DataGridView Events)

您可以做的是在DataGridView的EditingControlShowing事件中注册EditingControl的PreviewKeyDown事件。 从那里可以检测是否在编辑控件中按下了escape,并设置了一个将由CellEndEdit事件读取的标志。 您可以从方法名称推断要注册的必要事件。 这假设您的类中有一个名为escapePressed的bool字段,这个字段(毫不奇怪)是按下escape的标志。 private void dataGridView1_EditingControlS ...

需要为Hyperledger Composer事件设置什么功能?(What needs to be set up for Hyperledger Composer events to work?)

所以 在您的TP代码中发出您的事件(并且您已完成此操作) - 示例 - > https://github.com/hyperledger/composer-sample-networks/blob/master/packages/trade-network/lib/logic.js #L28或如https://hyperledger.github.io/composer/business-network/publishing-events.html所示 已经在模型文件中定义了Event类 - 例如 ...

Rails - 删除索引页面后的事件信息(发生后)(Rails - Remove Event information from Index page post-date (after it has happened))

添加名为:time的日期时间字段。 确保在创建事件时将:time属性设置为事件发生的时间。 完成后,您可以在事件模型上添加范围。 # You can call the scope whatever. # :happened is the boolean field on the events table. scope :not_yet_done, -> { where('time >= ?', Time.current) } 在您的索引操作中: def index if params[:c ...

PAPI和本地事件(PAPI and native events)

如果您安装了papi,则可以使用papi_avail获取本机事件。 例如, papi_avail -e PAPI_TOT_INS 事件名称:PAPI_TOT_INS 事件代码:0x80000032 本地事件数量:1简要说明:| Instr completed | 详细描述:|说明完成| 开发人员手记:|| 派生类型:| NOT_DERIVED | 后缀处理字符串:|| 本机代码[0]:0x40000002 | INSTRUCTION_RETIRED | 寄存器数量:0 本机事件描述:|退休时的指示 ...

allegro 5没有事件发生(allegro 5 no events work)

我看了同样的教程。 您必须将al_register_event_source(event_queue,al_get_display_event_source(display))添加到main。 I watched the same tutorial. You have to add al_register_event_source(event_queue, al_get_display_event_source(display)) to main.

同步事件(Synchronizing Events)

由于OpenDevice是异步的(正如您在注释中提到的),因此它在与调用者不同的线程中运行。 有时它会在源代码执行的下一行之前完成: OpenDevice(); // Async: may finish before the next line executes! WaitForEvent = EventEnum.DeviceOpened; 当发生这种情况时, DeviceOpenedEvent不会执行您想要的操作,因为WaitForEvent仍然是EventEnum.None ...

如何仅在Textarea上设置jQuery事件?(How to set jQuery Events only on Textarea?)

$("#divUnique").on("click change keypress","#txt",function(){ // your work goes here }); 参考 $("#divUnique").on("click change keypress","#txt",function(){ // your work goes here }); reference on

仅在事件发生后才发出?(Emitting only after set of events?)

如果你有一个热的observable,并且想要从B发出值,那么你可能想要将switchMap与take(1)结合使用:(基本上只有3个最后一行是相关的,上面部分只是嘲笑一些数据流) // Mocking A and B const streamA$ = Rx.Observable .interval(2500) .do(() => console.log("Emitting on A => TAKE NEXT B!!!")) .share(); const streamB$ ...

相关文章

更多

最新问答

更多
  • 如何从远程文件拉取文件而不覆盖本地文件?(How do I pull files from remote without overwriting local files?)
  • Reactjs:状态改变时重新渲染iframe(Reactjs: re-renders iframes when state changed)
  • 奇怪的网址,以及跟随php页面流程的困难(odd url, and difficulty in following the php page flow)
  • 标签活动无效(Tab Activity is not working)
  • JavaME合适的语法编译器建议?(JavaME-suitable grammar compiler recommendations?)
  • 指定参数(Specifying arguments)
  • 可以通过Ruby插件或控制台覆盖Sketchup中的键盘快捷键吗?(Can one override keyboard shortcuts in Sketchup through the a Ruby Plugin or Console?)
  • 计算Java EE Web App中用户数的最佳方法(Best way to count number of users in a Java EE web App)
  • 无法使用templateUrl加载cordova中的外部模板(unable to load external templates in cordova with templateUrl)
  • PHPExcel:写入期间无法使用缓存(PHPExcel: Unable to use cache during write)
  • 在javascript中嵌套这个指针(nested this pointer in javascript)
  • 谁跟领航致远培训过,有问题问下啊
  • 控制器要求在入门时下载(Controller ask to download on entry)
  • 未能通过conda安装Asyncio(Failure to install Asyncio via conda)
  • 如何查找已完成项目的总长度?(How to find length of total completed items?)
  • 如何检查OleInitialize是否已被调用?(How to check if OleInitialize has already been called?)
  • SQL在特定范围内返回列中具有最大值的行(SQL Returning rows with max value in column, within a specific range)
  • preg_match从url获取id(preg_match get the id from url)
  • 如何在运算符中为make方程转换perl变量?(How to convert a perl variable in a operator for make equations?)
  • 在导航上方添加空格/标题。(Add a white space/ header above navigation.)
  • MeetingItem已保存;(MeetingItem saved; but change now shown in Calendar)
  • c#vb:我们应该使用System.Lazy进行资源密集型任务吗?(c# vb: Should we use System.Lazy for resource-intensive task? (when threading is not needed))
  • 为什么在armeabi代码中使用armeabi-v7a代码?(Why use armeabi-v7a code over armeabi code?)
  • 获取请求的自定义标头(Java HTTP)(Fetching a custom header of a request (Java HTTP))
  • 是否可以在嵌套的if语句中从varchar转换为numeric以动态评估参数?(Is it possible to convert from varchar to numeric within a nested if statement in order to dynamically evaluate a parameter?)
  • 如何将Html.ActionLink转换为链接到Ajax调用的按钮?(How to convert from Html.ActionLink to a button linked to Ajax call?)
  • 应用程序如何处理Windows符号链接?(How are Windows symbolic links treated by the apps?)
  • html,js,css在jsfiddle中工作,但不在sharepoint中(html, js, css works in jsfiddle but not in sharepoint)
  • 从Ruby脚本调用Elasticsearch Rest API(Calling Elasticsearch Rest API from Ruby script)
  • 如何将嵌套setTimeouts转换为承诺(How to convert nested setTimeouts to promises)