Skip to content

Commit

Permalink
#58 edge case where multiple users joins "quasi simultaneously"...
Browse files Browse the repository at this point in the history
  • Loading branch information
xeronimus@gmail.com authored and xeronimus@gmail.com committed Jun 21, 2020
1 parent 2a1a3a6 commit 1d98ace
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
12 changes: 12 additions & 0 deletions client/app/services/clientActionReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ export default function clientActionReducer(state, action) {
...state.pendingCommands,
[action.command.id]: action.command
};

if (action.command.name === 'joinRoom') {
// special case, we need to remember that we are joining / waiting for "joinedRoom"
// if first-time user, we don't have a userId. we need to know, if it is "us" that joins...

return {
...state,
pendingCommands: modifiedPendingCommands,
pendingJoinCommandId: action.command.id
};
}

return {...state, pendingCommands: modifiedPendingCommands};
}
case EVENT_RECEIVED: {
Expand Down
25 changes: 11 additions & 14 deletions client/app/services/eventReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,9 @@ const eventActionHandlers = {
*/
[EVENT_ACTION_TYPES.joinedRoom]: {
fn: (state, payload, event) => {
if (state.userId) {
// if our client state has already a userId set, this event indicates that someone else joined
const modifiedUsers = {...state.users};
modifiedUsers[event.userId] = payload.users[event.userId];
return {
...state,
users: modifiedUsers
};
} else {
if (state.pendingJoinCommandId && state.pendingJoinCommandId === event.correlationId) {
// you joined

// set the page title
document.title = `PoinZ - ${event.roomId}`;

clientSettingsStore.setPresetUserId(event.userId);

// server sends current room state (users, stories, etc.)
Expand All @@ -125,7 +114,16 @@ const eventActionHandlers = {
userId: event.userId,
selectedStory: payload.selectedStory,
users: payload.users || {},
stories: payload.stories || {}
stories: payload.stories || {},
pendingJoinCommand: undefined
};
} else {
// if our client state has already a userId set, this event indicates that someone else joined
const modifiedUsers = {...state.users};
modifiedUsers[event.userId] = payload.users[event.userId];
return {
...state,
users: modifiedUsers
};
}
},
Expand All @@ -147,7 +145,6 @@ const eventActionHandlers = {
fn: (state, payload, event) => {
// If your user (in this or in another browser) left the room
if (state.userId === event.userId) {
document.title = 'PoinZ';
return {...initialState()};
}

Expand Down
4 changes: 2 additions & 2 deletions client/app/store/clientSettingsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function getPresetUserId() {
return getItem('presetUserId');
}

function setPresetUserId(username) {
setItem('presetUserId', username);
function setPresetUserId(userId) {
setItem('presetUserId', userId);
}

function getHideNewUserHints() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import reduceMultipleEventActions from './reduceMultipleEventActions';
import eventReducer from '../../../app/services/eventReducer';

test('You joining a new room', () => {
const startingState = initialState();
const cmdId = uuid();

const startingState = {
...initialState(),
pendingJoinCommandId: cmdId
};
let modifiedState;

const cmdId = uuid();
const userId = uuid();
const roomId = uuid();
const eventActions = [
Expand Down

0 comments on commit 1d98ace

Please sign in to comment.