Skip to content

Commit f63c05a

Browse files
committed
refactor(app): add compose util + use for hocs
1 parent e7cf156 commit f63c05a

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

src/app/hocs/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import { compose } from "shared/helpers";
12
import withApollo from "./with-apollo";
23
import withRouter from "./with-router";
34
import withAntd from "./with-antd";
45

56
/**
67
* @hoc Инициализирующая логика приложения
7-
* FIXME: Потом какой-нибудь `compose` метод заинсталлим откуда-нить и покрасивше будет
8+
* @remark Содержит:
9+
* - логику инициализации antd (withAntd)
10+
* - логику подключения к API (withApollo)
11+
* - логику инициализации роутера (withRouter)
812
*/
9-
export const withHocs = (component: Component) => withAntd(withRouter(withApollo(component)));
13+
export const withHocs = compose(withAntd, withRouter, withApollo);

src/app/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ const ErrorPage = lazy(() => import("pages/error"));
1111

1212
/**
1313
* Entry-point приложения
14-
* @remark Содержит в HOC-обертке
15-
* - логику подключения к API (withApollo)
16-
* - логику инициализации роутера (withRouter)
14+
* @remark Содержит в HOC-обертке инициализирующую логику приложения
15+
* @see withHocs
1716
*/
1817
const App = () => {
1918
return (

src/shared/helpers/compose.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/** @see https://github.com/reduxjs/redux/blob/master/src/compose.ts */
2+
3+
type Func<T extends any[], R> = (...a: T) => R;
4+
5+
/**
6+
* Composes single-argument functions from right to left. The rightmost
7+
* function can take multiple arguments as it provides the signature for the
8+
* resulting composite function.
9+
*
10+
* @param funcs The functions to compose.
11+
* @returns A function obtained by composing the argument functions from right
12+
* to left. For example, `compose(f, g, h)` is identical to doing
13+
* `(...args) => f(g(h(...args)))`.
14+
*/
15+
function compose(): <R>(a: R) => R;
16+
17+
function compose<F extends Function>(f: F): F;
18+
19+
/* two functions */
20+
function compose<A, T extends any[], R>(f1: (a: A) => R, f2: Func<T, A>): Func<T, R>;
21+
22+
/* three functions */
23+
function compose<A, B, T extends any[], R>(
24+
f1: (b: B) => R,
25+
f2: (a: A) => B,
26+
f3: Func<T, A>,
27+
): Func<T, R>;
28+
29+
/* four functions */
30+
function compose<A, B, C, T extends any[], R>(
31+
f1: (c: C) => R,
32+
f2: (b: B) => C,
33+
f3: (a: A) => B,
34+
f4: Func<T, A>,
35+
): Func<T, R>;
36+
37+
/* rest */
38+
function compose<R>(f1: (a: any) => R, ...funcs: Function[]): (...args: any[]) => R;
39+
40+
function compose<R>(...funcs: Function[]): (...args: any[]) => R;
41+
42+
function compose(...funcs: Function[]) {
43+
if (funcs.length === 0) {
44+
// infer the argument type so it is usable in inference down the line
45+
return <T>(arg: T) => arg;
46+
}
47+
48+
if (funcs.length === 1) {
49+
return funcs[0];
50+
}
51+
52+
return funcs.reduce((a, b) => (...args: any) => a(b(...args)));
53+
}
54+
55+
export default compose;

src/shared/helpers/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as str from "./string";
22
import * as dom from "./dom";
33
import * as alert from "./alert";
4+
import compose from "./compose";
45

5-
export { str, dom, alert };
6+
export { str, dom, alert, compose };

0 commit comments

Comments
 (0)