Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit cd089a3

Browse files
committed
Track the user's own typing state external to the composer
Fixes element-hq/element-web#9986 There's a few reasons for pushing this out to its own place: * In future, we might want to move WhoIsTyping here. * We have multiple composers now, and although they don't send typing notifications, they could (see https://github.com/vector-im/riot-web/issues/10188) * In future we may have status for where/what the user is typing (https://github.com/matrix-org/matrix-doc/issues/437) * The composer is complicated enough - it doesn't need to dedupe typing states too. Note: This makes use of the principles introduced in element-hq/element-web#8923 and element-hq/element-web#9090
1 parent 15d286e commit cd089a3

File tree

3 files changed

+86
-14
lines changed

3 files changed

+86
-14
lines changed

src/Lifecycle.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import PlatformPeg from "./PlatformPeg";
3434
import { sendLoginRequest } from "./Login";
3535
import * as StorageManager from './utils/StorageManager';
3636
import SettingsStore from "./settings/SettingsStore";
37+
import TypingStore from "./stores/TypingStore";
3738

3839
/**
3940
* Called at startup, to attempt to build a logged-in Matrix session. It tries
@@ -505,6 +506,7 @@ async function startMatrixClient() {
505506

506507
Notifier.start();
507508
UserActivity.sharedInstance().start();
509+
TypingStore.sharedInstance().reset(); // just in case
508510
if (!SettingsStore.getValue("lowBandwidth")) {
509511
Presence.start();
510512
}
@@ -553,6 +555,7 @@ function _clearStorage() {
553555
export function stopMatrixClient() {
554556
Notifier.stop();
555557
UserActivity.sharedInstance().stop();
558+
TypingStore.sharedInstance().reset();
556559
Presence.stop();
557560
ActiveWidgetStore.stop();
558561
if (DMRoomMap.shared()) DMRoomMap.shared().stop();

src/components/views/rooms/MessageComposerInput.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
Copyright 2015, 2016 OpenMarket Ltd
33
Copyright 2017, 2018 New Vector Ltd
4+
Copyright 2019 The Matrix.org Foundation C.I.C.
45
56
Licensed under the Apache License, Version 2.0 (the "License");
67
you may not use this file except in compliance with the License.
@@ -61,10 +62,11 @@ import {ContentHelpers} from 'matrix-js-sdk';
6162
import AccessibleButton from '../elements/AccessibleButton';
6263
import {findEditableEvent} from '../../../utils/EventUtils';
6364
import ComposerHistoryManager from "../../../ComposerHistoryManager";
65+
import TypingStore, {TYPING_SERVER_TIMEOUT} from "../../../stores/TypingStore";
6466

6567
const REGEX_EMOTICON_WHITESPACE = new RegExp('(?:^|\\s)(' + EMOTICON_REGEX.source + ')\\s$');
6668

67-
const TYPING_USER_TIMEOUT = 10000; const TYPING_SERVER_TIMEOUT = 30000;
69+
const TYPING_USER_TIMEOUT = 10000;
6870

6971
// the Slate node type to default to for unstyled text
7072
const DEFAULT_NODE = 'paragraph';
@@ -477,19 +479,7 @@ export default class MessageComposerInput extends React.Component {
477479
}
478480

479481
sendTyping(isTyping) {
480-
if (!SettingsStore.getValue('sendTypingNotifications')) return;
481-
if (SettingsStore.getValue('lowBandwidth')) return;
482-
MatrixClientPeg.get().sendTyping(
483-
this.props.room.roomId,
484-
this.isTyping, TYPING_SERVER_TIMEOUT,
485-
).done();
486-
}
487-
488-
refreshTyping() {
489-
if (this.typingTimeout) {
490-
clearTimeout(this.typingTimeout);
491-
this.typingTimeout = null;
492-
}
482+
TypingStore.sharedInstance().setSelfTyping(this.props.room.roomId, isTyping);
493483
}
494484

495485
onChange = (change: Change, originalEditorState?: Value) => {

src/stores/TypingStore.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright 2019 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import MatrixClientPeg from "../MatrixClientPeg";
18+
import SettingsStore from "../settings/SettingsStore";
19+
20+
export const TYPING_SERVER_TIMEOUT = 30000;
21+
22+
/**
23+
* Tracks typing state for users.
24+
*/
25+
export default class TypingStore {
26+
constructor() {
27+
this.reset();
28+
}
29+
30+
static sharedInstance(): TypingStore {
31+
if (global.mxTypingStore === undefined) {
32+
global.mxTypingStore = new TypingStore();
33+
}
34+
return global.mxTypingStore;
35+
}
36+
37+
/**
38+
* Clears all cached typing states. Intended to be called when the
39+
* MatrixClientPeg client changes.
40+
*/
41+
reset() {
42+
this._typingStates = {}; // roomId => { isTyping, expireMs }
43+
}
44+
45+
/**
46+
* Changes the typing status for the MatrixClientPeg user.
47+
* @param {string} roomId The room ID to set the typing state in.
48+
* @param {boolean} isTyping Whether the user is typing or not.
49+
*/
50+
setSelfTyping(roomId: string, isTyping: boolean): void {
51+
if (!SettingsStore.getValue('sendTypingNotifications')) return;
52+
if (SettingsStore.getValue('lowBandwidth')) return;
53+
54+
const currentTyping = this._typingStates[roomId];
55+
if ((!isTyping && !currentTyping) || (currentTyping && currentTyping.isTyping === isTyping)) {
56+
// No change in state, so don't do anything. We'll let the timer run its course.
57+
return;
58+
}
59+
60+
const now = new Date().getTime();
61+
this._typingStates[roomId] = {
62+
isTyping: isTyping,
63+
expireMs: now + TYPING_SERVER_TIMEOUT,
64+
};
65+
66+
if (isTyping) {
67+
setTimeout(() => {
68+
const currentTyping = this._typingStates[roomId];
69+
const now = new Date().getTime();
70+
71+
if (currentTyping && currentTyping.expireMs >= now) {
72+
currentTyping.isTyping = false;
73+
}
74+
}, TYPING_SERVER_TIMEOUT);
75+
}
76+
77+
MatrixClientPeg.get().sendTyping(roomId, isTyping, TYPING_SERVER_TIMEOUT);
78+
}
79+
}

0 commit comments

Comments
 (0)