Check the playground.

import {Counter, CountDownState, ConterStateKeys, PartialCountDownState} from './counter'
import { Subject, Observable, merge, timer, NEVER, combineLatest} from 'rxjs';
import { map, mapTo, switchMap, pluck, scan, startWith,shareReplay, distinctUntilChanged, withLatestFrom, tap} from 'rxjs/operators'; // EXERCISE DESCRIPTION ============================== /**
* Use `ConterStateKeys` for property names.
* Explort the counterUI API by typing `counterUI.` somewhere. ;)
*
* Implement all features of the counter:
* 1. Start, pause the counter. Then restart the counter with 0 (+)
* 2. Start it again from paused number (++)
* 3. If Set to button is clicked set counter value to input value while counting (+++)
* 4. Reset to initial state if reset button is clicked (+)
* 5. If count up button is clicked count up, if count down button is clicked count down (+)
* 6. Change interval if input tickSpeed input changes (++)
* 7. Change count up if input countDiff changes (++)
* 8. Take care of rendering execution and other performance optimisations as well as refactoring (+)
*/ // ================================================================== // = BASE OBSERVABLES ====================================================
// == SOURCE OBSERVABLES ==================================================
const initialConterState: CountDownState = {
isTicking: false,
count: 0,
countUp: true,
tickSpeed: 200,
countDiff:1
}; const counterUI = new Counter(
document.body,
{
initialSetTo: initialConterState.count + 10,
initialTickSpeed: initialConterState.tickSpeed,
initialCountDiff: initialConterState.countDiff,
}
); // === STATE OBSERVABLES ==================================================
const programmaticCommandSubject = new Subject<PartialCountDownState>();
const counterCommands$ = merge(
counterUI.btnStart$.pipe(mapTo({isTicking: true})),
counterUI.btnPause$.pipe(mapTo({isTicking: false})),
counterUI.btnSetTo$.pipe(map(n => ({count: n}))),
counterUI.btnUp$.pipe(mapTo({countUp: true})),
counterUI.btnDown$.pipe(mapTo({countUp: false})),
counterUI.btnReset$.pipe(mapTo({...initialConterState})),
counterUI.inputTickSpeed$.pipe(map ( n => ({tickSpeed: n}))),
counterUI.inputCountDiff$.pipe(map ( n => ({countDiff: n}))),
programmaticCommandSubject.asObservable()
); const counterState$ = counterCommands$
.pipe(
startWith(initialConterState),
scan((state, command) => ({
...state,
...command
})),
shareReplay(1)
); // === INTERACTION OBSERVABLES ============================================
const count$ = counterState$.pipe(
queryState('count')
); const isTicking$ = counterState$.pipe(
queryState('isTicking')
);
const intervalTick$ = isTicking$.pipe(
switchMap(isTicking => isTicking ? timer(0, initialConterState.tickSpeed): NEVER)
);
// = SIDE EFFECTS =========================================================
// == UI INPUTS ===========================================================
const renderCountChange$ = count$
.pipe(
tap((n: number) => counterUI.renderCounterValue(n))
); // == UI OUTPUTS ==========================================================
const commandFromTick$ = intervalTick$
.pipe(
withLatestFrom(count$, (_, count) => count),
tap((count: number) => programmaticCommandSubject.next({count: ++count}))
); // == SUBSCRIPTION ======================================================== merge(
// Input side effect
renderCountChange$,
// Outputs side effect
commandFromTick$
)
.subscribe(); // == CREATION METHODS ====================================================
function queryState (name) {
return function (o$) {
return o$.pipe(
pluck(name),
distinctUntilChanged()
)
}
}

