cram OpenRoomsStore between RoomViewStore and dispatcher
the idea is that it will keep a RoomViewStore for every room on the screen, and also keep track of which one is the current one. For now, it just replicates the existing functionality of having just 1 room on the screen. Since the RoomViewStore just has access to a local dispatcher and not the global anymore, all dispatching of actions needs to be moved to the OpenRoomsStore, so room alias resolving, event forwarding, ... is moved there.pull/21833/head
parent
7bd6bb6eb6
commit
869c81eb90
|
@ -0,0 +1,166 @@
|
||||||
|
/*
|
||||||
|
Copyright 2018 New Vector Ltd
|
||||||
|
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
import MatrixDispatcher from '../matrix-dispatcher';
|
||||||
|
import dis from '../dispatcher';
|
||||||
|
import {RoomViewStore} from './RoomViewStore';
|
||||||
|
import {Store} from 'flux/utils';
|
||||||
|
import MatrixClientPeg from '../MatrixClientPeg';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class for keeping track of the RoomViewStores of the rooms shown on the screen.
|
||||||
|
* Routes the dispatcher actions to the store of currently active room.
|
||||||
|
*/
|
||||||
|
class OpenRoomsStore extends Store {
|
||||||
|
constructor() {
|
||||||
|
super(dis);
|
||||||
|
|
||||||
|
// Initialise state
|
||||||
|
this._state = {
|
||||||
|
room: {
|
||||||
|
store: null,
|
||||||
|
dispatcher: null
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
this._forwardingEvent = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
getRoomStore() {
|
||||||
|
return this._state.room.store;
|
||||||
|
}
|
||||||
|
|
||||||
|
getCurrentRoomStore() {
|
||||||
|
return this.getRoomStore(); // just one room for now
|
||||||
|
}
|
||||||
|
|
||||||
|
_setState(newState) {
|
||||||
|
this._state = Object.assign(this._state, newState);
|
||||||
|
this.__emitChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
_cleanupRoom() {
|
||||||
|
const room = this._state.room;
|
||||||
|
room.dispatcher.unregister(room.store.getDispatchToken());
|
||||||
|
this._setState({
|
||||||
|
room: {
|
||||||
|
store: null,
|
||||||
|
dispatcher: null
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_createRoom() {
|
||||||
|
const dispatcher = new MatrixDispatcher();
|
||||||
|
this._setState({
|
||||||
|
room: {
|
||||||
|
store: new RoomViewStore(dispatcher),
|
||||||
|
dispatcher,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_forwardAction(payload) {
|
||||||
|
if (this._state.room.dispatcher) {
|
||||||
|
this._state.room.dispatcher.dispatch(payload, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async _resolveRoomAlias(payload) {
|
||||||
|
try {
|
||||||
|
const result = await MatrixClientPeg.get()
|
||||||
|
.getRoomIdForAlias(payload.room_alias);
|
||||||
|
dis.dispatch({
|
||||||
|
action: 'view_room',
|
||||||
|
room_id: result.room_id,
|
||||||
|
event_id: payload.event_id,
|
||||||
|
highlighted: payload.highlighted,
|
||||||
|
room_alias: payload.room_alias,
|
||||||
|
auto_join: payload.auto_join,
|
||||||
|
oob_data: payload.oob_data,
|
||||||
|
});
|
||||||
|
} catch(err) {
|
||||||
|
this._forwardAction({
|
||||||
|
action: 'view_room_error',
|
||||||
|
room_id: null,
|
||||||
|
room_alias: payload.room_alias,
|
||||||
|
err: err,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__onDispatch(payload) {
|
||||||
|
switch (payload.action) {
|
||||||
|
// view_room:
|
||||||
|
// - room_alias: '#somealias:matrix.org'
|
||||||
|
// - room_id: '!roomid123:matrix.org'
|
||||||
|
// - event_id: '$213456782:matrix.org'
|
||||||
|
// - event_offset: 100
|
||||||
|
// - highlighted: true
|
||||||
|
case 'view_room':
|
||||||
|
console.log("!!! OpenRoomsStore: view_room", payload);
|
||||||
|
if (!payload.room_id && payload.room_alias) {
|
||||||
|
this._resolveRoomAlias(payload);
|
||||||
|
}
|
||||||
|
const currentStore = this.getCurrentRoomStore();
|
||||||
|
if (currentStore &&
|
||||||
|
(!payload.room_alias || payload.room_alias !== currentStore.getRoomAlias()) &&
|
||||||
|
(!currentStore.getRoomId() || payload.room_id !== currentStore.getRoomId())
|
||||||
|
) {
|
||||||
|
console.log("OpenRoomsStore: _cleanupRoom");
|
||||||
|
this._cleanupRoom();
|
||||||
|
}
|
||||||
|
if (!this._state.room.store) {
|
||||||
|
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;
|
||||||
|
case 'view_my_groups':
|
||||||
|
case 'view_group':
|
||||||
|
this._forwardAction(payload);
|
||||||
|
this._cleanupRoom();
|
||||||
|
break;
|
||||||
|
case 'will_join':
|
||||||
|
case 'cancel_join':
|
||||||
|
case 'join_room':
|
||||||
|
case 'join_room_error':
|
||||||
|
case 'on_logged_out':
|
||||||
|
case 'reply_to_event':
|
||||||
|
case 'open_room_settings':
|
||||||
|
case 'close_settings':
|
||||||
|
this._forwardAction(payload);
|
||||||
|
break;
|
||||||
|
case 'forward_event':
|
||||||
|
this._forwardingEvent = payload.event;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let singletonOpenRoomsStore = null;
|
||||||
|
if (!singletonOpenRoomsStore) {
|
||||||
|
singletonOpenRoomsStore = new OpenRoomsStore();
|
||||||
|
}
|
||||||
|
module.exports = singletonOpenRoomsStore;
|
|
@ -53,8 +53,8 @@ const INITIAL_STATE = {
|
||||||
* with a subset of the js-sdk.
|
* with a subset of the js-sdk.
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
class RoomViewStore extends Store {
|
export class RoomViewStore extends Store {
|
||||||
constructor() {
|
constructor(dis) {
|
||||||
super(dis);
|
super(dis);
|
||||||
|
|
||||||
// Initialise state
|
// Initialise state
|
||||||
|
@ -85,6 +85,8 @@ class RoomViewStore extends Store {
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case 'view_room_error':
|
case 'view_room_error':
|
||||||
|
// should not go over dispatcher anymore
|
||||||
|
// but be internal to RoomViewStore
|
||||||
this._viewRoomError(payload);
|
this._viewRoomError(payload);
|
||||||
break;
|
break;
|
||||||
case 'will_join':
|
case 'will_join':
|
||||||
|
@ -150,22 +152,11 @@ class RoomViewStore extends Store {
|
||||||
// pull the user out of Room Settings
|
// pull the user out of Room Settings
|
||||||
isEditingSettings: false,
|
isEditingSettings: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this._state.forwardingEvent) {
|
|
||||||
dis.dispatch({
|
|
||||||
action: 'send_event',
|
|
||||||
room_id: newState.roomId,
|
|
||||||
event: this._state.forwardingEvent,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this._setState(newState);
|
this._setState(newState);
|
||||||
|
|
||||||
if (payload.auto_join) {
|
if (payload.auto_join) {
|
||||||
this._joinRoom(payload);
|
this._joinRoom(payload);
|
||||||
}
|
}
|
||||||
} else if (payload.room_alias) {
|
} else if (payload.room_alias) {
|
||||||
// Resolve the alias and then do a second dispatch with the room ID acquired
|
|
||||||
this._setState({
|
this._setState({
|
||||||
roomId: null,
|
roomId: null,
|
||||||
initialEventId: null,
|
initialEventId: null,
|
||||||
|
@ -175,25 +166,6 @@ class RoomViewStore extends Store {
|
||||||
roomLoading: true,
|
roomLoading: true,
|
||||||
roomLoadError: null,
|
roomLoadError: null,
|
||||||
});
|
});
|
||||||
MatrixClientPeg.get().getRoomIdForAlias(payload.room_alias).done(
|
|
||||||
(result) => {
|
|
||||||
dis.dispatch({
|
|
||||||
action: 'view_room',
|
|
||||||
room_id: result.room_id,
|
|
||||||
event_id: payload.event_id,
|
|
||||||
highlighted: payload.highlighted,
|
|
||||||
room_alias: payload.room_alias,
|
|
||||||
auto_join: payload.auto_join,
|
|
||||||
oob_data: payload.oob_data,
|
|
||||||
});
|
|
||||||
}, (err) => {
|
|
||||||
dis.dispatch({
|
|
||||||
action: 'view_room_error',
|
|
||||||
room_id: null,
|
|
||||||
room_alias: payload.room_alias,
|
|
||||||
err: err,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -330,8 +302,7 @@ class RoomViewStore extends Store {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let singletonRoomViewStore = null;
|
const MatrixDispatcher = require("../matrix-dispatcher");
|
||||||
if (!singletonRoomViewStore) {
|
const blubber = new RoomViewStore(new MatrixDispatcher());
|
||||||
singletonRoomViewStore = new RoomViewStore();
|
|
||||||
}
|
export default blubber;
|
||||||
module.exports = singletonRoomViewStore;
|
|
||||||
|
|
Loading…
Reference in New Issue