- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经设置了一个史诗,等待另一个史诗完成,就像@jayphelps在这里的回答:Invoking epics from within other epics
但是我发现它似乎只运行一次。之后,我可以在控制台中看到 CART_CONFIG_READY
操作,但 DO_THE_NEXT_THING
操作未触发。
我尝试了 mergeMap
和 switchMap
的各种组合,无论有没有 take
,但似乎都没有帮助。
这(有点)是我的代码的样子。
import { NgRedux } from '@angular-redux/store';
import { Observable } from 'rxjs/Observable';
import { ActionsObservable } from 'redux-observable';
export class CartEpicsService {
checkCart = (action$: ActionsObservable<any>, store: NgRedux<any>) => {
return action$.ofType('CHECK_CART')
.switchMap(() => {
console.log('___LISTENING___');
return action$.ofType('CART_CONFIG_READY')
.take(1) // removing this doesn't help
.mergeMap(() => {
console.log('___RECEIVED___');
// do stuff here
return Observable.of({
type: 'DO_THE_NEXT_THING'
});
})
.startWith({
type: 'GET_CART_CONFIG'
});
});
}
getCartConfig = (action$: ActionsObservable<any>, store: NgRedux<any>) => {
return action$.ofType('GET_CART_CONFIG')
.switchMap(() => {
const config = store.getState().config;
// we already have the config
if (config) {
return Observable.of({
type: 'CART_CONFIG_READY'
});
}
// otherwise load it from the server using out HTTP service
return this.http.get('/cart/config')
.switchMap((response) => {
return Observable.concat(
Observable.of({
type: 'CART_CONFIG_SUCCESS'
}),
Observable.of({
type: 'CART_CONFIG_READY'
})
);
})
.catch(error => Observable.of({
type: 'CART_CONFIG_ERROR',
error
}));
});
}
}
对于上下文,我需要来自/cart/config 端点的响应来检查购物车的有效性。我只需要下载一次配置。
这是一个在 JS Bin 上可运行的示例:
最佳答案
这绝对是一个棘手的问题!
何时 state.config === true
你返回一个 Observable CART_CONFIG_READY
同步发出,而在第一次 http 请求(或延迟,在 jsbin 中)意味着它总是异步的。
为什么这会产生影响,请参见 checkCart
史诗般的,你返回一个可观察的链,监听 CART_CONFIG_READY
与 action$.ofType('CART_CONFIG_READY')
但也适用 .startWith({ type: 'GET_CART_CONFIG' })
。这意味着GET_CART_CONFIG
将在 之前 action$.ofType('CART_CONFIG_READY')
同步发出已订阅,因为 startWith
是 basically shorthand for a concat ,如果您熟悉的话,这可能会使问题更加清晰。这与执行此操作几乎完全相同:
Observable.concat(
Observable.of({
type: 'GET_CART_CONFIG'
}),
action$.ofType('CART_CONFIG_READY') // not subscribed until prior complete()s
.take(1)
.mergeMap(() => {
// stuff
})
);
总结一下,第二次发生了什么 GET_CART_CONFIG
同步调度,getCartConfig
收到它并看到配置已经在存储中,因此它同步调度 CART_CONFIG_READY
。但我们还没有在 checkCart
中监听它。所以它没有得到答复。然后该调用堆栈返回,并返回 concat 中的下一个 Observable,即我们的 action$.ofType('CART_CONFIG_READY')
链,被订阅。但为时已晚,它监听的 Action 已经发出了!
解决此问题的一种方法是发出 CART_CONFIG_READY
始终异步,或者在我们调度 GET_CART_CONFIG
之前开始在另一个史诗中监听它.
Observable.of
接受调度程序作为其最后一个参数,并且 RxJS supports several of them 。
在这种情况下,您可以使用 AsyncScheduler
(宏任务)或 AsapScheduler
(微任务)。在这种情况下,两者都可以工作,但它们在 JavaScript 事件循环中安排在不同的时间。如果您不熟悉事件循环任务,check this out .
我个人建议使用AsyncSheduler
在这种情况下,因为它将提供与发出 http 请求最接近的异步行为。
import { async } from 'rxjs/scheduler/async';
// later inside your epic...
return Observable.of({
type: 'CART_CONFIG_READY'
}, async);
CART_CONFIG_READY
在发射之前GET_CART_CONFIG
因为startWith
是 concat
的简写(我们不想这样做)我们需要使用某种形式的 merge
,与我们的 ofType
首先链,以便我们在发出之前进行监听。
action$.ofType('CART_CONFIG_READY')
.take(1)
.mergeMap(() => {
// stuff
})
.merge(
Observable.of({ type: 'GET_CART_CONFIG' })
)
// or
Observable.merge(
action$.ofType('CART_CONFIG_READY')
.take(1)
.mergeMap(() => {
// stuff
}),
Observable.of({ type: 'GET_CART_CONFIG' })
)
// both are exactly the same, pick personal preference on appearance
您只需执行这些解决方案之一,但同时执行这两个解决方案也没有什么坏处。我可能会建议同时使用两者,以便事情保持一致和预期,即使它们有点冗长。
您可能也会很高兴知道 Observable.of
接受任意数量的项目,这些项目将按顺序发出。所以你不需要使用concat
:
// before
Observable.concat(
Observable.of({
type: 'CART_CONFIG_SUCCESS'
}),
Observable.of({
type: 'CART_CONFIG_READY'
})
)
// after
Observable.of({
type: 'CART_CONFIG_SUCCESS'
}, {
type: 'CART_CONFIG_READY'
})
非常感谢 jsbin 顺便说一句,它使调试变得容易容易。
根据您的评论进行编辑:
Out of curiosity did you figure this out through experience or debugging?
两者的结合。我处理过大量的异步/预定代码,而排序通常是问题的根源。我扫描了代码,在脑海中想象执行情况,注意到异步与同步取决于代码路径的差异,然后我创建了一个快速运算符,让我可以轻松确认任何 Observable 链的订阅顺序。
Observable.prototype.logOnSubscribe = function (msg) {
// defer is a pretty useful Observable to learn if you haven't yet
return Observable.defer(() => {
console.log(msg);
return this; // the original source
});
};
我把它应用到了好几个地方,但最重要的是这两个地方:
action$.ofType('CART_CONFIG_READY')
.take(1)
.mergeMap(() => {
// stuff
})
.logOnSubscribe('listening for CART_CONFIG_READY') // <--- here
.startWith({
type: 'GET_CART_CONFIG'
});
// and in the other epic...
if (hasConfig) {
return Observable.of({
type: 'CART_CONFIG_READY'
})
.logOnSubscribe('emitting CART_CONFIG_READY'); // <--- and here
}
确认在第二个代码路径 CART_CONFIG_READY
在另一个史诗监听它之前就被发出了。
关于rxjs - 链式 redux-observable 史诗仅正确触发一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46450262/
这个问题已经有答案了: jQuery trigger click vs click ()? (3 个回答) 已关闭 5 年前。 我无法区分 trigger('click')与 trigger('cli
我正在运行 VS 2008 和 .NET 3.5 SP1。 我想在 HttpModule 中实现命中跟踪在我的 ASP.NET 应用程序中。很简单,我想。然而,BeginRequest我的事件 Htt
这是一段代码,我收到以下错误 #1064 - You have an error in your SQL syntax; check the manual that corresponds to yo
有没有办法用任意增量触发滚轮事件。就像 jQuery 对“点击”所做的那样: $('#selector').trigger('click'); 我需要类似的东西,只需一个滚轮即可: $('#selec
我正在尝试在配音数据库中触发时间。我想检查一下在不出现角色的电影配音中不能对角色进行配音。这是PDM: 和CDM 我是SQL的初学者,但我知道表“DUBBES”中应该有一些触发器。我试图做这样的事情,
这个问题已经有答案了: jquery programmatically click on new dom element (3 个回答) 已关闭 6 年前。 我有一个 jQuery 事件定义如下: $
主菜单的点击代码适用于类更改,但不适用于子菜单...当单击食物或鞋子等子菜单项时,它不会触发警报命令...事实上,悬停非常适合子菜单但不是活跃的 HTML
问题非常简单: $('#btn1').click(function(event){ alert( "pageX: " + event.pageX + "\npa
我使用 Spring 的调度程序 (@EnableScheduling) 并具有以下 @Scheduled 方法,该方法每分钟调用一次: @Component public class Schedul
错误 SQL 查询:文档 CREATE TRIGGER `triggers_div` AFTER INSERT ON `produits` FOR EACH ROW BEGIN INSERT INTO
我想在插入另一个表时填充表中的一些列值,并为特定列设置条件。我使用触发器: CREATE TRIGGER inserttrigger AFTER INSERT ON table1 FOR EACH R
我可以在 5.6 MySQL 环境中使用一些关于触发器的指导。我想创建一个触发器,如果发现具有相同速度的电脑的价格较低,则该触发器会停止更新。 架构是产品(制造商、型号、类型)PC(型号、速度、内
背景:我们有一个 completed_flag,默认为 0,当有人完成调查时更新为 1。我想记录这次更新发生的时间戳 在编写了这个触发器/函数以在标志从 0 触发到 1 时更新时间戳后,我怀疑我这样做
数据库中有两个表 KistStatus和 LastKistStatus .后者将保存 KistStatus 的所有“最新”值。 . KistStatus有大约 174.000 条记录,LastKist
我正在开发一个使用 APNS 的 iPhone 应用程序。我很清楚实现 APNS、创 build 备 token 的过程,等等等等……我不知道如何通过 Web 服务从提供商端触发和启动 APNS。任何
我有这个 javascript,当数量更改时会触发 update_cart... jQuery('div.woocommerce').on('change', '.qty', function
当我单击任何按钮时,click 事件不会被触发。艰难的是,我使用 $("div").on("click", "button", function () { 让它工作,但我想看到它使用 .class 工
如何在我的代码中触发 Android onCreateOptionsMenu 函数,即无需用户单击手机上的选项菜单按钮? 最佳答案 Activity.openOptionsMenu(); 就可以了 关
我将表单包装在 中然后我设置 list android:windowSoftInputMode="adjustResize" (默认 react native )。现在,当我用手指触摸事件手动聚焦一
我有一个 Android 编程问题。使用下面的代码我想验证一个字符串匹配。它验证正常,但 LogCat 显示 TextWatcher 方法在每次击键时触发两次,我不明白为什么。我希望每次击键只触发一次
我是一名优秀的程序员,十分优秀!