Exercise - Unicast
Instructions
The goal of this exercise is to practice creating a unicast Observable.
- Go to codesandbox.io, sign in, and then create a new sandbox using the "Vanilla TypeScript" template.
- Add the
rxjs
dependency. - Open the index.ts file and import the
Observable
class from therxjs
module. - Create a new
Observable
instance, providing thesubscribe
function. - Use the
console.log()
function to log a message for each new subscription. - Optionally produce a value.
- Return a teardown function.
- Subscribe to the new Observable multiple times and note the number of subscriptions.
Hints
Here is the signature for the Observable
class's constructor()
function:
constructor(subscribe?: (this: Observable<T>, subscriber: Subscriber<T>) => TeardownLogic);