[RxJS] RxJS Advanced Patterns Operate Heavily Dynamic UIs的更多相关文章

  1. 使用angular的HttpClient搭配rxjs

    一.原Http使用总结 使用方法 在根模块或核心模块引入HttpModule 即在AppModule或CoreModule中引入HttpModule: import { HttpModule } fr ...

  2. RxJS速成 (上)

    What is RxJS? RxJS是ReactiveX编程理念的JavaScript版本.ReactiveX是一种针对异步数据流的编程.简单来说,它将一切数据,包括HTTP请求,DOM事件或者普通数 ...

  3. RxJS v6 学习指南

    为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...

  4. 构建流式应用—RxJS详解[转]

    目录 常规方式实现搜索功能 RxJS · 流 Stream RxJS 实现原理简析 观察者模式 迭代器模式 RxJS 的观察者 + 迭代器模式 RxJS 基础实现 Observable Observe ...

  5. 【CuteJavaScript】Angular6入门项目(3.编写服务和引入RxJS)

    本文目录 一.项目起步 二.编写路由组件 三.编写页面组件 1.编写单一组件 2.模拟数据 3.编写主从组件 四.编写服务 1.为什么需要服务 2.编写服务 五.引入RxJS 1.关于RxJS 2.引 ...

  6. rxjs 入门--环境配置

    原文: https://codingthesmartway.com/getting-started-with-rxjs-part-1-setting-up-the-development-enviro ...

  7. RxJS 6有哪些新变化?

    我们的前端工程由Angular4升级到Angular6,rxjs也要升级到rxjs6.  rxjs6的语法做了很大的改动,幸亏引入了rxjs-compact包,否则升级工作会无法按时完成. 按照官方的 ...

  8. [译]Rxjs&Angular-退订可观察对象的n中方式

    原文/出处: RxJS & Angular - Unsubscribe Like a Pro 在angular项目中我们不可避免的要使用RxJS可观察对象(Observables)来进行订阅( ...

  9. Dynamic Signals and Slots

    Ref https://doc.qt.io/archives/qq/qq16-dynamicqobject.html Trolltech | Documentation | Qt Quarterly ...

随机推荐

  1. Java集合框架——List接口

    第三阶段 JAVA常见对象的学习 集合框架--List接口 按照集合框架的继承体系,我们先从Collection中的List接口开始学习 (一) 概述及功能(ArrayList演示) (1) 概述 L ...

  2. Nginx03---重装

    1.先执行一下命令 1.1 删除nginx,–purge包括配置文件 sudo apt-get --purge remove nginx 1.2 自动移除全部不使用的软件包 sudo apt-get ...

  3. 二、python数据类型、字符编码、文件处理

    一. 前言 1. 什么是数据: x = 10,10就是我们要存储的数据 2. 为何数据要分不同的类型 数据是用来表示状态的, 不同的状态就应该用不同类型的数据去表示 3. 数据类型 数字(整型.长整型 ...

  4. 10-MySQlL DBA笔记-基础知识

    第四部分 运维篇 首先来了解一下数据库的定义,数据库是高效的.可靠的.易用的.安全的多用户存储引擎,我们可以通过它访问大量的持久化数据.我们管理和维护数据库,本质上也是要确保如上的特性,尽可能地保证数 ...

  5. Java装饰者模式(思维导图)

    图1 装饰者模式[点击查看图片] 1,一个简单的以人为主体的装饰者模式 被装饰者 public interface Human {//被装饰者 public void wearClothes(); p ...

  6. 如何演讲-摘录自TED

    一.首先,只传递一个主要思想  想法是个很复杂的东西,你要对你的内容做减法,突出重点,只关注一个主要思想,也就是你最富有热情的观点,利用机会好好的阐述它.你要给出案例,分享案例,生动阐述所以,找到一个 ...

  7. javaIO——BufferedWriter

    [环境] jdk1.8 前面学习过 BufferedReader,是缓冲字符输入流.那么今天来学习对应的缓冲字符输出流类:BufferedWriter.跟 BufferedReader 同理,它也是一 ...

  8. ADO与达梦7产生的一个未知问题

    采用OLEDB与达梦7建立数据库连接 连接成功 查询表成功 打开表成功 当进行到addnew 操作时  报异常,未知错误 而且是仅针对这张表 ,其他表都没有问题 当清空数据后可以再插入一次数据,之后就 ...

  9. TypeScript入门五:TypeScript的接口

    TypeScript接口的基本使用 TypeScript函数类型接口 TypeScript可索引类型接口 TypeScript类类型接口 TypeScript接口与继承 一.TypeScript接口的 ...

  10. JS实现旋转的魔方

    js <script> window.onload = function () { let cube = document.querySelector('.cube') let timer ...