redux observable map未被调用

时间:2018-04-15 14:04:15

标签: rxjs rxjs5 redux-observable

我有这个代码,并且无法理解为什么我没有进入地图功能(我在那里发表评论“我从来没有接触到这部分代码”):

export const fiveCPMonitoringLoadEpic = (action$, store) =>
action$
    .ofType(
        FIVE_CP_MONITORING_ACTION_TYPES.LOAD_FIVE_CP_MONITORING_DATA_STARTED
    )
    .debounceTime(250)
    .switchMap(action => {
        const params = action.params;
        const siteId = { params };

        // getting site's EDC accounts (observable):
        const siteEdcAccount$ = getSiteEDCAccountsObservable(params);
        const result$ = siteEdcAccount$.map(edcResponse => {
            // getting here - all good so far.
            const edcAccount = edcResponse[0];
            // creating another observable (from promise - nothing special)
            const fiveCPMonitoringEvent$ = getFiveCPAndTransmissionEventsObservable(
                {
                    ...params,
                    edcAccountId: edcAccount.utilityAccountNumber
                }
            );

            fiveCPMonitoringEvent$.subscribe(x => {
                // this is working... I am getting to this part of the code
                // --------------------------------------------------------
                console.log(x);
                console.log('I am getting this printed out as expected');
            });

            return fiveCPMonitoringEvent$.map(events => {
                // I NEVER GET TO THIS PART!!!!!
                // -----------------------------
                console.log('----- forecast-----');
                // according to response - request the prediction (from the event start time if ACTIVE event exists, or from current time if no active event)
                const activeEvent = DrEventUtils.getActiveEvent(events);
                if (activeEvent) {
                    // get event start time
                    const startTime = activeEvent.startTime;
                    // return getPredictionMeasurementsObservable({...params, startTime}

                    const predictions = getPredictionMock(startTime - 300);
                    return Observable.of(predictions).delay(Math.random() * 2000);
                } else {
                    // return getPredictionMeasurementsObservable({...params}

                    const predictions = getPredictionMock(
                        DateUtils.getLocalDateInUtcSeconds(new Date().getTime())
                    );
                    return Observable.of(predictions).delay(Math.random() * 2000);
                }
            });
有人可以在这里说清楚吗? 为什么在使用subscribe时它正在工作,但是当在observable上使用map时它不是? 是不是每次observable触发时都会调用map?

谢谢, 吉姆。

1 个答案:

答案 0 :(得分:0)

在您订阅您的o​​bservable之前,它很冷并且不会发出值。订阅后,将调用地图。这是rxjs的一个特性,旨在避免不做任何更改的操作(=没有cunsumer使用这些值)。有很多关于这个主题的博客文章,在Google上搜索“冷酷热门观众”