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 13, 2022
1 parent 3b3a5ce commit 6c29db3
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 4 deletions.
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 };
1 change: 1 addition & 0 deletions 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
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 };
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 6c29db3

Please sign in to comment.