From ae83466cad42b43b5507b07f621faea87068024c Mon Sep 17 00:00:00 2001 From: Vitaliy Pasiuta Date: Wed, 23 Aug 2023 18:00:08 +0300 Subject: [PATCH] init --- .gitignore | 1 + index.js | 3 +- package-lock.json | 12 ++++ src/index.ts | 158 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 package-lock.json create mode 100644 src/index.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/index.js b/index.js index c0f3ec3..b6e255d 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ class Observer { constructor(handlers) { this.handlers = handlers; this.isUnsubscribed = false; + this._unsubscribe = undefined; } next(value) { @@ -33,7 +34,7 @@ class Observer { unsubscribe() { this.isUnsubscribed = true; - if (this._unsubscribe) { +if (this._unsubscribe) { this._unsubscribe(); } } diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..7f6fff6 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,12 @@ +{ + "requires": true, + "lockfileVersion": 1, + "dependencies": { + "typescript": { + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", + "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", + "dev": true + } + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..49817a1 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,158 @@ +interface Handlers{ + next: Function, + error:Function, + complete: Function, +} + +interface User{ + name: string, + age: number, + roles: string[], + createdAt: Date, + isDeleated: boolean, +} + +class Observer { + handlers: Handlers; + isUnsubscribed: boolean; + _unsubscribe: Function; + + constructor(handlers: Handlers) { + this.handlers = handlers; + this.isUnsubscribed = false; + } + + next(value: Observer): void { + if (this.handlers.next && !this.isUnsubscribed) { + this.handlers.next(value); + } + } + + 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 { + _subscribe: Function; + + constructor(subscribe) { + this._subscribe = subscribe; + } + + static from(values) { + return new Observable((observer: Observer): Function => { + values.forEach((value) => observer.next(value)); + + observer.complete(); + + return (): void => { + console.log('unsubscribed'); + }; + }); + } + + subscribe(obs): Object { + const observer: Observer = new Observer(obs); + + observer._unsubscribe = this._subscribe(observer); + + return ({ + unsubscribe() { + observer.unsubscribe(); + } + }); + } +} + +type HTTP_METHODS = 'POST' | 'GET'; +type HTTP_STATUS = 200 | 500; + +const HTTP_POST_METHOD: HTTP_METHODS = 'POST'; +const HTTP_GET_METHOD: HTTP_METHODS = 'GET'; + +const HTTP_STATUS_OK: HTTP_STATUS = 200; +const HTTP_STATUS_INTERNAL_SERVER_ERROR: HTTP_STATUS = 500; + + +const userMock: User = { + name: 'User Name', + age: 26, + roles: [ + 'user', + 'admin' + ], + createdAt: new Date(), + isDeleated: false, +}; + +const requestsMock: { + method: HTTP_METHODS, + host: string, + path: string, + body?: object, + params: object, +}[] = [ + { + method: HTTP_POST_METHOD, + host: 'service.example', + path: 'user', + body: userMock, + params: {}, + }, + { + method: HTTP_GET_METHOD, + host: 'service.example', + path: 'user', + params: { + id: '3f5h67s4s' + }, + } +]; + +type RESPONSE = {status: HTTP_STATUS}; + +const handleRequest = (request): RESPONSE => { + // handling of request + return {status: HTTP_STATUS_OK}; +}; +const handleError = (error): RESPONSE => { + // handling of error + return {status: HTTP_STATUS_INTERNAL_SERVER_ERROR}; +}; + +const handleComplete = (): void => console.log('complete'); + +const requests$ = Observable.from(requestsMock); + +const subscription = requests$.subscribe({ + next: handleRequest, + error: handleError, + complete: handleComplete +}); + +subscription.unsubscribe();