首页 > 其他分享 >[RxJS] Use filter and partition for conditional logic

[RxJS] Use filter and partition for conditional logic

时间:2022-10-22 16:46:45浏览次数:73  
标签:box Use conditional const MOVE leftPosition partition RxJS click

// begin lesson code
import { fromEvent, partition } from 'rxjs';
import { filter, pluck } from 'rxjs/operators';

const MOVE_SPEED = 20;
let leftPosition = 0;

// elems
const box = document.getElementById('box');

// streams
const click$ = fromEvent(document, 'click');
const xPositionClick$ = click$.pipe(pluck('clientX'));

xPositionClick$.subscribe(xPos => {
  /*
   * Generally if you have a single if statement in
   * you subscribe block, prefer filter instead.
   */
  if(xPos < window.innerWidth / 2) {
    box.style.left = `${leftPosition -= MOVE_SPEED}px`;
  } else {
    box.style.left = `${leftPosition += MOVE_SPEED}px`;
  }
});

If you found youself doing if/elseit is possible to use partition operator.

 

// begin lesson code
import { fromEvent, partition, merge } from 'rxjs';
import { filter, pluck, tap } from 'rxjs/operators';

const MOVE_SPEED = 20;
let leftPosition = 0;

// elems
const box = document.getElementById('box');

// streams
const click$ = fromEvent(document, 'click');
const xPositionClick$ = click$.pipe(pluck('clientX'));

const [ clickLeft$, clickRight$ ] = partition(
  xPositionClick$,
  xPos => xPos < window.innerWidth / 2
);

const moveLeft = () => {
   box.style.left = `${leftPosition -= MOVE_SPEED}px`;
}

const moveRight = () => {
   box.style.left = `${leftPosition += MOVE_SPEED}px`;
}

const moveLeft$ = clickLeft$.pipe(tap(moveLeft));
const moveRight$ = clickRight$.pipe(tap(moveRight));

merge(moveLeft$, moveRight$).subscribe()

 

<iframe src="https://stackblitz.com/edit/rxjs-txzn2d?embed=1&file=index.ts" style="width: 100%; height: 500px"></iframe>

 

标签:box,Use,conditional,const,MOVE,leftPosition,partition,RxJS,click
From: https://www.cnblogs.com/Answer1215/p/16816379.html

相关文章

  • [RxJS] Extract common operator logic into standalone function
    /**From*/click$.pipe(mergeMapTo(throwError({status:400,message:'Servererror'}).pipe(retryWhen(attempts=>{returnatt......
  • cat userlist的理解
    学习要求Linux文件系统的三层抽象是什么?写出Catuserlist的过程,要详述目录文件,i-node.数据块,要画图示意假设块大小为4k,userlist的大小不小于10k,自己假设大小Linux......
  • cat userlist
    catuserlistLinux文件系统的三层抽象是什么?linux的三层抽象分别是:磁盘——>分区分区——>块数组块数组——>(超级块,inode,数据块)写出Catuserlist的过程,要详述目录......
  • [Typescript] Tips: Use assertion functions inside classes
    Youcandosomereally,reallyneatstuffwithassertionfunctionsinsideclasses.Here,weassertthattheuserisloggedinandgetproperinferenceontheu......
  • 爬虫基础知识(web前端,请求模块urllib,重构user_agent)
    ✅个人主页:​​hacker_demo的51CTO博客......
  • 6.ClickHouse系列之配置分片集群
    副本集对数据进行完整备份,数据高可用,对于分片集群来说,不管是ES还是ClickHouse是为了解决数据横向扩展的问题,ClickHouse在实际应用中一般配置副本集就好了1.编写clickhous......
  • 7.ClickHouse系列之查询优化(一)
    1.Explain查询计划查看//查看执行计划,默认值EXPLAINPLANSELECTarrayJoin([6,6,7])//AST语法树查看EXPLAINASTSELECTnumbersFROMsystem.numbersLIMIT10;/......
  • 8.ClickHouse系列之查询优化(二)
    本文介绍多表关联查询优化方式1.用IN代替JOIN当多表查询时,查询的数据仅从一张表出时,可考虑用IN操作而不是JOINSELECTa.*FROMhits_v1aWHEREa.CounterIDin(SELEC......
  • 1.ClickHouse系列之Docker本地部署
    本文介绍docker-compose方式部署clickhouse数据库编写docker-compose.yml文件:version:'3'services:elasticsearch:image:clickhouse/clickhouse-server......
  • 2.ClickHouse系列之特点介绍
    1.列式存储采用列式存储时,数据在磁盘上的组织结构为:123张三李四王五182025好处:对于列的聚合、计数、求和等统计操作由于列式存储由于列数据类型相同,更容易......