-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreation.js
More file actions
139 lines (113 loc) · 2.64 KB
/
creation.js
File metadata and controls
139 lines (113 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import * as Rx from 'rxjs';
import {
repeat
} from 'rxjs/operators';
/*
* of
*/
Rx.of(1, 2, 3)
.subscribe(
console.log,
() => console.log('error'),
() => console.log('of complete')
)
// source : |--- (1 , 2 , 3) ---|--->
// 無時間間隔一次把1,2,3給Observer
// demo: https://rxviz.com/v/38lkjrKJ
/*
* range
*/
// Rx.range(1.5, 10)
Rx.range(1, 10)
// .map( x => x * 2 )
.subscribe(
console.log,
() => console.log('error'),
() => console.log('range complete')
)
// source : |--- (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10) ---|--->
// 第一個參數代表從1開始,第二個參數為遞增100次,每次遞增1
// demo: https://rxviz.com/v/9J9X0mdO
/*
* generate
*/
Rx.generate(
1, //初始值
value => value < 10, //繼續的條件
value => value + 2, //每次遞增的值
value => value * value // 產生結果
).subscribe(
console.log,
() => console.log('error'),
() => console.log('generate complete')
)
// source : |--- (1 , 9 , 25 , 49 , 81) ---|--->
// demo: https://rxviz.com/v/A8Dl9gK8
/*
* repeat
*/
Rx.of(1, 2, 3)
.pipe(
repeat(2)
)
.subscribe(
console.log,
() => console.log('error'),
() => console.log('repeat complete')
)
// source : |--- 1 --- 2 --- 3 ------ 1 --- 2 --- 3 ----->
// demo: https://rxviz.com/v/38lkjDKJ
/*
* empty
*/
Rx.empty()
// source : |-------------------------->
// 沒有任何(不會產生)數據,直接結束訂閱
// demo: https://rxviz.com/v/XJzlRGgo
/*
* throw
*/
Rx.throwError(new Error('oops!'))
// 5版 throw
// source : X-------------------------->
// 直接拋錯
// demo: https://rxviz.com/v/1o7zpvV8
/*
* never
*/
Rx.never()
// source : -------------------------->
// 什麼都不做,不會產生數據,不會拋出錯誤,也不會結束,一直呆著到永遠
// demo: https://rxviz.com/v/RObNvGdO
/*
* interval
*/
Rx.interval(1000)
// source : ---0---1---2---3---4---5---6---7--->
// 參數代表產生數據的間隔時間,從0開始,也可以搭配map改變初始值
// demo: https://rxviz.com/v/WJx21z1o
/*
* timer
*/
Rx.timer(1000, 1000)
// 第一個參數代表 N 秒後開始執行
// 第二個參數代表每 N 秒執行一次
// source : ---0---1---2---3---4---5---6---7--->
// demo: https://rxviz.com/v/58Gn2LpJ
/*
* from
*/
Rx.from([1, 2, 3])
// .toArray()
.subscribe(
console.log,
() => console.log('error'),
() => console.log('from complete')
)
// 把一切轉換成 Observable
/*
* fromEvent
*/
// Rx.fromEvent(document.body, 'body')
// 把DOM事件轉換成 Observable
// https://stackblitz.com/edit/typescript-mfyefr?file=index.ts&devtoolsheight=50