Implement a store for RoomView
This allows for a truely flux-y way of storing the currently viewed room, making some callbacks (like onRoomIdResolved) redundant and making sure that the currently viewed room (ID) is only stored in one place as opposed to the previous many places. This was required for the `join_room` action which can be dispatched to join the currently viewed room. Another change was to introduce `LifeCycleStore` which is a start at encorporating state related to the lifecycle of the app into a flux store. Currently it only contains an action which will be dispatched when the sync state has become PREPARED. This was necessary to do a deferred dispatch of `join_room` following the registration of a PWLU (PassWord-Less User). The following actions are introduced: - RoomViewStore: - `view_room`: dispatch to change the currently viewed room ID - `join_room`: dispatch to join the currently viewed room - LifecycleStore: - `do_after_sync_prepared`: dispatch to store an action which will be dispatched when `sync_state` is dispatched with `state = 'PREPARED'` - MatrixChat: - `sync_state`: dispatched when the sync state changes. Ideally there'd be a SyncStateStore that emitted an `update` upon receiving this, but for now the `LifecycleStore` will listen for `sync_state` directly.
This commit is contained in:
parent
b0a824c941
commit
298c5e4df3
11 changed files with 399 additions and 241 deletions
145
src/stores/RoomViewStore.js
Normal file
145
src/stores/RoomViewStore.js
Normal file
|
@ -0,0 +1,145 @@
|
|||
/*
|
||||
Copyright 2017 Vector Creations 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 dis from '../dispatcher';
|
||||
import {Store} from 'flux/utils';
|
||||
import MatrixClientPeg from '../MatrixClientPeg';
|
||||
|
||||
const INITIAL_STATE = {
|
||||
// Whether we're joining the currently viewed room
|
||||
joining: false,
|
||||
// Any error occurred during joining
|
||||
joinError: null,
|
||||
// The room ID of the room
|
||||
roomId: null,
|
||||
// The room alias of the room (or null if not originally specified in view_room)
|
||||
roomAlias: null,
|
||||
// Whether the current room is loading
|
||||
roomLoading: false,
|
||||
// Any error that has occurred during loading
|
||||
roomLoadError: null,
|
||||
};
|
||||
|
||||
/**
|
||||
* A class for storing application state for RoomView. This is the RoomView's interface
|
||||
* with a subset of the js-sdk.
|
||||
* ```
|
||||
*/
|
||||
class RoomViewStore extends Store {
|
||||
constructor() {
|
||||
super(dis);
|
||||
|
||||
// Initialise state
|
||||
this._state = INITIAL_STATE;
|
||||
}
|
||||
|
||||
_setState(newState) {
|
||||
this._state = Object.assign(this._state, newState);
|
||||
this.__emitChange();
|
||||
}
|
||||
|
||||
__onDispatch(payload) {
|
||||
switch (payload.action) {
|
||||
// view_room:
|
||||
// - room_alias: '#somealias:matrix.org'
|
||||
// - room_id: '!roomid123:matrix.org'
|
||||
case 'view_room':
|
||||
this._viewRoom(payload);
|
||||
break;
|
||||
|
||||
// join_room:
|
||||
// - opts: options for joinRoom
|
||||
case 'join_room':
|
||||
this._joinRoom(payload);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_viewRoom(payload) {
|
||||
const address = payload.room_alias || payload.room_id;
|
||||
if (address[0] == '#') {
|
||||
this._setState({
|
||||
roomLoading: true,
|
||||
});
|
||||
MatrixClientPeg.get().getRoomIdForAlias(address).then(
|
||||
(result) => {
|
||||
this._setState({
|
||||
roomId: result.room_id,
|
||||
roomAlias: address,
|
||||
roomLoading: false,
|
||||
roomLoadError: null,
|
||||
});
|
||||
}, (err) => {
|
||||
console.error(err);
|
||||
this._setState({
|
||||
roomLoading: false,
|
||||
roomLoadError: err,
|
||||
});
|
||||
});
|
||||
} else {
|
||||
this._setState({
|
||||
roomId: address,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
_joinRoom(payload) {
|
||||
this._setState({
|
||||
joining: true,
|
||||
});
|
||||
MatrixClientPeg.get().joinRoom(this._state.roomId, payload.opts).then(
|
||||
() => {
|
||||
this._setState({
|
||||
joining: false,
|
||||
});
|
||||
}, (err) => {
|
||||
this._setState({
|
||||
joining: false,
|
||||
joinError: err,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
reset() {
|
||||
this._state = Object.assign({}, INITIAL_STATE);
|
||||
}
|
||||
|
||||
getRoomId() {
|
||||
return this._state.roomId;
|
||||
}
|
||||
|
||||
getRoomAlias() {
|
||||
return this._state.roomAlias;
|
||||
}
|
||||
|
||||
isRoomLoading() {
|
||||
return this._state.roomLoading;
|
||||
}
|
||||
|
||||
isJoining() {
|
||||
return this._state.joining;
|
||||
}
|
||||
|
||||
getJoinError() {
|
||||
return this._state.joinError;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let singletonRoomViewStore = null;
|
||||
if (!singletonRoomViewStore) {
|
||||
singletonRoomViewStore = new RoomViewStore();
|
||||
}
|
||||
module.exports = singletonRoomViewStore;
|
Loading…
Add table
Add a link
Reference in a new issue