From 7422bba8a342a10ed6652ef907db6c9f069591aa Mon Sep 17 00:00:00 2001 From: mcolta Date: Thu, 15 May 2025 07:54:43 +0300 Subject: [PATCH 1/2] Covered code with types --- index.ts | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 index.ts diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..f675c83 --- /dev/null +++ b/index.ts @@ -0,0 +1,157 @@ +type ObserverHandlers = { + next: (value: RequestType) => void; + error: (error: Error) => void; + complete: () => void; +} + +class Observer { + private handlers: ObserverHandlers; + private isUnsubscribed: boolean; + _unsubscribe: () => void; + + constructor(handlers: ObserverHandlers) { + this.handlers = handlers; + this.isUnsubscribed = false; + } + + next(value: RequestType) { + if (this.handlers.next && !this.isUnsubscribed) { + this.handlers.next(value); + } + } + + error(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 { + private _subscribe: (observer: Observer) => () => void; + + constructor(subscribe: (observer: Observer) => () => void) { + this._subscribe = subscribe; + } + + static from(values: RequestType[]) { + return new Observable((observer: Observer) => { + values.forEach((value: RequestType) => observer.next(value)); + + observer.complete(); + + return () => { + console.log('unsubscribed'); + }; + }); + } + + subscribe(obs: ObserverHandlers) { + 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; + +type User = { + name: string; + age: number; + roles: string[]; + createdAt: Date; + isDeleated: boolean; +} + + +const userMock: User = { + name: 'User Name', + age: 26, + roles: [ + 'user', + 'admin' + ], + createdAt: new Date(), + isDeleated: false, +}; + +type RequestType = { + method: string; + host: string; + path: string; + body?: User; + params?: { + [key: string]: string; + } +} + +const requestsMock: RequestType[] = [ + { + 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: RequestType) => { + // handling of request + return {status: HTTP_STATUS_OK}; +}; +const handleError = (error: 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(); From ce6d34118d4bf8a06006c51848bda107148f34f0 Mon Sep 17 00:00:00 2001 From: mcolta Date: Mon, 9 Jun 2025 16:55:57 +0300 Subject: [PATCH 2/2] Add types to the top --- index.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/index.ts b/index.ts index f675c83..bb83a70 100644 --- a/index.ts +++ b/index.ts @@ -1,3 +1,13 @@ +type RequestType = { + method: string; + host: string; + path: string; + body?: User; + params?: { + [key: string]: string; + } +} + type ObserverHandlers = { next: (value: RequestType) => void; error: (error: Error) => void; @@ -95,7 +105,6 @@ type User = { isDeleated: boolean; } - const userMock: User = { name: 'User Name', age: 26, @@ -107,16 +116,6 @@ const userMock: User = { isDeleated: false, }; -type RequestType = { - method: string; - host: string; - path: string; - body?: User; - params?: { - [key: string]: string; - } -} - const requestsMock: RequestType[] = [ { method: HTTP_POST_METHOD,