Created
November 26, 2019 14:54
-
-
Save oshosanya/d507bccd2c47408a01ae635a87d7fd7a to your computer and use it in GitHub Desktop.
Example of using rxjs with typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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