Rxjs debounce 操作符在 SAP Spartacus 函数节流中的一个实际使用例子
SAP 函数 一个 例子 Spartacus 实际 操作符 Rxjs
2023-09-14 09:04:00 时间
在 window-ref.ts 的实现里,定义了一个每隔 300 毫秒,通过 fromEvent 发射一个 resize event 的Observable:
/**
* Returns an observable for the window resize event and emits an event
* every 300ms in case of resizing. An event is simulated initially.
*
* If there's no window object available (i.e. in SSR), a null value is emitted.
*/
get resize$(): Observable<any> {
if (!this.nativeWindow) {
return of(null);
} else {
return fromEvent(this.nativeWindow, 'resize').pipe(
debounceTime(300),
startWith({ target: this.nativeWindow }),
distinctUntilChanged()
);
}
}
加上 distingctUntilChanged 操作符后,能过滤掉完全一致的 resize event. 下面的例子,展示了如何使用 distingctUntilChanged,将数字序列里重复的数字过滤掉。distingctUntilChanged 默认会将当前元素和前一元素做比较。
import { of } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
of(1, 1, 2, 2, 2, 1, 1, 2, 3, 3, 4).pipe(
distinctUntilChanged(),
)
.subscribe(x => console.log(x)); // 1, 2, 1, 2, 3, 4
下列例子展示了如何将自定义的比较逻辑,通过箭头函数作为参数,传入 distinctUntilChanged 里。
import { of } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
interface Person {
age: number,
name: string
}
of<Person>(
{ age: 4, name: 'Foo'},
{ age: 7, name: 'Bar'},
{ age: 5, name: 'Foo'},
{ age: 6, name: 'Foo'},
).pipe(
distinctUntilChanged((p: Person, q: Person) => p.name === q.name),
)
.subscribe(x => console.log(x));
// displays:
// { age: 4, name: 'Foo' }
// { age: 7, name: 'Bar' }
// { age: 5, name: 'Foo' }
相关文章
- SAP Commerce Cloud 里 OAuth2 Client 的两种配置方法
- 浅谈 SAP ABAP 系统里的 ALV 输出方式实现
- SAP UI5 应用中的 sap.ui.require.toUrl 使用场景
- SAP UI5 sap.ui.base.ManagedObject 的构造函数参数讲解
- SAP BAPI是个啥
- 创建 SAP Fiori Catalog 时遇到的 duplicate 记录的问题分析
- SAP UI5 sap.ui.Device.media.RANGESETS.SAP_STANDARD_EXTENDED 介绍
- SAP UI5 sap.ui.Device.media 的使用介绍
- SAP UI5 sap.ui.Device.media 公有方法介绍
- SAP ABAP CDS view 里 INNER JOIN 和 Association 的区别
- SAP UI5 里 FlexBox 的使用方法
- SAP QM 不常用功能之Source Inspection
- SAP UI5 响应式表格 sap.m.Table 根据不同宽度的屏幕动态决定显示或隐藏 Column 的实现源代码讲解试读版
- SAP中日期时间函数总结详解编程语言
- SAP RFC通信模式详解编程语言
- SAP 常用系统函数详解编程语言
- SAP寄售业务流程详解编程语言
- SAP系统权限管理及参数设置详解编程语言
- SAP批量锁定用户详解编程语言
- SAP MM物料管理系统功能详解编程语言
- SAP MIR7输入数量自动金额的设置详解编程语言
- SAP 甘特图详解编程语言
- 系统SAP在Linux系统上的开启之旅(sap启动linux)
- 对比:Oracle 与 SAP的优势(oracle 与 sap)
- Oracle从SAP抽取数据的简单方法(oracle从sap抽数)
- 数据库解决方案如何使用SAP创建Redis数据库(sap怎么创建redis)
- 对比Oracle和SAP价格比较哪个是更值得投入的选择(oracle与sap价格)
- Oracle与SAP金蝶谁更具竞争力(oracle sap金蝶)
- 探索 Oracle 与 SAP 技术的开发可能性(oracle sap开发)