You've already forked matrix-react-sdk
mirror of
https://github.com/matrix-org/matrix-react-sdk.git
synced 2025-07-28 15:22:05 +03:00
also forward actions from room dispatcher to global one
avoiding replay if the action would be forwarded back to the same room dispatcher also some fixing & renaming in OpenRoomsStore
This commit is contained in:
@ -28,7 +28,7 @@ import OpenRoomsStore from './stores/OpenRoomsStore';
|
|||||||
class ActiveRoomObserver {
|
class ActiveRoomObserver {
|
||||||
constructor() {
|
constructor() {
|
||||||
this._listeners = {};
|
this._listeners = {};
|
||||||
const roomStore = OpenRoomsStore.getCurrentRoomStore();
|
const roomStore = OpenRoomsStore.getActiveRoomStore();
|
||||||
this._activeRoomId = roomStore && roomStore.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.
|
||||||
@ -67,7 +67,7 @@ class ActiveRoomObserver {
|
|||||||
// 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();
|
const activeRoomStore = OpenRoomsStore.getActiveRoomStore();
|
||||||
// update our cache
|
// update our cache
|
||||||
this._activeRoomId = activeRoomStore && activeRoomStore.getRoomId();
|
this._activeRoomId = activeRoomStore && activeRoomStore.getRoomId();
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ export default class RoomGridView extends React.Component {
|
|||||||
if (this._unmounted) return;
|
if (this._unmounted) return;
|
||||||
this.setState({
|
this.setState({
|
||||||
roomStores: OpenRoomsStore.getRoomStores(),
|
roomStores: OpenRoomsStore.getRoomStores(),
|
||||||
currentRoomStore: OpenRoomsStore.getCurrentRoomStore(),
|
activeRoomStore: OpenRoomsStore.getActiveRoomStore(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ export default class RoomGridView extends React.Component {
|
|||||||
return (<main className="mx_GroupGridView">
|
return (<main className="mx_GroupGridView">
|
||||||
{ roomStores.map((roomStore, i) => {
|
{ roomStores.map((roomStore, i) => {
|
||||||
if (roomStore) {
|
if (roomStore) {
|
||||||
const isActive = roomStore === this.state.currentRoomStore;
|
const isActive = roomStore === this.state.activeRoomStore;
|
||||||
const tileClasses = classNames({
|
const tileClasses = classNames({
|
||||||
"mx_GroupGridView_tile": true,
|
"mx_GroupGridView_tile": true,
|
||||||
"mx_GroupGridView_activeTile": isActive,
|
"mx_GroupGridView_activeTile": isActive,
|
||||||
|
@ -430,14 +430,14 @@ const LoggedInView = React.createClass({
|
|||||||
|
|
||||||
switch (this.props.page_type) {
|
switch (this.props.page_type) {
|
||||||
case PageTypes.RoomView:
|
case PageTypes.RoomView:
|
||||||
if (!OpenRoomsStore.getCurrentRoomStore()) {
|
if (!OpenRoomsStore.getActiveRoomStore()) {
|
||||||
console.warn(`LoggedInView: getCurrentRoomStore not set!`);
|
console.warn(`LoggedInView: getCurrentRoomStore not set!`);
|
||||||
}
|
}
|
||||||
else if (OpenRoomsStore.getCurrentRoomStore().getRoomId() !== this.props.currentRoomId) {
|
else if (OpenRoomsStore.getActiveRoomStore().getRoomId() !== this.props.currentRoomId) {
|
||||||
console.warn(`LoggedInView: room id in store not the same as in props: ${OpenRoomsStore.getCurrentRoomStore().getRoomId()} & ${this.props.currentRoomId}`);
|
console.warn(`LoggedInView: room id in store not the same as in props: ${OpenRoomsStore.getActiveRoomStore().getRoomId()} & ${this.props.currentRoomId}`);
|
||||||
}
|
}
|
||||||
page_element = <RoomView
|
page_element = <RoomView
|
||||||
roomViewStore={OpenRoomsStore.getCurrentRoomStore()}
|
roomViewStore={OpenRoomsStore.getActiveRoomStore()}
|
||||||
ref='roomView'
|
ref='roomView'
|
||||||
autoJoin={this.props.autoJoin}
|
autoJoin={this.props.autoJoin}
|
||||||
onRegistered={this.props.onRegistered}
|
onRegistered={this.props.onRegistered}
|
||||||
|
@ -53,14 +53,14 @@ class OpenRoomsStore extends Store {
|
|||||||
return this._state.rooms.map((r) => r.store);
|
return this._state.rooms.map((r) => r.store);
|
||||||
}
|
}
|
||||||
|
|
||||||
getCurrentRoomStore() {
|
getActiveRoomStore() {
|
||||||
const currentRoom = this._getCurrentRoom();
|
const openRoom = this._getActiveOpenRoom();
|
||||||
if (currentRoom) {
|
if (openRoom) {
|
||||||
return currentRoom.store;
|
return openRoom.store;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_getCurrentRoom() {
|
_getActiveOpenRoom() {
|
||||||
const index = this._state.currentIndex;
|
const index = this._state.currentIndex;
|
||||||
if (index !== null && index < this._state.rooms.length) {
|
if (index !== null && index < this._state.rooms.length) {
|
||||||
return this._state.rooms[index];
|
return this._state.rooms[index];
|
||||||
@ -80,31 +80,79 @@ class OpenRoomsStore extends Store {
|
|||||||
return this._state.rooms.findIndex((r) => matchesRoom(payload, r.store));
|
return this._state.rooms.findIndex((r) => matchesRoom(payload, r.store));
|
||||||
}
|
}
|
||||||
|
|
||||||
_cleanupRooms() {
|
_cleanupOpenRooms() {
|
||||||
const room = this._state.room;
|
|
||||||
this._state.rooms.forEach((room) => {
|
this._state.rooms.forEach((room) => {
|
||||||
|
room.dispatcher.unregister(room.dispatcherRef);
|
||||||
room.dispatcher.unregister(room.store.getDispatchToken());
|
room.dispatcher.unregister(room.store.getDispatchToken());
|
||||||
});
|
});
|
||||||
this._setState({
|
this._setState({
|
||||||
rooms: [],
|
rooms: [],
|
||||||
group_id: null,
|
group_id: null,
|
||||||
currentIndex: null
|
currentIndex: null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_createRoom() {
|
_createOpenRoom(room_id, room_alias) {
|
||||||
const dispatcher = new MatrixDispatcher();
|
const dispatcher = new MatrixDispatcher();
|
||||||
|
// forward all actions coming from the room dispatcher
|
||||||
|
// to the global one
|
||||||
|
const dispatcherRef = dispatcher.register((action) => {
|
||||||
|
action.grid_src_room_id = room_id;
|
||||||
|
action.grid_src_room_alias = room_alias;
|
||||||
|
this.getDispatcher().dispatch(action);
|
||||||
|
});
|
||||||
|
const openRoom = {
|
||||||
|
store: new RoomViewStore(dispatcher),
|
||||||
|
dispatcher,
|
||||||
|
dispatcherRef,
|
||||||
|
};
|
||||||
|
|
||||||
|
dispatcher.dispatch({
|
||||||
|
action: 'view_room',
|
||||||
|
room_id: room_id,
|
||||||
|
room_alias: room_alias,
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
return openRoom;
|
||||||
|
}
|
||||||
|
|
||||||
|
_setSingleOpenRoom(payload) {
|
||||||
this._setState({
|
this._setState({
|
||||||
rooms: [{
|
rooms: [this._createOpenRoom(payload.room_id, payload.room_alias)],
|
||||||
store: new RoomViewStore(dispatcher),
|
currentIndex: 0,
|
||||||
dispatcher,
|
});
|
||||||
}],
|
}
|
||||||
|
|
||||||
|
_setGroupOpenRooms(group_id) {
|
||||||
|
this._cleanupOpenRooms();
|
||||||
|
// TODO: register to GroupStore updates
|
||||||
|
const rooms = GroupStore.getGroupRooms(group_id);
|
||||||
|
const openRooms = rooms.map((room) => {
|
||||||
|
return this._createOpenRoom(room.roomId);
|
||||||
|
});
|
||||||
|
this._setState({
|
||||||
|
rooms: openRooms,
|
||||||
|
group_id: group_id,
|
||||||
currentIndex: 0,
|
currentIndex: 0,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_forwardAction(payload) {
|
_forwardAction(payload) {
|
||||||
const currentRoom = this._getCurrentRoom();
|
// don't forward an event to a room dispatcher
|
||||||
|
// if the event originated from that dispatcher, as this
|
||||||
|
// would cause the event to be observed twice in that
|
||||||
|
// dispatcher
|
||||||
|
if (payload.grid_src_room_id || payload.grid_src_room_alias) {
|
||||||
|
const srcPayload = {
|
||||||
|
room_id: payload.grid_src_room_id,
|
||||||
|
room_alias: payload.grid_src_room_alias,
|
||||||
|
};
|
||||||
|
const srcIndex = this._roomIndex(srcPayload);
|
||||||
|
if (srcIndex === this._state.currentIndex) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const currentRoom = this._getActiveOpenRoom();
|
||||||
if (currentRoom) {
|
if (currentRoom) {
|
||||||
currentRoom.dispatcher.dispatch(payload, true);
|
currentRoom.dispatcher.dispatch(payload, true);
|
||||||
}
|
}
|
||||||
@ -114,7 +162,7 @@ class OpenRoomsStore extends Store {
|
|||||||
try {
|
try {
|
||||||
const result = await MatrixClientPeg.get()
|
const result = await MatrixClientPeg.get()
|
||||||
.getRoomIdForAlias(payload.room_alias);
|
.getRoomIdForAlias(payload.room_alias);
|
||||||
dis.dispatch({
|
this.getDispatcher().dispatch({
|
||||||
action: 'view_room',
|
action: 'view_room',
|
||||||
room_id: result.room_id,
|
room_id: result.room_id,
|
||||||
event_id: payload.event_id,
|
event_id: payload.event_id,
|
||||||
@ -133,6 +181,36 @@ class OpenRoomsStore extends Store {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_viewRoom(payload) {
|
||||||
|
console.log("!!! OpenRoomsStore: view_room", payload);
|
||||||
|
if (!payload.room_id && payload.room_alias) {
|
||||||
|
this._resolveRoomAlias(payload);
|
||||||
|
}
|
||||||
|
const currentStore = this.getActiveRoomStore();
|
||||||
|
if (!matchesRoom(payload, currentStore)) {
|
||||||
|
if (this._hasRoom(payload)) {
|
||||||
|
const roomIndex = this._roomIndex(payload);
|
||||||
|
this._setState({currentIndex: roomIndex});
|
||||||
|
} else {
|
||||||
|
this._cleanupOpenRooms();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!this.getActiveRoomStore()) {
|
||||||
|
console.log("OpenRoomsStore: _setSingleOpenRoom");
|
||||||
|
this._setSingleOpenRoom(payload);
|
||||||
|
}
|
||||||
|
console.log("OpenRoomsStore: _forwardAction");
|
||||||
|
this._forwardAction(payload);
|
||||||
|
if (this._forwardingEvent) {
|
||||||
|
this.getDispatcher().dispatch({
|
||||||
|
action: 'send_event',
|
||||||
|
room_id: payload.room_id,
|
||||||
|
event: this._forwardingEvent,
|
||||||
|
});
|
||||||
|
this._forwardingEvent = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
__onDispatch(payload) {
|
__onDispatch(payload) {
|
||||||
switch (payload.action) {
|
switch (payload.action) {
|
||||||
// view_room:
|
// view_room:
|
||||||
@ -142,38 +220,12 @@ class OpenRoomsStore extends Store {
|
|||||||
// - event_offset: 100
|
// - event_offset: 100
|
||||||
// - highlighted: true
|
// - highlighted: true
|
||||||
case 'view_room':
|
case 'view_room':
|
||||||
console.log("!!! OpenRoomsStore: view_room", payload);
|
this._viewRoom(payload);
|
||||||
if (!payload.room_id && payload.room_alias) {
|
|
||||||
this._resolveRoomAlias(payload);
|
|
||||||
}
|
|
||||||
const currentStore = this.getCurrentRoomStore();
|
|
||||||
if (matchesRoom(payload, currentStore)) {
|
|
||||||
if (this._hasRoom(payload)) {
|
|
||||||
const roomIndex = this._roomIndex(payload);
|
|
||||||
this._setState({currentIndex: roomIndex});
|
|
||||||
} else {
|
|
||||||
this._cleanupRooms();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!this.getCurrentRoomStore()) {
|
|
||||||
console.log("OpenRoomsStore: _createRoom");
|
|
||||||
this._createRoom();
|
|
||||||
}
|
|
||||||
console.log("OpenRoomsStore: _forwardAction");
|
|
||||||
this._forwardAction(payload);
|
|
||||||
if (this._forwardingEvent) {
|
|
||||||
dis.dispatch({
|
|
||||||
action: 'send_event',
|
|
||||||
room_id: payload.room_id,
|
|
||||||
event: this._forwardingEvent,
|
|
||||||
});
|
|
||||||
this._forwardingEvent = null;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'view_my_groups':
|
case 'view_my_groups':
|
||||||
case 'view_group':
|
case 'view_group':
|
||||||
this._forwardAction(payload);
|
this._forwardAction(payload);
|
||||||
this._cleanupRooms();
|
this._cleanupOpenRooms();
|
||||||
break;
|
break;
|
||||||
case 'will_join':
|
case 'will_join':
|
||||||
case 'cancel_join':
|
case 'cancel_join':
|
||||||
@ -183,6 +235,7 @@ class OpenRoomsStore extends Store {
|
|||||||
case 'reply_to_event':
|
case 'reply_to_event':
|
||||||
case 'open_room_settings':
|
case 'open_room_settings':
|
||||||
case 'close_settings':
|
case 'close_settings':
|
||||||
|
case 'focus_composer':
|
||||||
this._forwardAction(payload);
|
this._forwardAction(payload);
|
||||||
break;
|
break;
|
||||||
case 'forward_event':
|
case 'forward_event':
|
||||||
@ -198,27 +251,7 @@ class OpenRoomsStore extends Store {
|
|||||||
break;
|
break;
|
||||||
case 'group_grid_view':
|
case 'group_grid_view':
|
||||||
if (payload.group_id !== this._state.group_id) {
|
if (payload.group_id !== this._state.group_id) {
|
||||||
this._cleanupRooms();
|
this._setGroupOpenRooms(payload.group_id);
|
||||||
// TODO: register to GroupStore updates
|
|
||||||
const rooms = GroupStore.getGroupRooms(payload.group_id);
|
|
||||||
const roomStores = rooms.map((room) => {
|
|
||||||
const dispatcher = new MatrixDispatcher();
|
|
||||||
const store = new RoomViewStore(dispatcher);
|
|
||||||
// set room id of store
|
|
||||||
dispatcher.dispatch({
|
|
||||||
action: 'view_room',
|
|
||||||
room_id: room.roomId
|
|
||||||
}, true);
|
|
||||||
return {
|
|
||||||
store,
|
|
||||||
dispatcher,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
this._setState({
|
|
||||||
rooms: roomStores,
|
|
||||||
group_id: payload.group_id,
|
|
||||||
currentIndex: 0,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user