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
Observable
class takes asubscribe
function as its only parameter. This function will receive an observer object and start delivering values to it. - The
subscribe
method of the Observable class takes an observer object, wraps it in a "safe" version that checks for error and completion, and calls thesubscribe
function with this safe observer. It also returns aSubscription
object that the observer can use to unsubscribe. - The "safe" observer object is created from the original observer object, with default empty functions for
error
andcomplete
if they are not provided. It also has acompleted
property that is set tofalse
by default, and will be set totrue
after an error or completion event is triggered to ensure they are only delivered once. - The
subscribe
function passed in the constructor ofObservable
would be executed with a "real" observer that the user provides. It will deliver values to it through itsnext
function, and check for error or completion events to deliver them to the "safe" observer through itserror
orcomplete
functions. - When an error or completion event is delivered to the safe observer, it marks the
completed
property astrue
and returns. Thesubscribe
function is then automatically unsubscribed by calling theunsubscribe
method of theSubscription
object obtained earlier. - The
Subscription
class is a simple class that has aunsubscribe
method, which calls theunsubscribe
function 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.