当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript switchMap.switchMap函数代码示例

本文整理汇总了TypeScript中rxjs/operators/switchMap.switchMap函数的典型用法代码示例。如果您正苦于以下问题:TypeScript switchMap函数的具体用法?TypeScript switchMap怎么用?TypeScript switchMap使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了switchMap函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: withLatestFrom

 [Overlay.Info]: (xs: Observable<[LogNames, any]>, inputs: Inputs) => {
     return xs.pipe(
         withLatestFrom(
             inputs.option$,
             inputs.notifyElement$,
             inputs.document$
         ),
         /**
          * Reject all notifications if notify: false
          */
         filter(([, options]) => Boolean(options.notify)),
         /**
          * Set the HTML of the notify element
          */
         tap(([event, options, element, document]) => {
             element.innerHTML = event[0];
             element.style.display = "block";
             document.body.appendChild(element);
         }),
         /**
          * Now remove the element after the given timeout
          */
         switchMap(([event, options, element, document]) => {
             return timer(event[1] || 2000).pipe(
                 tap(() => {
                     element.style.display = "none";
                     if (element.parentNode) {
                         document.body.removeChild(element);
                     }
                 })
             );
         })
     );
 }
开发者ID:BrowserSync,项目名称:browser-sync,代码行数:34,代码来源:log.ts

示例2: getFormInputStream

export function getFormInputStream(
    document: Document,
    socket$: Inputs["socket$"],
    option$: Inputs["option$"]
): Observable<OutgoingSocketEvent> {
    const canSync$ = createTimedBooleanSwitch(
        socket$.pipe(filter(([name]) => name === IncomingSocketNames.Keyup))
    );
    return option$.pipe(
        skip(1), // initial option set before the connection event
        pluck("ghostMode", "forms", "inputs"),
        distinctUntilChanged(),
        switchMap(formInputs => {
            if (!formInputs) {
                return empty();
            }
            return fromEvent(document.body, "keyup", true).pipe(
                map((e: Event) => e.target || e.srcElement),
                filter(
                    (target: Element) =>
                        target.tagName === "INPUT" ||
                        target.tagName === "TEXTAREA"
                ),
                withLatestFrom(canSync$),
                filter(([, canSync]) => canSync),
                map(([eventTarget]) => {
                    const target = getElementData(eventTarget);
                    const value = eventTarget.value;

                    return KeyupEvent.outgoing(target, value);
                })
            );
        })
    );
}
开发者ID:BrowserSync,项目名称:browser-sync,代码行数:35,代码来源:form-inputs.listener.ts

示例3: createTimedBooleanSwitch

export function createTimedBooleanSwitch(source$, timeout = 1000) {
    return source$.pipe(
        switchMap(() => {
            return concat(of(false), timer(timeout).pipe(mapTo(true)));
        }),
        startWith(true)
    );
}
开发者ID:BrowserSync,项目名称:browser-sync,代码行数:8,代码来源:utils.ts

示例4: getScrollStream

export function getScrollStream(
    window: Window,
    document: Document,
    socket$: Inputs["socket$"],
    option$: Inputs["option$"]
): Observable<OutgoingSocketEvent> {
    const canSync$ = createTimedBooleanSwitch(
        socket$.pipe(filter(([name]) => name === IncomingSocketNames.Scroll))
    );

    /**
     * If the option 'scrollElementMapping' is provided
     * we cache thw
     * @type {Observable<(Element | null)[]>}
     */
    const elemMap$ = option$.pipe(
        pluck("scrollElementMapping"),
        map((selectors: string[]) =>
            selectors.map(selector => document.querySelector(selector))
        )
    );

    return option$.pipe(
        skip(1), // initial option set before the connection event
        pluck("ghostMode", "scroll"),
        distinctUntilChanged(),
        switchMap(scroll => {
            if (!scroll) return empty();
            return fromEvent(document, "scroll", true).pipe(
                map((e: Event) => e.target),
                withLatestFrom(canSync$, elemMap$),
                filter(([, canSync]) => Boolean(canSync)),
                map(([target, canSync, elemMap]: [any, boolean, any[]]) => {
                    if (target === document) {
                        return ScrollEvent.outgoing(
                            getScrollPosition(window, document),
                            "document",
                            0
                        );
                    }

                    const elems = document.getElementsByTagName(target.tagName);
                    const index = Array.prototype.indexOf.call(
                        elems || [],
                        target
                    );

                    return ScrollEvent.outgoing(
                        getScrollPositionForElement(target),
                        target.tagName,
                        index,
                        elemMap.indexOf(target)
                    );
                })
            );
        })
    );
}
开发者ID:BrowserSync,项目名称:browser-sync,代码行数:58,代码来源:scroll.listener.ts

示例5: handle

    handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
        return fromPromise(this.platform.ready()).pipe(
            switchMap(() => {
                /**
                 * Native HTTP Cordova plugin doesn't like local requests
                 */
                const isExternalRequest = checkExternalRequest(req);

                if (
                    isExternalRequest &&
                    checkAvailability('cordova.plugin.http') === true
                ) {
                    return this.cordovaHttpBackend.handle(req);
                } else {
                    return this.fallbackBackend.handle(req);
                }
            }),
        );
    }
开发者ID:achubutkin,项目名称:ionic-native-http-connection-backend,代码行数:19,代码来源:native-http-fallback.ts

示例6: retrieveStock

 @Effect()
 retrieveStock(): Observable<Action> {
   return this.actions$.ofType(StockMarketActionTypes.RETRIEVE).pipe(
     tap((action: ActionStockMarketRetrieve) =>
       this.localStorageService.setItem(STOCK_MARKET_KEY, {
         symbol: action.payload.symbol
       })
     ),
     distinctUntilChanged(),
     debounceTime(500),
     switchMap((action: ActionStockMarketRetrieve) =>
       this.service
         .retrieveStock(action.payload.symbol)
         .pipe(
           map(stock => new ActionStockMarketRetrieveSuccess({ stock })),
           catchError(error =>
             of(new ActionStockMarketRetrieveError({ error }))
           )
         )
     )
   );
 }
开发者ID:spairo,项目名称:angular-ngrx-material-starter,代码行数:22,代码来源:stock-market.effects.ts

示例7: retrieveStock

 @Effect()
 retrieveStock(): Observable<Action> {
   return this.actions$.ofType(STOCK_MARKET_RETRIEVE).pipe(
     tap(action =>
       this.localStorageService.setItem(STOCK_MARKET_KEY, {
         symbol: action.payload
       })
     ),
     distinctUntilChanged(),
     debounceTime(500),
     switchMap(action =>
       this.service.retrieveStock(action.payload).pipe(
         map(stock => ({
           type: STOCK_MARKET_RETRIEVE_SUCCESS,
           payload: stock
         })),
         catchError(err =>
           of({ type: STOCK_MARKET_RETRIEVE_ERROR, payload: err })
         )
       )
     )
   );
 }
开发者ID:a727891,项目名称:bb,代码行数:23,代码来源:stock-market.effects.ts

示例8: getClickStream

export function getClickStream(
    document: Document,
    socket$: Inputs["socket$"],
    option$: Inputs["option$"]
): Observable<OutgoingSocketEvent> {
    const canSync$ = createTimedBooleanSwitch(
        socket$.pipe(filter(([name]) => name === IncomingSocketNames.Click))
    );

    return option$.pipe(
        skip(1), // initial option set before the connection event
        pluck("ghostMode", "clicks"),
        distinctUntilChanged(),
        switchMap(canClick => {
            if (!canClick) {
                return empty();
            }
            return fromEvent(document, "click", true).pipe(
                map((e: Event) => e.target),
                filter((target: any) => {
                    if (target.tagName === "LABEL") {
                        const id = target.getAttribute("for");
                        if (id && document.getElementById(id)) {
                            return false;
                        }
                    }
                    return true;
                }),
                withLatestFrom(canSync$),
                filter(([, canSync]) => canSync),
                map(([target]): OutgoingSocketEvent => {
                    return ClickEvent.outgoing(getElementData(target));
                })
            );
        })
    );
}
开发者ID:BrowserSync,项目名称:browser-sync,代码行数:37,代码来源:clicks.listener.ts

示例9: getFormTogglesStream

export function getFormTogglesStream(
    document: Document,
    socket$: Inputs["socket$"],
    option$: Inputs["option$"]
): Observable<OutgoingSocketEvent> {
    const canSync$ = createTimedBooleanSwitch(
        socket$.pipe(
            filter(([name]) => name === IncomingSocketNames.InputToggle)
        )
    );

    return option$.pipe(
        skip(1),
        pluck("ghostMode", "forms", "toggles"),
        distinctUntilChanged(),
        switchMap(canToggle => {
            if (!canToggle) {
                return empty();
            }
            return fromEvent(document, "change", true).pipe(
                map((e: Event) => e.target || e.srcElement),
                filter((elem: HTMLInputElement) => elem.tagName === "SELECT"),
                withLatestFrom(canSync$),
                filter(([, canSync]) => canSync),
                map(([elem, canSync]: [HTMLInputElement, boolean]) => {
                    const data = getElementData(elem);

                    return FormToggleEvent.outgoing(data, {
                        type: elem.type,
                        checked: elem.checked,
                        value: elem.value
                    });
                })
            );
        })
    );
}
开发者ID:BrowserSync,项目名称:browser-sync,代码行数:37,代码来源:form-toggles.listener.ts


注:本文中的rxjs/operators/switchMap.switchMap函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。