2019-06-27 06:36:55 +02:00
|
|
|
/*
|
2021-04-06 13:26:50 +02:00
|
|
|
Copyright 2019, 2021 The Matrix.org Foundation C.I.C.
|
2019-06-27 06:36:55 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-29 14:11:58 +02:00
|
|
|
import { MatrixClientPeg } from "../MatrixClientPeg";
|
2019-06-27 06:36:55 +02:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2019-06-27 18:27:11 +02:00
|
|
|
import Timer from "../utils/Timer";
|
2019-06-27 06:36:55 +02:00
|
|
|
|
2019-06-27 18:35:44 +02:00
|
|
|
const TYPING_USER_TIMEOUT = 10000;
|
|
|
|
const TYPING_SERVER_TIMEOUT = 30000;
|
2019-06-27 06:36:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracks typing state for users.
|
|
|
|
*/
|
|
|
|
export default class TypingStore {
|
2021-04-26 15:02:53 +02:00
|
|
|
private typingStates: {
|
2021-04-06 13:26:50 +02:00
|
|
|
[roomId: string]: {
|
2021-07-02 00:23:03 +02:00
|
|
|
isTyping: boolean;
|
|
|
|
userTimer: Timer;
|
|
|
|
serverTimer: Timer;
|
|
|
|
};
|
2021-04-06 13:26:50 +02:00
|
|
|
};
|
|
|
|
|
2019-06-27 06:36:55 +02:00
|
|
|
constructor() {
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
static sharedInstance(): TypingStore {
|
2021-04-06 13:26:50 +02:00
|
|
|
if (window.mxTypingStore === undefined) {
|
|
|
|
window.mxTypingStore = new TypingStore();
|
2019-06-27 06:36:55 +02:00
|
|
|
}
|
2021-04-06 13:26:50 +02:00
|
|
|
return window.mxTypingStore;
|
2019-06-27 06:36:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears all cached typing states. Intended to be called when the
|
|
|
|
* MatrixClientPeg client changes.
|
|
|
|
*/
|
|
|
|
reset() {
|
2021-04-26 15:02:53 +02:00
|
|
|
this.typingStates = {
|
2019-06-27 18:35:44 +02:00
|
|
|
// "roomId": {
|
|
|
|
// isTyping: bool, // Whether the user is typing or not
|
|
|
|
// userTimer: Timer, // Local timeout for "user has stopped typing"
|
|
|
|
// serverTimer: Timer, // Maximum timeout for the typing state
|
|
|
|
// },
|
|
|
|
};
|
2019-06-27 06:36:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the typing status for the MatrixClientPeg user.
|
|
|
|
* @param {string} roomId The room ID to set the typing state in.
|
|
|
|
* @param {boolean} isTyping Whether the user is typing or not.
|
|
|
|
*/
|
|
|
|
setSelfTyping(roomId: string, isTyping: boolean): void {
|
|
|
|
if (!SettingsStore.getValue('sendTypingNotifications')) return;
|
|
|
|
if (SettingsStore.getValue('lowBandwidth')) return;
|
|
|
|
|
2021-04-26 15:02:53 +02:00
|
|
|
let currentTyping = this.typingStates[roomId];
|
2019-06-27 06:36:55 +02:00
|
|
|
if ((!isTyping && !currentTyping) || (currentTyping && currentTyping.isTyping === isTyping)) {
|
|
|
|
// No change in state, so don't do anything. We'll let the timer run its course.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:27:11 +02:00
|
|
|
if (!currentTyping) {
|
2021-04-26 15:02:53 +02:00
|
|
|
currentTyping = this.typingStates[roomId] = {
|
2019-06-27 18:27:11 +02:00
|
|
|
isTyping: isTyping,
|
2019-06-27 18:35:44 +02:00
|
|
|
serverTimer: new Timer(TYPING_SERVER_TIMEOUT),
|
|
|
|
userTimer: new Timer(TYPING_USER_TIMEOUT),
|
2019-06-27 18:27:11 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTyping.isTyping = isTyping;
|
2019-06-27 06:36:55 +02:00
|
|
|
|
|
|
|
if (isTyping) {
|
2019-06-28 20:29:03 +02:00
|
|
|
if (!currentTyping.serverTimer.isRunning()) {
|
|
|
|
currentTyping.serverTimer.restart().finished().then(() => {
|
2021-04-26 15:02:53 +02:00
|
|
|
const currentTyping = this.typingStates[roomId];
|
2019-06-28 20:29:03 +02:00
|
|
|
if (currentTyping) currentTyping.isTyping = false;
|
|
|
|
|
|
|
|
// The server will (should) time us out on typing, so we don't
|
|
|
|
// need to advertise a stop of typing.
|
|
|
|
});
|
|
|
|
} else currentTyping.serverTimer.restart();
|
|
|
|
|
|
|
|
if (!currentTyping.userTimer.isRunning()) {
|
|
|
|
currentTyping.userTimer.restart().finished().then(() => {
|
|
|
|
this.setSelfTyping(roomId, false);
|
|
|
|
});
|
|
|
|
} else currentTyping.userTimer.restart();
|
2019-06-27 06:36:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MatrixClientPeg.get().sendTyping(roomId, isTyping, TYPING_SERVER_TIMEOUT);
|
|
|
|
}
|
2019-06-27 06:40:08 +02:00
|
|
|
}
|