Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Lint, prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Tejas Kumar committed Jul 16, 2018
1 parent 91bc38d commit 7005a07
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"git add"
],
"*.(tsx|ts)": [
"tslint --fix",
"tslint --fix --project .",
"prettier --write",
"git add"
]
Expand Down
2 changes: 1 addition & 1 deletion src/Context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const { Provider, Consumer: RestfulReactConsumer } = React.createContext<Restful
});

export default class RestfulReactProvider<T> extends React.Component<RestfulReactProviderProps<T>> {
render() {
public render() {
const { children, ...value } = this.props;
return <Provider value={value}>{children}</Provider>;
}
Expand Down
31 changes: 19 additions & 12 deletions src/Get.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import RestfulProvider, { RestfulReactConsumer, RestfulReactProviderProps } from "./Context";
import RestfulReactProvider, { RestfulReactConsumer, RestfulReactProviderProps } from "./Context";

/**
* A function that resolves returned data from
Expand Down Expand Up @@ -98,26 +98,33 @@ export interface GetComponentState<T> {
class ContextlessGet<T> extends React.Component<GetComponentProps<T>, Readonly<GetComponentState<T>>> {
private shouldFetchImmediately = () => !this.props.wait && !this.props.lazy;

readonly state: Readonly<GetComponentState<T>> = {
public readonly state: Readonly<GetComponentState<T>> = {
data: null, // Means we don't _yet_ have data.
response: null,
error: "",
loading: this.shouldFetchImmediately(),
};

componentDidMount() {
this.shouldFetchImmediately() && this.fetch();
public componentDidMount() {
if (this.shouldFetchImmediately()) {
this.fetch();
}
}

componentDidUpdate(prevProps: GetComponentProps<T>) {
public componentDidUpdate(prevProps: GetComponentProps<T>) {
// If the path or base prop changes, refetch!
const { path, base } = this.props;
if (prevProps.path !== path || prevProps.base !== base) {
this.shouldFetchImmediately() && this.fetch();
if (this.shouldFetchImmediately()) {
this.fetch();
}
}
}

getRequestOptions = (extraOptions?: Partial<RequestInit>, extraHeaders?: boolean | { [key: string]: string }) => {
public getRequestOptions = (
extraOptions?: Partial<RequestInit>,
extraHeaders?: boolean | { [key: string]: string },
) => {
const { requestOptions } = this.props;

if (typeof requestOptions === "function") {
Expand All @@ -143,12 +150,12 @@ class ContextlessGet<T> extends React.Component<GetComponentProps<T>, Readonly<G
};
};

fetch = async (requestPath?: string, thisRequestOptions?: RequestInit) => {
public fetch = async (requestPath?: string, thisRequestOptions?: RequestInit) => {
const { base, path } = this.props;
this.setState(() => ({ error: "", loading: true }));

const { resolve } = this.props;
const foolProofResolve = resolve || (data => data);
const foolProofResolve = resolve || (noop => noop);
const response = await fetch(`${base}${requestPath || path || ""}`, this.getRequestOptions(thisRequestOptions));

if (!response.ok) {
Expand All @@ -163,7 +170,7 @@ class ContextlessGet<T> extends React.Component<GetComponentProps<T>, Readonly<G
return data;
};

render() {
public render() {
const { children, wait, path, base } = this.props;
const { data, error, loading, response } = this.state;

Expand All @@ -189,9 +196,9 @@ function Get<T>(props: GetComponentProps<T>) {
return (
<RestfulReactConsumer>
{contextProps => (
<RestfulProvider {...contextProps} base={`${contextProps.base}${props.path}`}>
<RestfulReactProvider {...contextProps} base={`${contextProps.base}${props.path}`}>
<ContextlessGet {...contextProps} {...props} />
</RestfulProvider>
</RestfulReactProvider>
)}
</RestfulReactConsumer>
);
Expand Down
12 changes: 6 additions & 6 deletions src/Mutate.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import RestfulProvider, { RestfulReactConsumer, RestfulReactProviderProps } from "./Context";
import RestfulReactProvider, { RestfulReactConsumer, RestfulReactProviderProps } from "./Context";

/**
* An enumeration of states that a fetchable
Expand Down Expand Up @@ -70,13 +70,13 @@ export interface MutateComponentState {
* debugging.
*/
class ContextlessMutate extends React.Component<MutateComponentProps, MutateComponentState> {
readonly state: Readonly<MutateComponentState> = {
public readonly state: Readonly<MutateComponentState> = {
response: null,
loading: false,
error: "",
};

mutate = async (body?: string | {}, mutateRequestOptions?: RequestInit) => {
public mutate = async (body?: string | {}, mutateRequestOptions?: RequestInit) => {
const { base, path, verb: method, requestOptions: providerRequestOptions } = this.props;
this.setState(() => ({ error: "", loading: true }));

Expand All @@ -103,7 +103,7 @@ class ContextlessMutate extends React.Component<MutateComponentProps, MutateComp
return response;
};

render() {
public render() {
const { children, path, base } = this.props;
const { error, loading, response } = this.state;

Expand All @@ -125,9 +125,9 @@ function Mutate(props: MutateComponentProps) {
return (
<RestfulReactConsumer>
{contextProps => (
<RestfulProvider {...contextProps} base={`${contextProps.base}${props.path}`}>
<RestfulReactProvider {...contextProps} base={`${contextProps.base}${props.path}`}>
<ContextlessMutate {...contextProps} {...props} />
</RestfulProvider>
</RestfulReactProvider>
)}
</RestfulReactConsumer>
);
Expand Down
27 changes: 15 additions & 12 deletions src/Poll.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from "react";
import { RestfulReactConsumer } from "./Context";
import { RestfulProvider } from ".";
import { GetComponentState, Meta as GetComponentMeta, GetComponentProps } from "./Get";
import { GetComponentProps, GetComponentState, Meta as GetComponentMeta } from "./Get";

/**
* Meta information returned from the poll.
Expand Down Expand Up @@ -128,23 +127,24 @@ interface PollState<T> {
*/
class ContextlessPoll<T> extends React.Component<PollProps<T>, Readonly<PollState<T>>> {
private keepPolling = !this.props.lazy;
readonly state: Readonly<PollState<T>> = {

public readonly state: Readonly<PollState<T>> = {
data: null,
loading: !this.props.lazy,
lastResponse: null,
polling: this.keepPolling,
finished: false,
};

static defaultProps = {
public static defaultProps = {
interval: 1000,
resolve: (data: any) => data,
};

/**
* This thing does the actual poll.
*/
cycle = async () => {
public cycle = async () => {
// Have we stopped?
if (!this.keepPolling) {
return; // stop.
Expand Down Expand Up @@ -172,22 +172,23 @@ class ContextlessPoll<T> extends React.Component<PollProps<T>, Readonly<PollStat
data: resolve ? resolve(responseBody) : responseBody,
}));

await new Promise(resolve => setTimeout(resolve, interval)); // Wait for interval to pass.
// Wait for interval to pass.
await new Promise(resolvePromise => setTimeout(resolvePromise, interval));
this.cycle(); // Do it all again!
};

start = async () => {
public start = async () => {
this.keepPolling = true;
this.setState(() => ({ polling: true })); // let everyone know we're done here.}
this.cycle();
};

stop = async () => {
public stop = async () => {
this.keepPolling = false;
this.setState(() => ({ polling: false, finished: true })); // let everyone know we're done here.}
};

componentDidMount() {
public componentDidMount() {
const { path, lazy } = this.props;

if (!path) {
Expand All @@ -196,14 +197,16 @@ class ContextlessPoll<T> extends React.Component<PollProps<T>, Readonly<PollStat
);
}

!lazy && this.start();
if (!lazy) {
this.start();
}
}

componentWillUnmount() {
public componentWillUnmount() {
this.stop();
}

render() {
public render() {
const { lastResponse: response, data, polling, loading, error, finished } = this.state;
const { children, base, path } = this.props;

Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export { default as Poll } from "./Poll";
export { default as Mutate } from "./Mutate";

export { Get };

export default Get;
24 changes: 23 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{}
{
"extends": ["tslint:recommended", "tslint-config-prettier", "tslint-plugin-blank-line"],
"rules": {
"blank-line": true,
"one-variable-per-declaration": false,
"semicolon": false,
"quotemark": "double",
"variable-name": ["allow-pascal-case"],
"indent": false,
"import-name": false,
"object-literal-sort-keys": false,
"interface-name": false,
"no-console": [true, "log", "error"],
"no-unused-expression": true,
"no-implicit-dependencies": [true, "dev"],
"no-unused-variable": [true, { "check-parameters": true }],
"no-duplicate-imports": true,
"completed-docs": false,
"import-spacing": true,
"match-default-export-name": true,
"member-ordering": false
}
}

0 comments on commit 7005a07

Please sign in to comment.