@@ -15,15 +15,14 @@ See the License for the specific language governing permissions and
15
15
limitations under the License.
16
16
*/
17
17
18
- import { Store } from 'flux/utils' ;
19
- import { Room } from "matrix-js-sdk/src/models/room" ;
20
- import { MatrixClient } from "matrix-js-sdk/src/client" ;
18
+ import { MatrixClient } from "matrix-js-sdk/src/client" ;
21
19
import { ActionPayload , defaultDispatcher } from "../../dispatcher-types" ;
22
20
import SettingsStore from "../../settings/SettingsStore" ;
23
- import { OrderedDefaultTagIDs , DefaultTagID , TagID } from "./models" ;
21
+ import { DefaultTagID , OrderedDefaultTagIDs , TagID } from "./models" ;
24
22
import { IAlgorithm , ITagMap , ITagSortingMap , ListAlgorithm , SortAlgorithm } from "./algorithms/IAlgorithm" ;
25
23
import TagOrderStore from "../TagOrderStore" ;
26
24
import { getAlgorithmInstance } from "./algorithms" ;
25
+ import { AsyncStore } from "../AsyncStore" ;
27
26
28
27
interface IState {
29
28
tagsEnabled ?: boolean ;
@@ -32,8 +31,13 @@ interface IState {
32
31
preferredAlgorithm ?: ListAlgorithm ;
33
32
}
34
33
35
- class _RoomListStore extends Store < ActionPayload > {
36
- private state : IState = { } ;
34
+ /**
35
+ * The event/channel which is called when the room lists have been changed. Raised
36
+ * with one argument: the instance of the store.
37
+ */
38
+ export const LISTS_UPDATE_EVENT = "lists_update" ;
39
+
40
+ class _RoomListStore extends AsyncStore < ActionPayload > {
37
41
private matrixClient : MatrixClient ;
38
42
private initialListsGenerated = false ;
39
43
private enabled = false ;
@@ -49,7 +53,6 @@ class _RoomListStore extends Store<ActionPayload> {
49
53
super ( defaultDispatcher ) ;
50
54
51
55
this . checkEnabled ( ) ;
52
- this . reset ( ) ;
53
56
for ( const settingName of this . watchedSettings ) SettingsStore . monitorSetting ( settingName , null ) ;
54
57
}
55
58
@@ -66,41 +69,35 @@ class _RoomListStore extends Store<ActionPayload> {
66
69
}
67
70
}
68
71
69
- private reset ( ) : void {
70
- // We don't call setState() because it'll cause changes to emitted which could
71
- // crash the app during logout/signin/etc.
72
- this . state = { } ;
73
- }
74
-
75
- private readAndCacheSettingsFromStore ( ) {
72
+ private async readAndCacheSettingsFromStore ( ) {
76
73
const tagsEnabled = SettingsStore . isFeatureEnabled ( "feature_custom_tags" ) ;
77
74
const orderByImportance = SettingsStore . getValue ( "RoomList.orderByImportance" ) ;
78
75
const orderAlphabetically = SettingsStore . getValue ( "RoomList.orderAlphabetically" ) ;
79
- this . setState ( {
76
+ await this . updateState ( {
80
77
tagsEnabled,
81
78
preferredSort : orderAlphabetically ? SortAlgorithm . Alphabetic : SortAlgorithm . Recent ,
82
79
preferredAlgorithm : orderByImportance ? ListAlgorithm . Importance : ListAlgorithm . Natural ,
83
80
} ) ;
84
81
this . setAlgorithmClass ( ) ;
85
82
}
86
83
87
- protected __onDispatch ( payload : ActionPayload ) : void {
84
+ protected async onDispatch ( payload : ActionPayload ) {
88
85
if ( payload . action === 'MatrixActions.sync' ) {
89
86
// Filter out anything that isn't the first PREPARED sync.
90
87
if ( ! ( payload . prevState === 'PREPARED' && payload . state !== 'PREPARED' ) ) {
91
88
return ;
92
89
}
93
90
91
+ // TODO: Remove this once the RoomListStore becomes default
94
92
this . checkEnabled ( ) ;
95
93
if ( ! this . enabled ) return ;
96
94
97
95
this . matrixClient = payload . matrixClient ;
98
96
99
97
// Update any settings here, as some may have happened before we were logically ready.
100
- this . readAndCacheSettingsFromStore ( ) ;
101
-
102
- // noinspection JSIgnoredPromiseFromCall
103
- this . regenerateAllLists ( ) ;
98
+ console . log ( "Regenerating room lists: Startup" ) ;
99
+ await this . readAndCacheSettingsFromStore ( ) ;
100
+ await this . regenerateAllLists ( ) ;
104
101
}
105
102
106
103
// TODO: Remove this once the RoomListStore becomes default
@@ -109,7 +106,7 @@ class _RoomListStore extends Store<ActionPayload> {
109
106
if ( payload . action === 'on_client_not_viable' || payload . action === 'on_logged_out' ) {
110
107
// Reset state without causing updates as the client will have been destroyed
111
108
// and downstream code will throw NPE errors.
112
- this . reset ( ) ;
109
+ this . reset ( null , true ) ;
113
110
this . matrixClient = null ;
114
111
this . initialListsGenerated = false ; // we'll want to regenerate them
115
112
}
@@ -120,14 +117,15 @@ class _RoomListStore extends Store<ActionPayload> {
120
117
121
118
if ( payload . action === 'setting_updated' ) {
122
119
if ( this . watchedSettings . includes ( payload . settingName ) ) {
123
- this . readAndCacheSettingsFromStore ( ) ;
120
+ console . log ( "Regenerating room lists: Settings changed" ) ;
121
+ await this . readAndCacheSettingsFromStore ( ) ;
124
122
125
- // noinspection JSIgnoredPromiseFromCall
126
- this . regenerateAllLists ( ) ; // regenerate the lists now
123
+ await this . regenerateAllLists ( ) ; // regenerate the lists now
127
124
}
128
125
} else if ( payload . action === 'MatrixActions.Room.receipt' ) {
129
126
// First see if the receipt event is for our own user. If it was, trigger
130
127
// a room update (we probably read the room on a different device).
128
+ // noinspection JSObjectNullOrUndefined - this.matrixClient can't be null by this point in the lifecycle
131
129
const myUserId = this . matrixClient . getUserId ( ) ;
132
130
for ( const eventId of Object . keys ( payload . event . getContent ( ) ) ) {
133
131
const receiptUsers = Object . keys ( payload . event . getContent ( ) [ eventId ] [ 'm.read' ] || { } ) ;
@@ -167,19 +165,18 @@ class _RoomListStore extends Store<ActionPayload> {
167
165
}
168
166
}
169
167
170
- private setState ( newState : IState ) {
168
+ protected async updateState ( newState : IState ) {
171
169
if ( ! this . enabled ) return ;
172
170
173
- this . state = Object . assign ( this . state , newState ) ;
174
- this . __emitChange ( ) ;
171
+ await super . updateState ( newState ) ;
175
172
}
176
173
177
174
private setAlgorithmClass ( ) {
178
175
this . algorithm = getAlgorithmInstance ( this . state . preferredAlgorithm ) ;
179
176
}
180
177
181
178
private async regenerateAllLists ( ) {
182
- console . log ( "REGEN ") ;
179
+ console . warn ( "Regenerating all room lists ") ;
183
180
const tags : ITagSortingMap = { } ;
184
181
for ( const tagId of OrderedDefaultTagIDs ) {
185
182
tags [ tagId ] = this . getSortAlgorithmFor ( tagId ) ;
@@ -196,7 +193,7 @@ class _RoomListStore extends Store<ActionPayload> {
196
193
197
194
this . initialListsGenerated = true ;
198
195
199
- // TODO: How do we asynchronously update the store's state? or do we just give in and make it all sync?
196
+ this . emit ( LISTS_UPDATE_EVENT , this ) ;
200
197
}
201
198
}
202
199
0 commit comments