Hone logo
Hone
Problems

Implementing combineLatest in Angular

combineLatest is a powerful RxJS operator that combines the latest values from multiple Observables into a single Observable. This is incredibly useful in Angular applications when you need to react to changes in multiple input fields or data sources simultaneously, triggering updates or calculations based on the combined state. This challenge asks you to implement a simplified version of combineLatest to solidify your understanding of RxJS and Observables.

Problem Description

You are tasked with creating a function combineLatestSimplified that mimics the core functionality of RxJS's combineLatest operator. The function should accept an array of Observables as input and return a new Observable. This new Observable should emit a value whenever any of the input Observables emit a value. The emitted value should be an array containing the latest values from all input Observables at the time of emission.

Key Requirements:

  • The returned Observable must be a new Observable, not a modification of the input Observables.
  • The function should handle an arbitrary number of input Observables.
  • The function should correctly emit the latest values from all Observables whenever any of them emit.
  • The function should handle cases where one or more input Observables complete. The combined Observable should also complete when all input Observables complete.
  • The function should handle errors emitted by any of the input Observables. The combined Observable should emit the same error and complete.

Expected Behavior:

The combineLatestSimplified function should subscribe to all input Observables. When any of the input Observables emits a value, the function should emit an array containing the latest values from all input Observables. The order of values in the emitted array should correspond to the order of the input Observables in the input array.

Edge Cases to Consider:

  • Empty Input Array: If the input array is empty, the function should return an empty Observable that never emits or completes.
  • Observables that never emit: The combined Observable should not emit if any of the input Observables never emit a value.
  • Error Handling: If any of the input Observables emits an error, the combined Observable should immediately emit that error and complete.
  • Completion: The combined Observable should complete when all input Observables have completed.

Examples

Example 1:

Input: [Observable of 1, Observable of 2, Observable of 3]
Output: [1, 2, 3]
Explanation: Each Observable emits a single value. The combined Observable emits an array containing the latest values from each.

Example 2:

Input: [Observable of 'a', Observable of 'b', Observable of 'c']
Output: ['a', 'b', 'c']
Explanation: Similar to Example 1, but with string values.

Example 3:

Input: [Observable of 1, Observable that emits after 2 seconds (2), Observable of 3]
Output: [1, 3] (emitted after 2 seconds)
Explanation: The Observable emitting after 2 seconds delays the emission of the combined Observable.

Example 4: (Error Handling)

Input: [Observable of 1, Observable that emits an error, Observable of 3]
Output: Error
Explanation: The Observable emitting an error causes the combined Observable to emit the same error and complete.

Constraints

  • The function combineLatestSimplified must accept an array of Observables as input.
  • The function must return a new Observable.
  • The function should be implemented using RxJS operators.
  • The function should not modify the original Observables.
  • The emitted array should maintain the order of the input Observables.
  • The function should handle a maximum of 10 input Observables. (This is a practical limit for demonstration purposes; a production implementation might handle more.)

Notes

  • Consider using combineLatest internally to simplify the implementation, but the goal is to understand the underlying logic. You can use combineLatest as a reference, but the challenge is to understand how it works.
  • Think about how to track the latest values from each Observable.
  • Pay close attention to the completion and error handling scenarios.
  • Use Subscription to manage the subscriptions to the input Observables. Remember to unsubscribe when the combined Observable is unsubscribed to prevent memory leaks.
  • This is a simplified implementation and does not include all the features of the full combineLatest operator (e.g., handling initial values). The focus is on the core combination logic.
Loading editor...
typescript