Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 87 additions & 110 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,127 +1,104 @@
"use strict";
var HTTPMethod;
(function (HTTPMethod) {
HTTPMethod["POST"] = "POST";
HTTPMethod["GET"] = "GET";
})(HTTPMethod || (HTTPMethod = {}));
var HTTPStatus;
(function (HTTPStatus) {
HTTPStatus[HTTPStatus["OK"] = 200] = "OK";
HTTPStatus[HTTPStatus["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
})(HTTPStatus || (HTTPStatus = {}));
const userMock = {
name: 'User Name',
age: 26,
roles: ['user', 'admin'],
createdAt: new Date(),
isDeleted: false,
};
const requestsMock = [
{
method: HTTPMethod.POST,
host: 'service.example',
path: 'user',
body: userMock,
params: {},
},
{
method: HTTPMethod.GET,
host: 'service.example',
path: 'user',
params: { id: '3f5h67s4s' },
},
];
class Observer {
constructor(handlers) {
this.handlers = handlers;
this.isUnsubscribed = false;
}

next(value) {
if (this.handlers.next && !this.isUnsubscribed) {
this.handlers.next(value);
constructor(handlers) {
this.handlers = handlers;
this.isUnsubscribed = false;
}
}

error(error) {
if (!this.isUnsubscribed) {
if (this.handlers.error) {
this.handlers.error(error);
}

this.unsubscribe();
next(value) {
if (this.handlers.next && !this.isUnsubscribed) {
this.handlers.next(value);
}
}
}

complete() {
if (!this.isUnsubscribed) {
if (this.handlers.complete) {
this.handlers.complete();
}

this.unsubscribe();
error(error) {
if (!this.isUnsubscribed) {
if (this.handlers.error) {
this.handlers.error(error);
}
this.unsubscribe();
}
}
}

unsubscribe() {
this.isUnsubscribed = true;

if (this._unsubscribe) {
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();
}
});
}
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) || undefined;
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};
// handling of request
return { status: HTTPStatus.OK };
};
const handleError = (error) => {
// handling of error
return {status: HTTP_STATUS_INTERNAL_SERVER_ERROR};
// handling of error
return { status: HTTPStatus.INTERNAL_SERVER_ERROR };
};

const handleComplete = () => console.log('complete');

const requests$ = Observable.from(requestsMock);

const subscription = requests$.subscribe({
next: handleRequest,
error: handleError,
complete: handleComplete
next: handleRequest,
error: handleError,
complete: handleComplete,
});

// Unsubscribing
subscription.unsubscribe();
146 changes: 146 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
enum HTTPMethod {
POST = 'POST',
GET = 'GET',
}

enum HTTPStatus {
OK = 200,
INTERNAL_SERVER_ERROR = 500,
}

interface User {
readonly name: string;
readonly age: number;
readonly roles: readonly string[];
readonly createdAt: Date;
readonly isDeleted: boolean;
}

const userMock: User = {
name: 'User Name',
age: 26,
roles: ['user', 'admin'],
createdAt: new Date(),
isDeleted: false,
};

interface RequestMock {
readonly method: HTTPMethod;
readonly host: string;
readonly path: string;
readonly body?: User;
readonly params: Record<string, string>;
}

const requestsMock: readonly RequestMock[] = [
{
method: HTTPMethod.POST,
host: 'service.example',
path: 'user',
body: userMock,
params: {},
},
{
method: HTTPMethod.GET,
host: 'service.example',
path: 'user',
params: { id: '3f5h67s4s' },
},
];

type ObserverHandlers<T> = {
next?: (value: T) => void;
error?: (error: Error) => void;
complete?: () => void;
};

class Observer<T> {
private readonly handlers: ObserverHandlers<T>;
private isUnsubscribed: boolean;
public _unsubscribe?: () => void;

constructor(handlers: ObserverHandlers<T>) {
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<T> {
private readonly _subscribe: (observer: Observer<T>) => (() => void) | void;

constructor(subscribe: (observer: Observer<T>) => (() => void) | void) {
this._subscribe = subscribe;
}

static from<T>(values: readonly T[]): Observable<T> {
return new Observable((observer) => {
values.forEach((value) => observer.next(value));
observer.complete();
return () => {
console.log('unsubscribed');
};
});
}

subscribe(obs: ObserverHandlers<T>) {
const observer = new Observer(obs);
observer._unsubscribe = this._subscribe(observer) || undefined;
return {
unsubscribe: () => observer.unsubscribe(),
};
}
}

const handleRequest = (request: RequestMock): { status: HTTPStatus } => {
// handling of request
return { status: HTTPStatus.OK };
};

const handleError = (error: Error): { status: HTTPStatus } => {
// handling of error
return { status: HTTPStatus.INTERNAL_SERVER_ERROR };
};

const handleComplete = (): void => console.log('complete');

const requests$ = Observable.from(requestsMock);

const subscription = requests$.subscribe({
next: handleRequest,
error: handleError,
complete: handleComplete,
});

// Unsubscribing
subscription.unsubscribe();
Loading