From 8de149704e52c4b1872b32efb899bdd5fd0b6707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 20 Jan 2020 17:43:55 +0100 Subject: [PATCH] EventIndexPanel: Dynamically update the indexer stats. --- .../views/settings/EventIndexPanel.js | 25 +++++++++++++++++++ src/indexing/EventIndex.js | 23 ++++++++++++++--- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/components/views/settings/EventIndexPanel.js b/src/components/views/settings/EventIndexPanel.js index f8c61e092d..b777957a3a 100644 --- a/src/components/views/settings/EventIndexPanel.js +++ b/src/components/views/settings/EventIndexPanel.js @@ -45,6 +45,29 @@ export default class EventIndexPanel extends React.Component { }; } + async updateCurrentRoom(room) { + const eventIndex = EventIndexPeg.get(); + const stats = await eventIndex.getStats(); + let currentRoom = null; + + if (room) currentRoom = room.name; + + this.setState({ + eventIndexSize: stats.size, + roomCount: stats.roomCount, + eventCount: stats.eventCount, + currentRoom: currentRoom, + }); + } + + componentWillUnmount(): void { + const eventIndex = EventIndexPeg.get(); + + if (eventIndex !== null) { + eventIndex.removeListener("changedCheckpoint", this.updateCurrentRoom.bind(this)); + } + } + async componentWillMount(): void { let eventIndexSize = 0; let roomCount = 0; @@ -56,6 +79,8 @@ export default class EventIndexPanel extends React.Component { const eventIndex = EventIndexPeg.get(); if (eventIndex !== null) { + eventIndex.on("changedCheckpoint", this.updateCurrentRoom.bind(this)); + const stats = await eventIndex.getStats(); eventIndexSize = stats.size; roomCount = stats.roomCount; diff --git a/src/indexing/EventIndex.js b/src/indexing/EventIndex.js index d00a0530ba..5676636eed 100644 --- a/src/indexing/EventIndex.js +++ b/src/indexing/EventIndex.js @@ -19,6 +19,7 @@ import {MatrixClientPeg} from "../MatrixClientPeg"; import SettingsStore from '../settings/SettingsStore'; import {SettingLevel} from "../settings/SettingsStore"; import {sleep} from "../utils/promise"; +import {EventEmitter} from "events"; /* * Event indexing class that wraps the platform specific event indexing. @@ -35,6 +36,7 @@ export default class EventIndex { this._crawler = null; this._currentCheckpoint = null; this.liveEventsForIndex = new Set(); + this._eventEmitter = new EventEmitter(); } async init() { @@ -185,6 +187,10 @@ export default class EventIndex { indexManager.addEventToIndex(e, profile); } + emitNewCheckpoint() { + this._eventEmitter.emit("changedCheckpoint", this.currentRoom()); + } + async crawlerFunc() { let cancelled = false; @@ -214,7 +220,10 @@ export default class EventIndex { sleepTime = this._crawlerIdleTime; } - this._currentCheckpoint = null; + if (this._currentCheckpoint !== null) { + this._currentCheckpoint = null; + this.emitNewCheckpoint(); + } await sleep(sleepTime); @@ -234,6 +243,7 @@ export default class EventIndex { } this._currentCheckpoint = checkpoint; + this.emitNewCheckpoint(); idle = false; @@ -465,18 +475,23 @@ export default class EventIndex { */ currentRoom() { if (this._currentCheckpoint === null && this.crawlerCheckpoints.length === 0) { - console.log("EventIndex: No current nor any checkpoint"); return null; } const client = MatrixClientPeg.get(); if (this._currentCheckpoint !== null) { - console.log("EventIndex: Current checkpoint available"); return client.getRoom(this._currentCheckpoint.roomId); } else { - console.log("EventIndex: No current but have checkpoint available"); return client.getRoom(this.crawlerCheckpoints[0].roomId); } } + + on(eventName, callback) { + this._eventEmitter.on(eventName, callback); + } + + removeListener(eventName, callback) { + this._eventEmitter.removeListener(eventName, callback); + } }