Skip to content

Commit

Permalink
ADD request dispatcher client
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetkuslular committed Jun 15, 2022
1 parent 3b3a5ce commit a70e8d6
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 10 deletions.
6 changes: 2 additions & 4 deletions config/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ function replaceString() {
{
search: '__V_REQUEST_CONFIGS__',
replace: normalizeUrl(
voltranConfig.routing.requestConfigs || path.resolve(__dirname, './emptyModule.js')
voltranConfig.requestConfigs || path.resolve(__dirname, './emptyModule.js')
),
flags: 'g'
},
{
search: '__V_PREVIEW__',
replace: normalizeUrl(
voltranConfig.routing.preview || path.resolve(__dirname, './emptyModule.js')
),
replace: normalizeUrl(voltranConfig.preview || path.resolve(__dirname, './emptyModule.js')),
flags: 'g'
},
{
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import voltran from './universal/partials/withBaseComponent';
import apiService, { ClientApiManager, ServerApiManager } from './universal/core/apiService';
import requestDispatcher from './universal/utils/requestDispatcher';
import useRequestDispatcher from './universal/hooks/useRequestDispatcher';

export default voltran;
export { ClientApiManager, ServerApiManager, apiService };
export { ClientApiManager, ServerApiManager, apiService, requestDispatcher, useRequestDispatcher };
3 changes: 2 additions & 1 deletion src/renderMultiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function getRenderer(name, req) {
cookies,
url: urlWithPath,
userAgent,
headers,
componentPath: fullComponentPath,
...renderOptions
};
Expand Down Expand Up @@ -178,7 +179,7 @@ async function getResponses(renderers) {
.filter(result => result.value != null)
.reduce((obj, item) => {
const el = obj;
const name = `${item.key}_${item.id}`;
const name = `${item.key}`;

if (!el[name]) el[name] = item.value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ const RequestDispatcher = () => {
const eventBus = getEventBus();

const broadcast = (effect, error, body) => {
effect.Subscribers.forEach(responseName => {
effect.subscribers.forEach(responseName => {
eventBus.emit(`${responsePrefix}${responseName}`, { error, body });
});
};

const runEffect = (effect, params) => {
effect
.Request({ params })
.request({ params })
.then(response => broadcast(effect, null, response))
.catch(error => broadcast(effect, error, null));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ const isEventExist = eventName => {
};

const isExitCondition = condition => {
return Object.keys(getProjectWindowData()).indexOf(condition) > -1 || condition === 'default';
return (
Object.keys(getProjectWindowData()).indexOf(condition?.toUpperCase()) > -1 ||
condition === 'default'
);
};

export { isExitCondition, isEventExist };
1 change: 0 additions & 1 deletion src/universal/core/apiService/apiManager/BaseApiManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axios from 'axios';
import Cookies from 'js-cookie';
import {
CONTENT_TYPE_HEADER,
JSON_CONTENT_TYPE,
Expand Down
82 changes: 82 additions & 0 deletions src/universal/hooks/useRequestDispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { useEffect, useReducer } from 'react';
import requestDispatcher from '../utils/requestDispatcher';

function createAction(type, payload) {
return {
type,
payload
};
}

function reducer(state, action) {
switch (action.type) {
case 'UPDATE_DATA':
return { ...state, ...action.payload };
default:
return state;
}
}

const initialState = {
isLoading: false,
data: null,
err: null
};

function init(defaultData) {
return { ...initialState, data: { ...initialState.data, ...defaultData } };
}

const useRequestDispatcher = ({ effect, subscribe = '', defaultData }) => {
const [store, dispatch] = useReducer(reducer, defaultData, init);
const { isLoading, data, error } = store;

const fetchData = params => {
if (effect) {
dispatch(
createAction('SET_LOADING', {
isLoading: true
})
);
if (Array.isArray(effect)) {
effect?.forEach(request => {
requestDispatcher.request(request, params);
});
} else {
requestDispatcher.request(effect, params);
}
}
};

useEffect(() => {
if (process.env.BROWSER && subscribe) {
requestDispatcher.subscribe(subscribe, (error, data) => {
if (error) {
dispatch(
createAction('UPDATE_DATA', {
isLoading: false,
error
})
);
} else {
dispatch(
createAction('UPDATE_DATA', {
isLoading: false,
error: null,
data
})
);
}
});
}
}, []);

return {
isLoading,
data,
error,
fetchData
};
};

export default useRequestDispatcher;
19 changes: 19 additions & 0 deletions src/universal/utils/requestDispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getEventBus } from './helper';

const responsePrefix = 'RequestDispatcher.Response.';
const requestPrefix = 'RequestDispatcher.';

export default {
subscribe(requestName, callback) {
getEventBus().on(`${responsePrefix}${requestName}`, ({ error, body }) => {
if (error) {
callback(error, null);
} else {
callback(null, body);
}
});
},
request(requestName, params, options) {
getEventBus().emit(`${requestPrefix}${requestName}`, params, options);
}
};

0 comments on commit a70e8d6

Please sign in to comment.