track active room with OpenRoomsStore

This commit is contained in:
Bruno Windels 2018-11-07 13:25:42 +01:00
parent df8539d6bc
commit 43efa29ef8
2 changed files with 10 additions and 9 deletions

View file

@ -14,10 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import RoomViewStore from './stores/RoomViewStore'; import OpenRoomsStore from './stores/OpenRoomsStore';
/** /**
* Consumes changes from the RoomViewStore and notifies specific things * Consumes changes from the OpenRoomsStore and notifies specific things
* about when the active room changes. Unlike listening for RoomViewStore * about when the active room changes. Unlike listening for RoomViewStore
* changes, you can subscribe to only changes relevant to a particular * changes, you can subscribe to only changes relevant to a particular
* room. * room.
@ -28,11 +28,11 @@ import RoomViewStore from './stores/RoomViewStore';
class ActiveRoomObserver { class ActiveRoomObserver {
constructor() { constructor() {
this._listeners = {}; this._listeners = {};
const roomStore = OpenRoomsStore.getCurrentRoomStore();
this._activeRoomId = RoomViewStore.getRoomId(); this._activeRoomId = roomStore && roomStore.getRoomId();
// TODO: We could self-destruct when the last listener goes away, or at least // TODO: We could self-destruct when the last listener goes away, or at least
// stop listening. // stop listening.
this._roomStoreToken = RoomViewStore.addListener(this._onRoomViewStoreUpdate.bind(this)); this._roomStoreToken = OpenRoomsStore.addListener(this._onOpenRoomsStoreUpdate.bind(this));
} }
addListener(roomId, listener) { addListener(roomId, listener) {
@ -59,12 +59,13 @@ class ActiveRoomObserver {
} }
} }
_onRoomViewStoreUpdate() { _onOpenRoomsStoreUpdate() {
// emit for the old room ID // emit for the old room ID
if (this._activeRoomId) this._emit(this._activeRoomId); if (this._activeRoomId) this._emit(this._activeRoomId);
const activeRoomStore = OpenRoomsStore.getCurrentRoomStore();
// update our cache // update our cache
this._activeRoomId = RoomViewStore.getRoomId(); this._activeRoomId = activeRoomStore && activeRoomStore.getRoomId();
// and emit for the new one // and emit for the new one
if (this._activeRoomId) this._emit(this._activeRoomId); if (this._activeRoomId) this._emit(this._activeRoomId);

View file

@ -116,9 +116,9 @@ module.exports = React.createClass({
} }
}, },
_onActiveRoomChange: function() { _onActiveRoomChange: function(activeRoomId) {
this.setState({ this.setState({
selected: this.props.room.roomId === RoomViewStore.getRoomId(), selected: this.props.room.roomId === activeRoomId,
}); });
}, },