This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.
AI solution for JavaScript Coding Question on BFE.dev
57. create an Observable
Here is a possible implementation of a basic Observable class:
class Observable { constructor(subscribe) { this._subscribe = subscribe } subscribe(observer) { const safeObserver = { next: observer.next ? value => observer.next(value) : () => {}, error: observer.error ? err => { if (!safeObserver.completed) { observer.error(err) safeObserver.completed = true } } : () => {}, complete: observer.complete ? () => { if (!safeObserver.completed) { observer.complete() safeObserver.completed = true } } : () => {}, completed: false } const subscription = new Subscription(() => { // clean up actions when unsubscribed safeObserver.completed = true }) this._subscribe({ next: value => { if (!safeObserver.completed) { safeObserver.next(value) } }, error: err => { if (!safeObserver.completed) { safeObserver.error(err) safeObserver.completed = true } }, complete: () => { if (!safeObserver.completed) { safeObserver.complete() safeObserver.completed = true } subscription.unsubscribe() }, }) return subscription }}class Subscription { constructor(unsubscribe) { this._unsubscribe = unsubscribe } unsubscribe() { if (!this.closed) { this._unsubscribe() this.closed = true } }}
Explanation:
- The
Observableclass takes asubscribefunction as its only parameter. This function will receive an observer object and start delivering values to it. - The
subscribemethod of the Observable class takes an observer object, wraps it in a "safe" version that checks for error and completion, and calls thesubscribefunction with this safe observer. It also returns aSubscriptionobject that the observer can use to unsubscribe. - The "safe" observer object is created from the original observer object, with default empty functions for
errorandcompleteif they are not provided. It also has acompletedproperty that is set tofalseby default, and will be set totrueafter an error or completion event is triggered to ensure they are only delivered once. - The
subscribefunction passed in the constructor ofObservablewould be executed with a "real" observer that the user provides. It will deliver values to it through itsnextfunction, and check for error or completion events to deliver them to the "safe" observer through itserrororcompletefunctions. - When an error or completion event is delivered to the safe observer, it marks the
completedproperty astrueand returns. Thesubscribefunction is then automatically unsubscribed by calling theunsubscribemethod of theSubscriptionobject obtained earlier. - The
Subscriptionclass is a simple class that has aunsubscribemethod, which calls theunsubscribefunction passed to its constructor, and marks itself asclosed. This is used to prevent double unsubscription.
Note that this is just one possible implementation of Observable, there are many other ways to achieve the same result or optimize some parts of the code.