Skip to content

Commit

Permalink
refactored centrifugo event names
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-8 committed Nov 21, 2024
1 parent d6ad3a8 commit fedc12f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/navigation/courier/TasksPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
selectIsCentrifugoConnected,
selectSettingsLatLng,
} from '../../redux/App/selectors';
import { connect as connectCentrifugo } from '../../redux/middlewares/CentrifugoMiddleware/actions';
import { connectCentrifugo } from '../../redux/middlewares/CentrifugoMiddleware/actions';
import { useGetMyTasksQuery } from '../../redux/api/slice';

const styles = StyleSheet.create({
Expand Down
4 changes: 2 additions & 2 deletions src/navigation/restaurant/components/WebSocketIndicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
selectIsCentrifugoConnected,
selectIsCentrifugoConnecting,
} from '../../../redux/App/selectors';
import { connect } from '../../../redux/middlewares/CentrifugoMiddleware/actions';
import { connectCentrifugo } from '../../../redux/middlewares/CentrifugoMiddleware/actions';

const WebSocketIndicator = () => {
const connecting = useSelector(selectIsCentrifugoConnecting);
Expand Down Expand Up @@ -37,7 +37,7 @@ const WebSocketIndicator = () => {
<Button
variant="link"
onPress={() => {
dispatch(connect());
dispatch(connectCentrifugo());
}}>
<Text style={styles.text}>{t('RETRY')}</Text>
</Button>
Expand Down
12 changes: 6 additions & 6 deletions src/redux/App/__tests__/reducers.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { omit } from 'lodash';
import {
connected,
disconnected,
centrifugoConnected,
centrifugoDisconnected,
} from '../../middlewares/CentrifugoMiddleware';
import reducer from '../reducers';

describe('Redux | App | Reducers', () => {
test(`${connected}`, () => {
test(`${centrifugoConnected}`, () => {
const initialState = reducer(undefined, {});
const newState = reducer(initialState, connected());
const newState = reducer(initialState, centrifugoConnected());

const restOldState = omit(initialState, ['isCentrifugoConnected']);
const restNewState = omit(newState, ['isCentrifugoConnected']);
Expand All @@ -21,12 +21,12 @@ describe('Redux | App | Reducers', () => {
expect(restNewState).toEqual(restOldState);
});

test(`${disconnected}`, () => {
test(`${centrifugoDisconnected}`, () => {
const initialState = {
...reducer(undefined, {}),
isCentrifugoConnected: true,
};
const newState = reducer(initialState, disconnected());
const newState = reducer(initialState, centrifugoDisconnected());

const restOldState = omit(initialState, ['isCentrifugoConnected']);
const restNewState = omit(newState, ['isCentrifugoConnected']);
Expand Down
10 changes: 5 additions & 5 deletions src/redux/App/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { AppState } from 'react-native';
import Config from 'react-native-config';

import { connected, disconnected } from '../middlewares/CentrifugoMiddleware';
import { centrifugoConnected, centrifugoDisconnected } from '../middlewares/CentrifugoMiddleware';

import {
ACCEPT_PRIVACY_POLICY,
Expand Down Expand Up @@ -56,7 +56,7 @@ import {

import { EVENT as EVENT_ORDER } from '../../domain/Order';
import { EVENT as EVENT_TASK_COLLECTION } from '../../domain/TaskCollection';
import { connect } from '../middlewares/CentrifugoMiddleware/actions';
import { connectCentrifugo } from '../middlewares/CentrifugoMiddleware/actions';

const initialState = {
customBuild: !!Config.DEFAULT_SERVER,
Expand Down Expand Up @@ -170,7 +170,7 @@ export default (state = initialState, action = {}) => {
loading: action.payload,
};

case connect.type: {
case connectCentrifugo.type: {
if (state.isCentrifugoConnected) {
return state;
}
Expand All @@ -181,14 +181,14 @@ export default (state = initialState, action = {}) => {
};
}

case connected.type:
case centrifugoConnected.type:
return {
...state,
isCentrifugoConnecting: false,
isCentrifugoConnected: true,
};

case disconnected.type:
case centrifugoDisconnected.type:
return {
...state,
isCentrifugoConnecting: false,
Expand Down
4 changes: 2 additions & 2 deletions src/redux/Dispatch/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createAction } from 'redux-actions';
import _ from 'lodash';
import NavigationHolder from '../../NavigationHolder';
import i18n from '../../i18n';
import { connect } from '../middlewares/CentrifugoMiddleware/actions';
import { connectCentrifugo } from '../middlewares/CentrifugoMiddleware/actions';

import {
createTaskListFailure,
Expand Down Expand Up @@ -191,7 +191,7 @@ export function initialize() {
dispatch(loadUsersSuccess(users['hydra:member']));
dispatch(loadUnassignedTasksSuccess(unassignedTasks['hydra:member']));
dispatch(loadTaskListsSuccess(taskLists['hydra:member']));
dispatch(connect());
dispatch(connectCentrifugo());
dispatch(_initialize());
})
.catch(e => dispatch(loadUnassignedTasksFailure(e)));
Expand Down
8 changes: 4 additions & 4 deletions src/redux/middlewares/CentrifugoMiddleware/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { updateTask } from '../../Dispatch/actions';

export const CENTRIFUGO_MESSAGE = '@centrifugo/MESSAGE';

export const connect = createAction('@centrifugo/CONNECT');
export const disconnect = createAction('@centrifugo/DISCONNECT');
export const connectCentrifugo = createAction('@centrifugo/CONNECT');
export const disconnectCentrifugo = createAction('@centrifugo/DISCONNECT');

export const connected = createAction('@centrifugo/CONNECTED');
export const disconnected = createAction('@centrifugo/DISCONNECTED');
export const centrifugoConnected = createAction('@centrifugo/CONNECTED');
export const centrifugoDisconnected = createAction('@centrifugo/DISCONNECTED');

export const _message = createAction(CENTRIFUGO_MESSAGE);

Expand Down
20 changes: 10 additions & 10 deletions src/redux/middlewares/CentrifugoMiddleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import parseUrl from 'url-parse';

import {
CENTRIFUGO_MESSAGE,
connect,
connected,
disconnect,
disconnected,
connectCentrifugo,
centrifugoConnected,
disconnectCentrifugo,
centrifugoDisconnected,
message,
} from './actions';

Expand Down Expand Up @@ -63,12 +63,12 @@ let subscription = null;
export default ({ getState, dispatch }) => {
return next => action => {
if (action.type === LOGOUT_SUCCESS) {
dispatch(disconnect());
dispatch(disconnectCentrifugo());
return next(action);
}

if (
action.type === connect.type ||
action.type === connectCentrifugo.type ||
shouldConnectBasedOnAppState(getState, action)
) {
const state = getState();
Expand Down Expand Up @@ -113,8 +113,8 @@ export default ({ getState, dispatch }) => {

centrifuge.setToken(tokenResponse.token);

centrifuge.on('connect', context => dispatch(connected(context)));
centrifuge.on('disconnect', context => dispatch(disconnected(context)));
centrifuge.on('connect', context => dispatch(centrifugoConnected(context)));
centrifuge.on('disconnect', context => dispatch(centrifugoDisconnected(context)));

subscription = centrifuge.subscribe(
`${tokenResponse.namespace}_events#${user.username}`,
Expand All @@ -128,7 +128,7 @@ export default ({ getState, dispatch }) => {
}

if (
action.type === disconnect.type ||
action.type === disconnectCentrifugo.type ||
shouldDisconnectBasedOnAppState(getState, action)
) {
if (subscription) {
Expand All @@ -148,4 +148,4 @@ export default ({ getState, dispatch }) => {
};
};

export { CENTRIFUGO_MESSAGE, connected, disconnected };
export { CENTRIFUGO_MESSAGE, centrifugoConnected, centrifugoDisconnected };

0 comments on commit fedc12f

Please sign in to comment.