Skip to content

Commit 6c29db3

Browse files
committed
ADD request dispatcher client
1 parent 3b3a5ce commit 6c29db3

File tree

6 files changed

+111
-4
lines changed

6 files changed

+111
-4
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import voltran from './universal/partials/withBaseComponent';
22
import apiService, { ClientApiManager, ServerApiManager } from './universal/core/apiService';
3+
import requestDispatcher from './universal/utils/requestDispatcher';
4+
import useRequestDispatcher from './universal/hooks/useRequestDispatcher';
35

46
export default voltran;
5-
export { ClientApiManager, ServerApiManager, apiService };
7+
export { ClientApiManager, ServerApiManager, apiService, requestDispatcher, useRequestDispatcher };

src/renderMultiple.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function getRenderer(name, req) {
4545
cookies,
4646
url: urlWithPath,
4747
userAgent,
48+
headers,
4849
componentPath: fullComponentPath,
4950
...renderOptions
5051
};

src/universal/components/RequestDispatcher/RequestDispatcher.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ const RequestDispatcher = () => {
2323
const eventBus = getEventBus();
2424

2525
const broadcast = (effect, error, body) => {
26-
effect.Subscribers.forEach(responseName => {
26+
effect.subscribers.forEach(responseName => {
2727
eventBus.emit(`${responsePrefix}${responseName}`, { error, body });
2828
});
2929
};
3030

3131
const runEffect = (effect, params) => {
3232
effect
33-
.Request({ params })
33+
.request({ params })
3434
.then(response => broadcast(effect, null, response))
3535
.catch(error => broadcast(effect, error, null));
3636
};

src/universal/components/RequestDispatcher/RequestDispatcher.utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const isEventExist = eventName => {
66
};
77

88
const isExitCondition = condition => {
9-
return Object.keys(getProjectWindowData()).indexOf(condition) > -1 || condition === 'default';
9+
return (
10+
Object.keys(getProjectWindowData()).indexOf(condition?.toUpperCase()) > -1 ||
11+
condition === 'default'
12+
);
1013
};
1114

1215
export { isExitCondition, isEventExist };
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { useEffect, useReducer } from 'react';
2+
import requestDispatcher from '../utils/requestDispatcher';
3+
4+
function createAction(type, payload) {
5+
return {
6+
type,
7+
payload
8+
};
9+
}
10+
11+
function reducer(state, action) {
12+
switch (action.type) {
13+
case 'UPDATE_DATA':
14+
return { ...state, ...action.payload };
15+
default:
16+
return state;
17+
}
18+
}
19+
20+
const initialState = {
21+
isLoading: false,
22+
data: null,
23+
err: null
24+
};
25+
26+
function init(defaultData) {
27+
return { ...initialState, data: { ...initialState.data, ...defaultData } };
28+
}
29+
30+
const useRequestDispatcher = ({ effect, subscribe = '', defaultData }) => {
31+
const [store, dispatch] = useReducer(reducer, defaultData, init);
32+
const { isLoading, data, error } = store;
33+
34+
const fetchData = params => {
35+
if (effect) {
36+
dispatch(
37+
createAction('SET_LOADING', {
38+
isLoading: true
39+
})
40+
);
41+
if (Array.isArray(effect)) {
42+
effect?.forEach(request => {
43+
requestDispatcher.request(request, params);
44+
});
45+
} else {
46+
requestDispatcher.request(effect, params);
47+
}
48+
}
49+
};
50+
51+
useEffect(() => {
52+
if (process.env.BROWSER && subscribe) {
53+
requestDispatcher.subscribe(subscribe, (error, data) => {
54+
if (error) {
55+
dispatch(
56+
createAction('UPDATE_DATA', {
57+
isLoading: false,
58+
error
59+
})
60+
);
61+
} else {
62+
dispatch(
63+
createAction('UPDATE_DATA', {
64+
isLoading: false,
65+
error: null,
66+
data
67+
})
68+
);
69+
}
70+
});
71+
}
72+
}, []);
73+
74+
return {
75+
isLoading,
76+
data,
77+
error,
78+
fetchData
79+
};
80+
};
81+
82+
export default useRequestDispatcher;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { getEventBus } from './helper';
2+
3+
const responsePrefix = 'RequestDispatcher.Response.';
4+
const requestPrefix = 'RequestDispatcher.';
5+
6+
export default {
7+
subscribe(requestName, callback) {
8+
getEventBus().on(`${responsePrefix}${requestName}`, ({ error, body }) => {
9+
if (error) {
10+
callback(error, null);
11+
} else {
12+
callback(null, body);
13+
}
14+
});
15+
},
16+
request(requestName, params, options) {
17+
getEventBus().emit(`${requestPrefix}${requestName}`, params, options);
18+
}
19+
};

0 commit comments

Comments
 (0)