Skip to content

Commit 8f709be

Browse files
committed
feat: added Multicasting Operators
1 parent 1a6c2d3 commit 8f709be

File tree

1 file changed

+110
-13
lines changed

1 file changed

+110
-13
lines changed

README.md

Lines changed: 110 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,38 +3051,135 @@ bootstrapApplication(App);
30513051

30523052
**multicast** -
30533053

3054-
```typescript
3054+
[Link](https://rxjs.dev/api/operators/multicast)
30553055

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.
30573057

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';
30593064

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+
}
30613092

3093+
bootstrapApplication(App);
30623094
```
30633095

3064-
**publishBehavior** -
3096+
**publishBehavior** - Creates a ConnectableObservable that utilizes a BehaviorSubject.
30653097

3066-
```typescript
3098+
[Link](https://rxjs.dev/api/operators/publishBehavior)
30673099

3068-
```
3100+
**publishLast** - Returns a connectable observable sequence that shares a single subscription to the underlying sequence containing only the last notification.
30693101

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';
30713108

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 {
30733118

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);
30743144
```
30753145

30763146
**publishReplay** -
30773147

3078-
```typescript
3148+
[Link](https://rxjs.dev/api/operators/publishReplay)
30793149

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().
30813151

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';
30833158

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 {
30853168

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);
30863183
```
30873184

30883185
[Back to top⤴️](#contents)

0 commit comments

Comments
 (0)