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
127 changes: 0 additions & 127 deletions index.js

This file was deleted.

152 changes: 152 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
export const enum HttpMethod {
POST = "POST",
GET = "GET",
}

export const enum HttpStatus {
OK = 200,
INTERNAL_SERVER_ERROR = 500,
}

export interface IHandlers<T> {
next?: (value: T) => void;
error?: (error: any) => void;
complete?: () => void;
}

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

export interface IRequest {
method: string;
host: string;
path: string;
body?: IUser;
params: { [key: string]: string };
}

class Observer<T> {
private isUnsubscribed: boolean;
private handlers: IHandlers<T>;

constructor(handlers: IHandlers<T>) {
this.handlers = handlers;
this.isUnsubscribed = false;
}

next(value: T): void {
if (this.handlers.next && !this.isUnsubscribed) {
this.handlers.next(value);
}
}

error(error: any): 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() {
this.isUnsubscribed = true;

if (this._unsubscribe) {
this._unsubscribe();
}
}

_unsubscribe?: () => void;
}

class Observable<T> {
constructor(
private _subscribe: (observer: Observer<T>) => (() => void) | undefined
) {}

static from<T>(values: T[]): Observable<T> {
return new Observable((observer) => {
values.forEach((value) => observer.next(value));

observer.complete();

return () => {
console.log("unsubscribed");
};
});
}

subscribe(obs: IHandlers<T>) {
const observer = new Observer(obs);

observer._unsubscribe = this._subscribe(observer);

return {
unsubscribe() {
observer.unsubscribe();
},
};
}
}

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

const requestsMock: IRequest[] = [
{
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: IRequest) => {
return { status: HttpStatus.OK };
};
const handleError = (error: any) => {
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,
});

subscription.unsubscribe();