当前位置:网站首页>Introduction to the implementation principle of rxjs observable filter operator
Introduction to the implementation principle of rxjs observable filter operator
2022-07-03 13:16:00 【Wang Zixi】
Look at the following paragraph filter Operator Code for :
import {
fromEvent, interval, MonoTypeOperatorFunction } from 'rxjs';
import {
filter } from 'rxjs/operators';
function filterKey(key) {
console.log('input: ', key);
const result: MonoTypeOperatorFunction<KeyboardEvent> = filter(
(event: KeyboardEvent) => {
console.log('input event: ', event);
return event.key === key;
}
);
console.log('result: ', result);
return result;
}
fromEvent(document, 'keyup')
.pipe(filterKey('Enter'))
.subscribe(
(data) => console.log(data) // KeyboardEvent
);
original Observable call pipe, Perform customization Operator The logic of , Generate a in logic filter Operator example . Single step debugging is as follows :
Return to one filterOperatorFunction:
filter The call returns a filterOperatorFunction:
So far, we're done pipe The first paragraph of the two-stage call : obtain filter Back to filterOperatorFunction.
And then execute pipe The second paragraph of the operation , As shown in the figure above , The original Observable Pass in filterOperatorFunction:
This second operation , Will create new FilterOperator example , stay lift In operation , Create a new one Observable, Then put the original Observable Set to this new Observable Example of source Field , take FilterOperator The instance is assigned to the original Observable Of Operator Field .
pipe The result of call execution , Back to a new Observable to pipe The caller . Finally we subscribe Of , This is this. pipe Back to Observable.
subscribe Inside ,this Namely pipe Back to the new Observable,operator Point to filterOperator,source Point to fromEvent Returned original Observable:
The first use of TypeScript Object structure syntax ,object destructing, take this.operator Fields are assigned to variables Operator, Then use the original Observable As context , call filter Operator.
such ,pipe Back to Observable Subscribe to , Will be passed to the original Observable Of subscribe operation :
When we hit a random character on the keyboard , Trigger MouseEvent Corresponding handler, stay handler Call inside Observer Of next operation .
next The operation will eventually trigger filtersubscriber predicate The attribute is passed in by the application developer filter Operator Is an anonymous function of :
filter The input parameter is also a function , The input to this function is MouseEvent:
fromEvent New Observable, In fact, the implementation logic is internal , Also called subscriber Of next Method to emit values . meanwhile fromEvent Back to Observable Same as other creation operators, such as of The characteristics of , Is that it can respond to all kinds of events , such as document Of keyup event .
This event registration mechanism , It's in the picture above setupSubscription
Function .
The specific implementation of registration , Adopted the native browser provided addEventListener, Callers are document Global object , Incoming eventName by keyup,handler namely fromEvent Internal implementation , That is, the code in the green highlighted area in the figure above .
export declare function filter<T>(predicate: (value: T, index: number) => boolean, thisArg?: any): MonoTypeOperatorFunction<T>;
filter Operator Accept one predicate As input parameter , Returns a type of MonoTypeOperatorFunction Function of .
there T Is a type parameter .MonoType Corresponding T, It means single parameter .
MonoTypeOperatorFunction It's a special kind OperatorFunction, When the input and return parameters of the latter are the same , That is, specialization into a single type OperatorFunction:
OperatorFunction It's another kind of special UnaryFunction( One variable function ), When the input and output types of a unary function are the same Observable when , That is, specialization becomes OperatorFunction.
边栏推荐
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【SQLServer2012综合练习】
- 35道MySQL面试必问题图解,这样也太好理解了吧
- [combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】
- February 14, 2022, incluxdb survey - mind map
- [Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]
- 用户和组命令练习
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第七章习题】
- 剑指 Offer 11. 旋转数组的最小数字
- Sword finger offer 14- I. cut rope
猜你喜欢
Harmonic current detection based on synchronous coordinate transformation
Flink SQL knows why (12): is it difficult to join streams? (top)
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]
Flink SQL knows why (XIV): the way to optimize the performance of dimension table join (Part 1) with source code
解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.
Solve system has not been booted with SYSTEMd as init system (PID 1) Can‘t operate.
OpenHarmony应用开发之ETS开发方式中的Image组件
Flink SQL knows why (19): the transformation between table and datastream (with source code)
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
Flink SQL knows why (16): dlink, a powerful tool for developing enterprises with Flink SQL
随机推荐
Sword finger offer 11 Rotate the minimum number of the array
Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
Will Huawei be the next one to fall
A large select drop-down box, village in Chaoyang District
C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - cases without asynchronous
Sword finger offer 17 Print from 1 to the maximum n digits
C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous
道路建设问题
2022-02-13 plan for next week
stm32和电机开发(从mcu到架构设计)
SLF4J 日志门面
Logback log framework
[colab] [7 methods of using external data]
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: delegatesamplep245
Brief introduction to mvcc
An example of newtonjason
JSP and filter
My creation anniversary: the fifth anniversary
2022-01-27 redis cluster brain crack problem analysis
Smbms project