better naming

This commit is contained in:
Bruno Windels 2019-01-17 18:52:22 +01:00
parent 146e651dae
commit 2920deaefe

View file

@ -44,12 +44,12 @@ module.exports = React.createClass({
getInitialState: function() { getInitialState: function() {
return { return {
usersTyping: WhoIsTyping.usersTypingApartFromMe(this.props.room), usersTyping: WhoIsTyping.usersTypingApartFromMe(this.props.room),
userTimers: {},
// a map with userid => Timer to delay // a map with userid => Timer to delay
// hiding the "x is typing" message for a // hiding the "x is typing" message for a
// user so hiding it can coincide // user so hiding it can coincide
// with the sent message by the other side // with the sent message by the other side
// resulting in less timeline jumpiness // resulting in less timeline jumpiness
delayedStopTypingTimers: {},
}; };
}, },
@ -74,7 +74,7 @@ module.exports = React.createClass({
client.removeListener("RoomMember.typing", this.onRoomMemberTyping); client.removeListener("RoomMember.typing", this.onRoomMemberTyping);
client.removeListener("Room.timeline", this.onRoomTimeline); client.removeListener("Room.timeline", this.onRoomTimeline);
} }
Object.values(this.state.userTimers).forEach((t) => t.abort()); Object.values(this.state.delayedStopTypingTimers).forEach((t) => t.abort());
}, },
onRoomTimeline: function(event, room) { onRoomTimeline: function(event, room) {
@ -91,12 +91,12 @@ module.exports = React.createClass({
onRoomMemberTyping: function(ev, member) { onRoomMemberTyping: function(ev, member) {
const usersTyping = WhoIsTyping.usersTypingApartFromMeAndIgnored(this.props.room); const usersTyping = WhoIsTyping.usersTypingApartFromMeAndIgnored(this.props.room);
this.setState({ this.setState({
userTimers: this._updateUserTimers(usersTyping), delayedStopTypingTimers: this._updateDelayedStopTypingTimers(usersTyping),
usersTyping, usersTyping,
}); });
}, },
_updateUserTimers(usersTyping) { _updateDelayedStopTypingTimers(usersTyping) {
const usersThatStoppedTyping = this.state.usersTyping.filter((a) => { const usersThatStoppedTyping = this.state.usersTyping.filter((a) => {
return !usersTyping.some((b) => a.userId === b.userId); return !usersTyping.some((b) => a.userId === b.userId);
}); });
@ -105,35 +105,35 @@ module.exports = React.createClass({
}); });
// abort all the timers for the users that started typing again // abort all the timers for the users that started typing again
usersThatStartedTyping.forEach((m) => { usersThatStartedTyping.forEach((m) => {
const timer = this.state.userTimers[m.userId]; const timer = this.state.delayedStopTypingTimers[m.userId];
timer && timer.abort(); timer && timer.abort();
}); });
// prepare new userTimers object to update state with // prepare new delayedStopTypingTimers object to update state with
let userTimers = Object.assign({}, this.state.userTimers); let delayedStopTypingTimers = Object.assign({}, this.state.delayedStopTypingTimers);
// remove members that started typing again // remove members that started typing again
userTimers = usersThatStartedTyping.reduce((userTimers, m) => { delayedStopTypingTimers = usersThatStartedTyping.reduce((delayedStopTypingTimers, m) => {
delete userTimers[m.userId]; delete delayedStopTypingTimers[m.userId];
return userTimers; return delayedStopTypingTimers;
}, userTimers); }, delayedStopTypingTimers);
// start timer for members that stopped typing // start timer for members that stopped typing
userTimers = usersThatStoppedTyping.reduce((userTimers, m) => { delayedStopTypingTimers = usersThatStoppedTyping.reduce((delayedStopTypingTimers, m) => {
if (!userTimers[m.userId]) { if (!delayedStopTypingTimers[m.userId]) {
const timer = new Timer(5000); const timer = new Timer(5000);
userTimers[m.userId] = timer; delayedStopTypingTimers[m.userId] = timer;
timer.start(); timer.start();
timer.finished().then( timer.finished().then(
() => this._removeUserTimer(m.userId), //on elapsed () => this._removeUserTimer(m.userId), //on elapsed
() => {/* aborted */}, () => {/* aborted */},
); );
} }
return userTimers; return delayedStopTypingTimers;
}, userTimers); }, delayedStopTypingTimers);
return userTimers; return delayedStopTypingTimers;
}, },
_abortUserTimer: function(userId) { _abortUserTimer: function(userId) {
const timer = this.state.userTimers[userId]; const timer = this.state.delayedStopTypingTimers[userId];
if (timer) { if (timer) {
timer.abort(); timer.abort();
this._removeUserTimer(userId); this._removeUserTimer(userId);
@ -141,11 +141,11 @@ module.exports = React.createClass({
}, },
_removeUserTimer: function(userId) { _removeUserTimer: function(userId) {
const timer = this.state.userTimers[userId]; const timer = this.state.delayedStopTypingTimers[userId];
if (timer) { if (timer) {
const userTimers = Object.assign({}, this.state.userTimers); const delayedStopTypingTimers = Object.assign({}, this.state.delayedStopTypingTimers);
delete userTimers[userId]; delete delayedStopTypingTimers[userId];
this.setState({userTimers}); this.setState({delayedStopTypingTimers});
} }
}, },
@ -181,7 +181,7 @@ module.exports = React.createClass({
render: function() { render: function() {
let usersTyping = this.state.usersTyping; let usersTyping = this.state.usersTyping;
const stoppedUsersOnTimer = Object.keys(this.state.userTimers) const stoppedUsersOnTimer = Object.keys(this.state.delayedStopTypingTimers)
.map((userId) => this.props.room.getMember(userId)); .map((userId) => this.props.room.getMember(userId));
// append the users that have been reported not typing anymore // append the users that have been reported not typing anymore
// but have a timeout timer running so they can disappear // but have a timeout timer running so they can disappear