@@ -3051,38 +3051,135 @@ bootstrapApplication(App);
3051
3051
3052
3052
** multicast** -
3053
3053
3054
- ``` typescript
3054
+ [ Link ] ( https://rxjs.dev/api/operators/multicast )
3055
3055
3056
- ```
3056
+ ** publish ** - Returns a ConnectableObservable, which is a variety of Observable that waits until its connect method is called before it begins emitting items to those Observers that have subscribed to it.
3057
3057
3058
- ** publish** -
3058
+ ``` jsx
3059
+ import ' zone.js/dist/zone' ;
3060
+ import { Component , OnInit } from ' @angular/core' ;
3061
+ import { CommonModule } from ' @angular/common' ;
3062
+ import { bootstrapApplication } from ' @angular/platform-browser' ;
3063
+ import { zip , interval , of , map , publish , merge , tap } from ' rxjs' ;
3059
3064
3060
- ``` typescript
3065
+ @Component ({
3066
+ selector: ' my-app' ,
3067
+ standalone: true ,
3068
+ imports: [CommonModule],
3069
+ template: `
3070
+ <h1>publish Example</h1>
3071
+ ` ,
3072
+ })
3073
+ export class App implements OnInit {
3074
+
3075
+ ngOnInit () {
3076
+ const source$ = zip (interval (2000 ), of (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ))
3077
+ .pipe (map (([, number ]) => number));
3078
+
3079
+ source$
3080
+ .pipe (
3081
+ publish (multicasted$ =>
3082
+ merge (
3083
+ multicasted$ .pipe (tap (x => console .log (' Stream 1:' , x))),
3084
+ multicasted$ .pipe (tap (x => console .log (' Stream 2:' , x))),
3085
+ multicasted$ .pipe (tap (x => console .log (' Stream 3:' , x)))
3086
+ )
3087
+ )
3088
+ )
3089
+ .subscribe ();
3090
+ }
3091
+ }
3061
3092
3093
+ bootstrapApplication (App);
3062
3094
```
3063
3095
3064
- ** publishBehavior** -
3096
+ ** publishBehavior** - Creates a ConnectableObservable that utilizes a BehaviorSubject.
3065
3097
3066
- ``` typescript
3098
+ [ Link ] ( https://rxjs.dev/api/operators/publishBehavior )
3067
3099
3068
- ```
3100
+ ** publishLast ** - Returns a connectable observable sequence that shares a single subscription to the underlying sequence containing only the last notification.
3069
3101
3070
- ** publishLast** -
3102
+ ``` jsx
3103
+ import ' zone.js/dist/zone' ;
3104
+ import { Component , OnInit } from ' @angular/core' ;
3105
+ import { CommonModule } from ' @angular/common' ;
3106
+ import { bootstrapApplication } from ' @angular/platform-browser' ;
3107
+ import { ConnectableObservable , interval , publishLast , tap , take } from ' rxjs' ;
3071
3108
3072
- ``` typescript
3109
+ @Component ({
3110
+ selector: ' my-app' ,
3111
+ standalone: true ,
3112
+ imports: [CommonModule],
3113
+ template: `
3114
+ <h1>publishLast Example</h1>
3115
+ ` ,
3116
+ })
3117
+ export class App implements OnInit {
3073
3118
3119
+ ngOnInit () {
3120
+ const connectable = < ConnectableObservable< number>> interval (1000 )
3121
+ .pipe (
3122
+ tap (x => console .log (' side effect' , x)),
3123
+ take (3 ),
3124
+ publishLast ()
3125
+ );
3126
+
3127
+ connectable .subscribe ({
3128
+ next : x => console .log (' Sub. A' , x),
3129
+ error : err => console .log (' Sub. A Error' , err),
3130
+ complete : () => console .log (' Sub. A Complete' )
3131
+ });
3132
+
3133
+ connectable .subscribe ({
3134
+ next : x => console .log (' Sub. B' , x),
3135
+ error : err => console .log (' Sub. B Error' , err),
3136
+ complete : () => console .log (' Sub. B Complete' )
3137
+ });
3138
+
3139
+ connectable .connect ();
3140
+ }
3141
+ }
3142
+
3143
+ bootstrapApplication (App);
3074
3144
```
3075
3145
3076
3146
** publishReplay** -
3077
3147
3078
- ``` typescript
3148
+ [ Link ] ( https://rxjs.dev/api/operators/publishReplay )
3079
3149
3080
- ```
3150
+ ** share ** - Returns a new Observable that multicasts (shares) the original Observable. As long as there is at least one Subscriber this Observable will be subscribed and emitting data. When all subscribers have unsubscribed it will unsubscribe from the source Observable. Because the Observable is multicasting it makes the stream hot. This is an alias for multicast(() => new Subject()), refCount().
3081
3151
3082
- ** share** -
3152
+ ``` jsx
3153
+ import ' zone.js/dist/zone' ;
3154
+ import { Component , OnInit } from ' @angular/core' ;
3155
+ import { CommonModule } from ' @angular/common' ;
3156
+ import { bootstrapApplication } from ' @angular/platform-browser' ;
3157
+ import { interval , tap , map , take , share } from ' rxjs' ;
3083
3158
3084
- ``` typescript
3159
+ @Component ({
3160
+ selector: ' my-app' ,
3161
+ standalone: true ,
3162
+ imports: [CommonModule],
3163
+ template: `
3164
+ <h1>share Example</h1>
3165
+ ` ,
3166
+ })
3167
+ export class App implements OnInit {
3085
3168
3169
+ ngOnInit () {
3170
+ const source = interval (1000 ).pipe (
3171
+ tap (x => console .log (' Processing: ' , x)),
3172
+ map (x => x * x),
3173
+ take (6 ),
3174
+ share ()
3175
+ );
3176
+
3177
+ source .subscribe (x => console .log (' subscription 1: ' , x));
3178
+ source .subscribe (x => console .log (' subscription 2: ' , x));
3179
+ }
3180
+ }
3181
+
3182
+ bootstrapApplication (App);
3086
3183
```
3087
3184
3088
3185
[ Back to top⤴️] ( #contents )
0 commit comments