Commit 6369d976 by Vladislav Lagunov

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

parent 2f06a7e2
import * as either from './either';
import { Either } from './either';
import { absurd } from './internal';
import * as either from '../either';
import { Either } from '../either';
import { absurd } from '../types';
// Alias
......@@ -66,6 +66,14 @@ export class EffBase<Error, Success> {
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() {
return this as any as Eff<Error, Success>;
}
......@@ -197,7 +205,7 @@ export function go<Error, Success>(effect: Eff<Error, Success>, onNext: (x: Eith
}
if (effect instanceof Thunk) {
onNext(effect.run.apply(_this, effect.args));
onNext(effect._run.apply(_this, effect.args));
onComplete();
return noopFunc;
}
......@@ -295,7 +303,7 @@ export class Pure<Error, Success> extends EffBase<Error, Success> {
export class Thunk<Error, Success> extends EffBase<Error, Success> {
constructor(
readonly run: (...args: unknown[]) => Either<Error, Success>,
readonly _run: (...args: unknown[]) => Either<Error, Success>,
readonly args: unknown[],
) { super(); }
}
......
import { Expr } from '~/types';
import { Expr } from '../types';
/** convenient instance methods for `Either` */
......@@ -183,7 +183,6 @@ export function traverse<L,A,B>(arr: Array<unknown>, f?: Function): Either<L,unk
}
export const Either = {
Left, Right, failure, success, left, of, ap, traverse,
};
import { HasEffect, Subscribe, Eff } from './eff';
import * as eff from './eff';
import { Decoder, Problem } from './decode';
import { Either } from './either';
import * as either from './either';
import { HasEffect, Subscribe, Eff } from '../eff';
import * as eff from '../eff';
import { Decoder, Problem } from '../decoder';
import { Either } from '../either';
import * as either from '../either';
/** http method */
......@@ -24,7 +24,7 @@ export interface RequestProgress extends Request {
}
/** raw error */
/** Raw error */
export type HttpError =
| { tag: 'BadUrl', desc: string }
| { tag: 'BadPayload', desc: string }
......@@ -34,7 +34,7 @@ export type HttpError =
| { tag: 'NetworkError' }
/** responce */
/** Responce */
export interface Response {
url: string;
status: number;
......@@ -44,12 +44,12 @@ export interface Response {
}
/** query params */
/** Query params */
export type ParamsPrimitive = number|string|undefined|null;
export type Params = Record<string, ParamsPrimitive|ParamsPrimitive[]>;
/** progress */
/** Progress */
export type Progress =
| { tag: 'Computable', total: number, loaded: number }
| { tag: 'Uncomputable' }
......@@ -58,49 +58,10 @@ export type Progress =
export class HttpEffect<A> extends HasEffect<HttpError, A> {
constructor(
readonly request: Request|RequestProgress,
) { super(); };
toEffect() {
return new Subscribe<HttpError, any>((onNext, onComplete) => {
const req = this.request;
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 onFailure = (x: HttpError) => (onNext(either.left(x)), onComplete());
const xhr = new XMLHttpRequest();
xhr.addEventListener('error', () => onFailure({ tag: 'NetworkError' }));
xhr.addEventListener('timeout', () => onFailure({ tag: 'Timeout' }));
xhr.addEventListener('load', () => onSuccess({
url: xhr.responseURL,
status: xhr.status,
statusText: xhr.statusText,
headers: parseHeaders(xhr.getAllResponseHeaders()),
body: xhr.response || xhr.responseText,
}));
try {
xhr.open(req.method, req.url, true);
} catch (e) {
onFailure({ tag: 'BadUrl', desc: req.url });
}
if ('progress' in req && req.progress) {
xhr.addEventListener('progress', e => onProgress(e.lengthComputable ? { tag: 'Computable', loaded: e.loaded, total: e.total } : { tag: 'Uncomputable' }));
}
if (req.timeout) xhr.timeout = req.timeout;
if (typeof (req.withCredentials) !== 'undefined') xhr.withCredentials = req.withCredentials;
if (typeof (req.headers) !== 'undefined') {
for (let key in req.headers) {
if (!req.headers.hasOwnProperty(key)) continue;
const value = req.headers[key];
if (typeof(value) !== 'undefined' && value !== null)
xhr.setRequestHeader(key, String(value));
}
}
const body = Object.prototype.toString.apply(req.body) === '[object Object]' ? JSON.stringify(req.body) : req.body;
xhr.send(body);
return () => xhr.abort();
});
return new Subscribe<HttpError, any>((onNext, onComplete) => doXHR(this.request, onNext, onComplete));
}
}
......@@ -129,6 +90,51 @@ export function post(url: string, request?: Omit<Request|RequestProgress, '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 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 xhr = new XMLHttpRequest();
xhr.addEventListener('error', () => onFailure({ tag: 'NetworkError' }));
xhr.addEventListener('timeout', () => onFailure({ tag: 'Timeout' }));
xhr.addEventListener('load', () => onSuccess({
url: xhr.responseURL,
status: xhr.status,
statusText: xhr.statusText,
headers: parseHeaders(xhr.getAllResponseHeaders()),
body: xhr.response || xhr.responseText,
}));
try {
xhr.open(req.method, req.url, true);
} catch (e) {
onFailure({ tag: 'BadUrl', desc: req.url });
}
if ('progress' in req && req.progress) {
xhr.addEventListener('progress', e => onProgress(e.lengthComputable ? { tag: 'Computable', loaded: e.loaded, total: e.total } : { tag: 'Uncomputable' }));
}
if (req.timeout) xhr.timeout = req.timeout;
if (typeof (req.withCredentials) !== 'undefined') xhr.withCredentials = req.withCredentials;
if (typeof (req.headers) !== 'undefined') {
for (let key in req.headers) {
if (!req.headers.hasOwnProperty(key)) continue;
const value = req.headers[key];
if (typeof(value) !== 'undefined' && value !== null)
xhr.setRequestHeader(key, String(value));
}
}
const body = Object.prototype.toString.apply(req.body) === '[object Object]' ? JSON.stringify(req.body) : req.body;
xhr.send(body);
return () => xhr.abort();
}
/** parse response as JSON */
export function expectJSON<A>(decoder: Decoder<A>): (resp: Response) => Eff<HttpError, A> {
return resp => {
......@@ -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> {
const output = {};
const lines = rawHeaders.split('\r\n');
......@@ -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 {
let path = '';
let params = {} as Record<string, string>;
......
import { Expr } from './internal/expr';
import { Expr } from '../types';
/** adt */
......@@ -7,7 +7,7 @@ export type Maybe<A> = Option<A>
/** instance method for convenience */
export class OptionBase<A> {
export class MaybeBase<A> {
readonly _A: A;
/** map */
......@@ -63,14 +63,14 @@ export class OptionBase<A> {
/** empty container */
export class None<A> extends OptionBase<A> {
export class None<A> extends MaybeBase<A> {
readonly _a: A;
readonly tag: 'None' = 'None';
}
/** container with a value */
export class Some<A> extends OptionBase<A> {
export class Some<A> extends MaybeBase<A> {
readonly _a: A;
readonly tag: 'Some' = 'Some';
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