Skip to content

Instantly share code, notes, and snippets.

View LayZeeDK's full-sized avatar
🇩🇰
Denmark

Lars Gyrup Brink Nielsen LayZeeDK

🇩🇰
Denmark
View GitHub Profile
const source = instrument(interval(100));
const counted = source.pipe(publish(), refCount());
const a = counted.subscribe(observer("a"));
setTimeout(() => a.unsubscribe(), 110);
setTimeout(() => counted.subscribe(observer("b")), 120);
const source = instrument(timer(100));
const counted = source.pipe(publish(), refCount());
const a = counted.subscribe(observer("a"));
setTimeout(() => a.unsubscribe(), 110);
setTimeout(() => counted.subscribe(observer("b")), 120);
const source = instrument(interval(100));
const counted = source.pipe(publish(), refCount());
const a = counted.pipe(take(1)).subscribe(observer("a"));
const b = counted.pipe(take(1)).subscribe(observer("b"));
function instrument<T>(source: Observable<T>) {
return new Observable<T>(observer => {
console.log("source: subscribing");
const subscription = source.pipe(
tap(value => console.log(`source: ${value}`)),
).subscribe(observer);
return () => {
subscription.unsubscribe();
console.log("source: unsubscribed");
};
const source = instrument(interval(100));
const published = source.pipe(publish());
const a = published.pipe(take(1)).subscribe(observer("a"));
const b = published.pipe(take(1)).subscribe(observer("b"));
const subscription = published.connect();
import { concat, defer, Observable, of } from "rxjs";
import { delay, publish } from "rxjs/operators";
function random() {
return Math.floor(Math.random() * 100);
}
const source = concat(
defer(() => of(random())),
defer(() => of(random())).pipe(delay(1))
import { defer, Observable, of, Subject } from "rxjs";
import { delay, multicast } from "rxjs/operators";
const source = defer(() => of(
Math.floor(Math.random() * 100)
)).pipe(
delay(0),
);
import { defer, Observable, of, Subject } from "rxjs";
const source = defer(() => of(
Math.floor(Math.random() * 100)
));
function observer(name: string) {
return {
next: (value: number) => console.log(`observer ${name}: ${value}`),
complete: () => console.log(`observer ${name}: complete`)
import { defer, Observable, of, Subject } from "rxjs";
import { multicast } from "rxjs/operators";
const source = defer(() => of(
Math.floor(Math.random() * 100)
));
function observer(name: string) {
return {
next: (value: number) => console.log(`observer ${name}: ${value}`),
import { Component, ContentChild } from '@angular/core';
import { map } from 'rxjs/operators';
import { ConnectionService } from './connection.service';
import { FastDirective} from './fast.directive';
import { SlowDirective} from './slow.directive';
@Component({
selector: 'connection',
template: `