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
rxjsdependency. - Open the index.ts file and import the
Observableclass from therxjsmodule. - Create a new
Observableinstance, providing thesubscribefunction. - 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);