File tree Expand file tree Collapse file tree 2 files changed +13
-12
lines changed
Filter options
Expand file tree Collapse file tree 2 files changed +13
-12
lines changed
Original file line number Diff line number Diff line change @@ -5,11 +5,10 @@ const randomChar = () => String
5
5
6
6
const subscribe = observer => {
7
7
const observable = { observer } ;
8
- const timer = setInterval ( ( ) => {
8
+ setInterval ( ( ) => {
9
9
const char = randomChar ( ) ;
10
10
observer ( char ) ;
11
11
} , 200 ) ;
12
- observable . complete = ( ) => clearInterval ( timer ) ;
13
12
return observable ;
14
13
} ;
15
14
@@ -22,7 +21,9 @@ function observer(char) {
22
21
process . stdout . write ( char ) ;
23
22
count ++ ;
24
23
if ( count > 50 ) {
25
- observable . complete ( ) ;
26
24
process . stdout . write ( '\n' ) ;
25
+ process . exit ( 0 ) ;
27
26
}
28
27
}
28
+
29
+ console . dir ( { observer, observable } ) ;
Original file line number Diff line number Diff line change @@ -5,19 +5,17 @@ const randomChar = () => String
5
5
6
6
class Observable {
7
7
constructor ( ) {
8
- this . timer = setInterval ( ( ) => {
9
- if ( ! this . onData ) return ;
8
+ this . observer = null ;
9
+ setInterval ( ( ) => {
10
+ if ( ! this . observer ) return ;
10
11
const char = randomChar ( ) ;
11
- this . onData ( char ) ;
12
+ this . observer ( char ) ;
12
13
} , 200 ) ;
13
14
}
14
- subscribe ( onData ) {
15
- this . onData = onData ;
15
+ subscribe ( observer ) {
16
+ this . observer = observer ;
16
17
return this ;
17
18
}
18
- complete ( ) {
19
- clearInterval ( this . timer ) ;
20
- }
21
19
}
22
20
23
21
// Usage
@@ -30,7 +28,9 @@ function observer(char) {
30
28
process . stdout . write ( char ) ;
31
29
count ++ ;
32
30
if ( count > 50 ) {
33
- observable . complete ( ) ;
34
31
process . stdout . write ( '\n' ) ;
32
+ process . exit ( 0 ) ;
35
33
}
36
34
}
35
+
36
+ console . dir ( { observer, observable } ) ;
You can’t perform that action at this time.
0 commit comments