From ca8e2373aadd25a4ccddb1a167dde9f2fb6345e6 Mon Sep 17 00:00:00 2001 From: Jenny Gallegos Date: Mon, 2 Feb 2026 20:19:55 -0600 Subject: [PATCH] Refactor: convert to TS and add types --- index.js | 127 --------------------------------------- index.ts | 178 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+), 127 deletions(-) delete mode 100644 index.js create mode 100644 index.ts diff --git a/index.js b/index.js deleted file mode 100644 index c0f3ec3..0000000 --- a/index.js +++ /dev/null @@ -1,127 +0,0 @@ -class Observer { - constructor(handlers) { - this.handlers = handlers; - this.isUnsubscribed = false; - } - - next(value) { - if (this.handlers.next && !this.isUnsubscribed) { - this.handlers.next(value); - } - } - - error(error) { - if (!this.isUnsubscribed) { - if (this.handlers.error) { - this.handlers.error(error); - } - - this.unsubscribe(); - } - } - - complete() { - if (!this.isUnsubscribed) { - if (this.handlers.complete) { - this.handlers.complete(); - } - - this.unsubscribe(); - } - } - - unsubscribe() { - this.isUnsubscribed = true; - - if (this._unsubscribe) { - this._unsubscribe(); - } - } -} - -class Observable { - constructor(subscribe) { - this._subscribe = subscribe; - } - - static from(values) { - return new Observable((observer) => { - values.forEach((value) => observer.next(value)); - - observer.complete(); - - return () => { - console.log('unsubscribed'); - }; - }); - } - - subscribe(obs) { - const observer = new Observer(obs); - - observer._unsubscribe = this._subscribe(observer); - - return ({ - unsubscribe() { - observer.unsubscribe(); - } - }); - } -} - -const HTTP_POST_METHOD = 'POST'; -const HTTP_GET_METHOD = 'GET'; - -const HTTP_STATUS_OK = 200; -const HTTP_STATUS_INTERNAL_SERVER_ERROR = 500; - - -const userMock = { - name: 'User Name', - age: 26, - roles: [ - 'user', - 'admin' - ], - createdAt: new Date(), - isDeleated: false, -}; - -const requestsMock = [ - { - method: HTTP_POST_METHOD, - host: 'service.example', - path: 'user', - body: userMock, - params: {}, - }, - { - method: HTTP_GET_METHOD, - host: 'service.example', - path: 'user', - params: { - id: '3f5h67s4s' - }, - } -]; - -const handleRequest = (request) => { - // handling of request - return {status: HTTP_STATUS_OK}; -}; -const handleError = (error) => { - // handling of error - return {status: HTTP_STATUS_INTERNAL_SERVER_ERROR}; -}; - -const handleComplete = () => console.log('complete'); - -const requests$ = Observable.from(requestsMock); - -const subscription = requests$.subscribe({ - next: handleRequest, - error: handleError, - complete: handleComplete -}); - -subscription.unsubscribe(); diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..692017b --- /dev/null +++ b/index.ts @@ -0,0 +1,178 @@ +enum HttpMethod { + POST = 'POST', + GET = 'GET', + PUT = 'PUT', + DELETE = 'DELETE', + PATCH = 'PATCH' +}; + +enum HttpStatus { + Ok = 200, + InternalServerError = 500 +}; + +type UserType = 'admin' | 'user'; + +interface User { + name: string; + age: number; + roles: UserType[]; + createdAt: Date; + isDeleted: boolean; +}; + +interface MyRequest { + method: string; + host: string; + path: string; + body?: User; + params: { + id?: string; + }, +} + +interface Result { + status: number; +} + +type TeardownFunc = () => void; +type SubscribeFunc = (observer: Observer) => TeardownFunc; + +interface ObserverHandlers { + next?: (value: T) => void; + error?: (error: Error) => void; + complete?: () => void; +} + +interface Subscription { + unsubscribe(): void; +} + +class Observer { + private handlers: ObserverHandlers; + private isUnsubscribed: boolean; + public _unsubscribe?: TeardownFunc; + + constructor(handlers: ObserverHandlers) { + this.handlers = handlers; + this.isUnsubscribed = false; + } + + next(value: T): void { + if (this.handlers.next && !this.isUnsubscribed) { + this.handlers.next(value); + } + } + + error(error: Error): void { + if (!this.isUnsubscribed) { + if (this.handlers.error) { + this.handlers.error(error); + } + + this.unsubscribe(); + } + } + + complete(): void { + if (!this.isUnsubscribed) { + if (this.handlers.complete) { + this.handlers.complete(); + } + + this.unsubscribe(); + } + } + + unsubscribe(): void { + this.isUnsubscribed = true; + + if (this._unsubscribe) { + this._unsubscribe(); + } + } +} + +class Observable { + private _subscribe: SubscribeFunc; + + constructor(subscribe: SubscribeFunc) { + this._subscribe = subscribe; + } + + static from(values: T[]): Observable { + return new Observable((observer: Observer) => { + values.forEach((value) => observer.next(value)); + + observer.complete(); + + return () => { + console.log('unsubscribed'); + }; + }); + } + + subscribe(obs: ObserverHandlers): Subscription { + const observer = new Observer(obs); + + observer._unsubscribe = this._subscribe(observer); + + return ({ + unsubscribe(): void { + observer.unsubscribe(); + } + }); + } +} + + + +const userMock: User = { + name: 'User Name', + age: 26, + roles: [ + 'user', + 'admin' + ], + createdAt: new Date(), + isDeleted: false, +}; + +const requestsMock: MyRequest[] = [ + { + method: HttpMethod.POST, + host: 'service.example', + path: 'user', + body: userMock, + params: {}, + }, + { + method: HttpMethod.GET, + host: 'service.example', + path: 'user', + params: { + id: '3f5h67s4s' + }, + } +]; + +const handleRequest = (request: MyRequest): Result => { + // handling of request + return {status: HttpStatus.Ok}; +}; +const handleError = (error: Error): Result => { + // handling of error + return {status: HttpStatus.InternalServerError}; +}; + +const handleComplete = (): void => console.log('complete'); + +const requests$: Observable = Observable.from(requestsMock); + +const subscription = requests$.subscribe({ + next: handleRequest, + error: handleError, + complete: handleComplete +}); + +subscription.unsubscribe();