Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 0004136

Browse files
committed
refactor(utils): extract asyncSuit scope
1 parent 1d78dbe commit 0004136

File tree

8 files changed

+80
-73
lines changed

8 files changed

+80
-73
lines changed

containers/WikiThread/logic.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,19 @@ import { useEffect } from 'react'
33

44
import {
55
buildLog,
6-
// asyncSuit,
6+
asyncSuit,
77
CONSTANT,
8-
// SR71,
9-
$solver,
10-
asyncRes,
11-
asyncErr,
128
errRescue,
139
BStore,
1410
nilOrEmpty,
1511
} from '@utils'
1612

17-
// import CONSTANT from '@utils/constant'
1813
import { githubApi } from '@services'
19-
20-
import SR71 from '@utils/async/sr71'
2114
import S from './schema'
2215

23-
// const { SR71, $solver, asyncRes, asyncErr } = asyncSuit
16+
const { SR71, $solver, asyncRes, asyncErr } = asyncSuit
2417
const { TYPE, EVENT, ERR, THREAD } = CONSTANT
18+
2519
const sr71$ = new SR71({
2620
resv_event: [EVENT.COMMUNITY_CHANGE, EVENT.TABBER_CHANGE],
2721
})

utils/async/handler.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import R from 'ramda'
33
/* import { Observable } from 'rxjs/Observable' */
44
import { of } from 'rxjs'
55

6-
import { buildLog, ERR } from '..'
6+
import constant from '../constant'
7+
78
import { TIMEOUT_THRESHOLD } from './setup'
9+
import { buildLog } from '../logger'
810

911
/* eslint-disable-next-line */
1012
const log = buildLog('Async')
1113

14+
const { ERR } = constant
15+
1216
export const TimoutObservable = of({
1317
error: ERR.TIMEOUT,
1418
details: `server has no-response in ${Math.round(

utils/async/index.js

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,2 @@
1-
import { from } from 'rxjs'
2-
import fetch from 'isomorphic-fetch'
3-
4-
import { buildLog } from '@utils'
5-
import { client, context } from './setup'
6-
7-
import { getThenHandler, getCatchHandler, formatGraphErrors } from './handler'
8-
9-
/* eslint-disable-next-line */
10-
const log = buildLog('Async')
11-
12-
const doQuery = (query, variables) =>
13-
client
14-
.query({
15-
query,
16-
variables,
17-
context,
18-
})
19-
.then(res => {
20-
if (res.errors) return formatGraphErrors(res.errors)
21-
return res.data
22-
})
23-
.catch(formatGraphErrors)
24-
25-
const doMutate = (mutation, variables) =>
26-
client
27-
.mutate({
28-
mutation,
29-
variables,
30-
context,
31-
})
32-
.then(res => {
33-
// once login user has mutation to server
34-
// then clear all the cache store in Apollo client.
35-
client.clearStore()
36-
return res.data
37-
})
38-
.catch(formatGraphErrors)
39-
40-
const GET = url =>
41-
fetch(`${url}`)
42-
.then(getThenHandler)
43-
.catch(getCatchHandler)
44-
45-
export const queryPromise = ({ query, variables }) =>
46-
from(doQuery(query, variables))
47-
48-
export const mutatePromise = ({ mutation, variables }) =>
49-
from(doMutate(mutation, variables))
50-
51-
export const restGetPromise = url => from(GET(url))
1+
export { default as SR71 } from './sr71'
2+
export const holder = 1

utils/async/methods.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { from } from 'rxjs'
2+
import fetch from 'isomorphic-fetch'
3+
4+
import { buildLog } from '../logger'
5+
import { client, context } from './setup'
6+
7+
import { getThenHandler, getCatchHandler, formatGraphErrors } from './handler'
8+
9+
/* eslint-disable-next-line */
10+
const log = buildLog('Async')
11+
12+
const doQuery = (query, variables) =>
13+
client
14+
.query({
15+
query,
16+
variables,
17+
context,
18+
})
19+
.then(res => {
20+
if (res.errors) return formatGraphErrors(res.errors)
21+
return res.data
22+
})
23+
.catch(formatGraphErrors)
24+
25+
const doMutate = (mutation, variables) =>
26+
client
27+
.mutate({
28+
mutation,
29+
variables,
30+
context,
31+
})
32+
.then(res => {
33+
// once login user has mutation to server
34+
// then clear all the cache store in Apollo client.
35+
client.clearStore()
36+
return res.data
37+
})
38+
.catch(formatGraphErrors)
39+
40+
const GET = url =>
41+
fetch(`${url}`)
42+
.then(getThenHandler)
43+
.catch(getCatchHandler)
44+
45+
export const queryPromise = ({ query, variables }) =>
46+
from(doQuery(query, variables))
47+
48+
export const mutatePromise = ({ mutation, variables }) =>
49+
from(doMutate(mutation, variables))
50+
51+
export const restGetPromise = url => from(GET(url))

utils/async/setup.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ import { onError } from 'apollo-link-error'
55
import { ApolloClient } from 'apollo-client'
66
import { InMemoryCache } from 'apollo-cache-inmemory'
77
import fetch from 'isomorphic-fetch'
8-
import { GRAPHQL_ENDPOINT } from '@config'
9-
import { errRescue } from '../functions'
10-
import { ERR } from '../constants'
118

129
/* import { onError } from 'apollo-link-error' */
1310

14-
import { buildLog, BStore } from '..'
11+
import { GRAPHQL_ENDPOINT } from '@config'
12+
13+
import { errRescue } from '../functions'
14+
import constant from '../constant'
15+
import { buildLog } from '../logger'
16+
import BStore from '../bstore'
1517

1618
/* eslint-disable-next-line */
1719
const log = buildLog('Async')
20+
const { ERR } = constant
1821

1922
const graphLink = new HttpLink({ uri: GRAPHQL_ENDPOINT, fetch })
2023

utils/async/sr71.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import { TimoutObservable } from './handler'
1515
import { TIMEOUT_THRESHOLD } from './setup'
1616

17-
import { queryPromise, mutatePromise, restGetPromise } from './index'
17+
import { queryPromise, mutatePromise, restGetPromise } from './methods'
1818

1919
// import { debounceTime, switchMap, merge, timeoutWith } from 'rxjs/operator'
2020

utils/async_suit.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import SR71 from './async/sr71'
2+
import { asyncErr, asyncRes } from './graphql_helper'
3+
import { $solver } from './mobx_helper'
4+
5+
export default {
6+
SR71,
7+
asyncErr,
8+
asyncRes,
9+
$solver,
10+
}

utils/index.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
/*
22
* utils functiosn
33
*/
4-
// import SR71 from './async/sr71'
5-
// import { asyncErr, asyncRes } from './graphql_helper'
6-
// import { $solver } from './mobx_helper'
74

8-
// aka network staff
9-
// export const asyncSuit = { SR71, asyncErr, asyncRes, $solver }
5+
export { default as asyncSuit } from './async_suit'
106
export { default as CONSTANT } from './constant'
117

128
export {
@@ -26,8 +22,6 @@ export {
2622
PAYMENT_METHOD,
2723
} from './constants'
2824

29-
// export { default as CONSTANT } from './constant'
30-
3125
export { buildLog } from './logger'
3226

3327
export { default as uid } from './uid'

0 commit comments

Comments
 (0)