Commit 6369d976 by Vladislav Lagunov

Исправление ошибок

parent 2f06a7e2
import * as either from './either'; import * as either from '../either';
import { Either } from './either'; import { Either } from '../either';
import { absurd } from './internal'; import { absurd } from '../types';
// Alias // Alias
...@@ -66,6 +66,14 @@ export class EffBase<Error, Success> { ...@@ -66,6 +66,14 @@ export class EffBase<Error, Success> {
return new Chain(this.toEff(), andThen); return new Chain(this.toEff(), andThen);
} }
run(onNext: (x: Either<Error, Success>) => void, onComplete: () => void): () => void {
return go(this.toEff(), onNext, onComplete);
}
subscribe(onNext: (x: Either<Error, Success>) => void, onComplete: () => void): () => void {
return go(this.toEff(), onNext, onComplete);
}
toEff() { toEff() {
return this as any as Eff<Error, Success>; return this as any as Eff<Error, Success>;
} }
...@@ -197,7 +205,7 @@ export function go<Error, Success>(effect: Eff<Error, Success>, onNext: (x: Eith ...@@ -197,7 +205,7 @@ export function go<Error, Success>(effect: Eff<Error, Success>, onNext: (x: Eith
} }
if (effect instanceof Thunk) { if (effect instanceof Thunk) {
onNext(effect.run.apply(_this, effect.args)); onNext(effect._run.apply(_this, effect.args));
onComplete(); onComplete();
return noopFunc; return noopFunc;
} }
...@@ -295,7 +303,7 @@ export class Pure<Error, Success> extends EffBase<Error, Success> { ...@@ -295,7 +303,7 @@ export class Pure<Error, Success> extends EffBase<Error, Success> {
export class Thunk<Error, Success> extends EffBase<Error, Success> { export class Thunk<Error, Success> extends EffBase<Error, Success> {
constructor( constructor(
readonly run: (...args: unknown[]) => Either<Error, Success>, readonly _run: (...args: unknown[]) => Either<Error, Success>,
readonly args: unknown[], readonly args: unknown[],
) { super(); } ) { super(); }
} }
......
import { Expr } from '~/types'; import { Expr } from '../types';
/** convenient instance methods for `Either` */ /** convenient instance methods for `Either` */
...@@ -183,7 +183,6 @@ export function traverse<L,A,B>(arr: Array<unknown>, f?: Function): Either<L,unk ...@@ -183,7 +183,6 @@ export function traverse<L,A,B>(arr: Array<unknown>, f?: Function): Either<L,unk
} }
export const Either = { export const Either = {
Left, Right, failure, success, left, of, ap, traverse, Left, Right, failure, success, left, of, ap, traverse,
}; };
import { HasEffect, Subscribe, Eff } from './eff'; import { HasEffect, Subscribe, Eff } from '../eff';
import * as eff from './eff'; import * as eff from '../eff';
import { Decoder, Problem } from './decode'; import { Decoder, Problem } from '../decoder';
import { Either } from './either'; import { Either } from '../either';
import * as either from './either'; import * as either from '../either';
/** http method */ /** http method */
...@@ -24,7 +24,7 @@ export interface RequestProgress extends Request { ...@@ -24,7 +24,7 @@ export interface RequestProgress extends Request {
} }
/** raw error */ /** Raw error */
export type HttpError = export type HttpError =
| { tag: 'BadUrl', desc: string } | { tag: 'BadUrl', desc: string }
| { tag: 'BadPayload', desc: string } | { tag: 'BadPayload', desc: string }
...@@ -34,7 +34,7 @@ export type HttpError = ...@@ -34,7 +34,7 @@ export type HttpError =
| { tag: 'NetworkError' } | { tag: 'NetworkError' }
/** responce */ /** Responce */
export interface Response { export interface Response {
url: string; url: string;
status: number; status: number;
...@@ -44,12 +44,12 @@ export interface Response { ...@@ -44,12 +44,12 @@ export interface Response {
} }
/** query params */ /** Query params */
export type ParamsPrimitive = number|string|undefined|null; export type ParamsPrimitive = number|string|undefined|null;
export type Params = Record<string, ParamsPrimitive|ParamsPrimitive[]>; export type Params = Record<string, ParamsPrimitive|ParamsPrimitive[]>;
/** progress */ /** Progress */
export type Progress = export type Progress =
| { tag: 'Computable', total: number, loaded: number } | { tag: 'Computable', total: number, loaded: number }
| { tag: 'Uncomputable' } | { tag: 'Uncomputable' }
...@@ -58,12 +58,44 @@ export type Progress = ...@@ -58,12 +58,44 @@ export type Progress =
export class HttpEffect<A> extends HasEffect<HttpError, A> { export class HttpEffect<A> extends HasEffect<HttpError, A> {
constructor( constructor(
readonly request: Request|RequestProgress, readonly request: Request|RequestProgress,
) { super(); }; ) { super(); };
toEffect() { toEffect() {
return new Subscribe<HttpError, any>((onNext, onComplete) => { return new Subscribe<HttpError, any>((onNext, onComplete) => doXHR(this.request, onNext, onComplete));
const req = this.request; }
}
/** send a request */
export function send(req: RequestProgress): HttpEffect<Either<Progress, Response>>;
export function send(req: Request): HttpEffect<Response>;
export function send(req: Request|RequestProgress): HttpEffect<unknown> {
return new HttpEffect(req);
}
/** shortcut for GET requests */
export function get(url: string, request?: Omit<RequestProgress, 'url'|'method'>): HttpEffect<Either<Progress, Response>>;
export function get(url: string, request?: Omit<Request, 'url'|'method'>): HttpEffect<Response>;
export function get(url: string, request?: Omit<Request|RequestProgress, 'url'|'method'>): HttpEffect<unknown> {
return send({ ...request, method: 'GET', url });
}
/** shortcut for POST requests */
export function post(url: string, request?: Omit<RequestProgress, 'url'|'method'>): HttpEffect<Either<Progress, Response>>;
export function post(url: string, request?: Omit<Request, 'url'|'method'>): HttpEffect<Response>;
export function post(url: string, request?: Omit<Request|RequestProgress, 'url'|'method'>): HttpEffect<unknown> {
return send({ ...request, method: 'POST', url });
}
/**
* Реализания запроса
*/
export function doXHR(req: Request, onNext: (x: Either<HttpError, Response>) => void, onComplete: () => void): () => void;
export function doXHR(req: RequestProgress, onNext: (x: Either<HttpError, Either<Progress, Response>>) => void, onComplete: () => void): () => void;
export function doXHR(req: Request|RequestProgress, onNext: (x: Either<HttpError, any>) => void, onComplete: () => void): () => void {
const onSuccess = (x: Response) => ('progress' in req && req.progress ? onNext(either.right(either.right(x))) : onNext(either.right(x)), onComplete()); const onSuccess = (x: Response) => ('progress' in req && req.progress ? onNext(either.right(either.right(x))) : onNext(either.right(x)), onComplete());
const onProgress = (x: Progress) => 'progress' in req && req.progress ? onNext(either.right(either.left(x))) : void 0; const onProgress = (x: Progress) => 'progress' in req && req.progress ? onNext(either.right(either.left(x))) : void 0;
const onFailure = (x: HttpError) => (onNext(either.left(x)), onComplete()); const onFailure = (x: HttpError) => (onNext(either.left(x)), onComplete());
...@@ -100,32 +132,6 @@ export class HttpEffect<A> extends HasEffect<HttpError, A> { ...@@ -100,32 +132,6 @@ export class HttpEffect<A> extends HasEffect<HttpError, A> {
const body = Object.prototype.toString.apply(req.body) === '[object Object]' ? JSON.stringify(req.body) : req.body; const body = Object.prototype.toString.apply(req.body) === '[object Object]' ? JSON.stringify(req.body) : req.body;
xhr.send(body); xhr.send(body);
return () => xhr.abort(); return () => xhr.abort();
});
}
}
/** send a request */
export function send(req: RequestProgress): HttpEffect<Either<Progress, Response>>;
export function send(req: Request): HttpEffect<Response>;
export function send(req: Request|RequestProgress): HttpEffect<unknown> {
return new HttpEffect(req);
}
/** shortcut for GET requests */
export function get(url: string, request?: Omit<RequestProgress, 'url'|'method'>): HttpEffect<Either<Progress, Response>>;
export function get(url: string, request?: Omit<Request, 'url'|'method'>): HttpEffect<Response>;
export function get(url: string, request?: Omit<Request|RequestProgress, 'url'|'method'>): HttpEffect<unknown> {
return send({ ...request, method: 'GET', url });
}
/** shortcut for POST requests */
export function post(url: string, request?: Omit<RequestProgress, 'url'|'method'>): HttpEffect<Either<Progress, Response>>;
export function post(url: string, request?: Omit<Request, 'url'|'method'>): HttpEffect<Response>;
export function post(url: string, request?: Omit<Request|RequestProgress, 'url'|'method'>): HttpEffect<unknown> {
return send({ ...request, method: 'POST', url });
} }
...@@ -146,7 +152,7 @@ export function expectJSON<A>(decoder: Decoder<A>): (resp: Response) => Eff<Http ...@@ -146,7 +152,7 @@ export function expectJSON<A>(decoder: Decoder<A>): (resp: Response) => Eff<Http
} }
/** parse headers from string to dict */ /** Parse headers from string to dict */
function parseHeaders(rawHeaders: string): Record<string, string> { function parseHeaders(rawHeaders: string): Record<string, string> {
const output = {}; const output = {};
const lines = rawHeaders.split('\r\n'); const lines = rawHeaders.split('\r\n');
...@@ -161,7 +167,7 @@ function parseHeaders(rawHeaders: string): Record<string, string> { ...@@ -161,7 +167,7 @@ function parseHeaders(rawHeaders: string): Record<string, string> {
} }
/** build an url */ /** Build an url */
export function join(...args: Array<string|Params>): string { export function join(...args: Array<string|Params>): string {
let path = ''; let path = '';
let params = {} as Record<string, string>; let params = {} as Record<string, string>;
......
import { Expr } from './internal/expr'; import { Expr } from '../types';
/** adt */ /** adt */
...@@ -7,7 +7,7 @@ export type Maybe<A> = Option<A> ...@@ -7,7 +7,7 @@ export type Maybe<A> = Option<A>
/** instance method for convenience */ /** instance method for convenience */
export class OptionBase<A> { export class MaybeBase<A> {
readonly _A: A; readonly _A: A;
/** map */ /** map */
...@@ -63,14 +63,14 @@ export class OptionBase<A> { ...@@ -63,14 +63,14 @@ export class OptionBase<A> {
/** empty container */ /** empty container */
export class None<A> extends OptionBase<A> { export class None<A> extends MaybeBase<A> {
readonly _a: A; readonly _a: A;
readonly tag: 'None' = 'None'; readonly tag: 'None' = 'None';
} }
/** container with a value */ /** container with a value */
export class Some<A> extends OptionBase<A> { export class Some<A> extends MaybeBase<A> {
readonly _a: A; readonly _a: A;
readonly tag: 'Some' = 'Some'; readonly tag: 'Some' = 'Some';
constructor( constructor(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment