Created
April 1, 2019 02:17
-
-
Save ddoronin/a5d53f22eb83217cdaf288658975b4af to your computer and use it in GitHub Desktop.
Application State
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
import RequestComposer from "src/models/request-composer"; | |
import { | |
overHistory, | |
IHistoricRequest, | |
overHistoryLatest5 | |
} from "src/functions/history"; | |
import { overHttp } from "src/functions/http"; | |
import { Observable } from "rxjs"; | |
import { Option } from "monas"; | |
import { IRequest } from "src/models/request-composer"; | |
import { computed } from "src/decorators/computed"; | |
import { filter, map } from "rxjs/operators"; | |
export interface IResponse { | |
isLoading: boolean; | |
req: Option<IRequest>; | |
resp: Option<{}>; | |
} | |
export default class AppState { | |
constructor(private composer = new RequestComposer()) {} | |
@computed get history$(): Observable<IHistoricRequest[]> { | |
return this.composer.request$.pipe(overHistory); | |
} | |
@computed get last5$(): Observable<IRequest[]> { | |
return this.composer.request$.pipe( | |
filter((_: Option<IRequest>) => _.isDefined()), | |
map((_: Option<IRequest>) => _.getOrElse({} as IRequest)), | |
overHistoryLatest5 | |
); | |
} | |
@computed get http$(): Observable<IResponse> { | |
return this.composer.request$.pipe(overHttp); | |
} | |
@computed get request(): Observable<Option<IRequest>> { | |
return this.composer.request$; | |
} | |
public setRequest(request: Option<IRequest>) { | |
this.composer.set(request); | |
} | |
} | |
export const AppStateContext = React.createContext<AppState>(new AppState()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment