Skip to content

Instantly share code, notes, and snippets.

@oshosanya
Created November 26, 2019 14:54
Show Gist options
  • Save oshosanya/d507bccd2c47408a01ae635a87d7fd7a to your computer and use it in GitHub Desktop.
Save oshosanya/d507bccd2c47408a01ae635a87d7fd7a to your computer and use it in GitHub Desktop.
Example of using rxjs with typescript
interface IObserver {
next(any);
complete();
}
interface IObservable {
subscribe(IObserver);
}
class WeatherStation implements IObservable {
observed = new Observable(subscriber => {
subscriber.next(1);
subscriber.next(2)
subscriber.next(3);
subscriber.complete()
})
public subscribe(x: IObserver) {
this.observed.subscribe(x)
}
}
class Receiver implements IObserver {
name: string = '';
public constructor(name: string) {
this.name = name;
}
public next(x: any) {
console.log(this.name +': Gotten value ' + x);
}
public complete() {
console.log('Done');
}
}
let weather = new WeatherStation();
let receiver = new Receiver('one');
let second_receiver = new Receiver('two');
weather.subscribe(receiver)
weather.subscribe(second_receiver);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment