@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- import { ListIteratee , Many , sortBy , throttle } from "lodash" ;
17
+ import { ListIteratee , Many , sortBy } from "lodash" ;
18
18
import { EventType , RoomType } from "matrix-js-sdk/src/@types/event" ;
19
19
import { Room , RoomEvent } from "matrix-js-sdk/src/models/room" ;
20
20
import { MatrixEvent } from "matrix-js-sdk/src/models/event" ;
@@ -235,6 +235,8 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
235
235
return ;
236
236
}
237
237
238
+ window . localStorage . setItem ( ACTIVE_SPACE_LS_KEY , this . _activeSpace = space ) ; // Update & persist selected space
239
+
238
240
if ( contextSwitch ) {
239
241
// view last selected room from space
240
242
const roomId = window . localStorage . getItem ( getSpaceContextKey ( space ) ) ;
@@ -251,36 +253,33 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
251
253
room_id : roomId ,
252
254
context_switch : true ,
253
255
metricsTrigger : "WebSpaceContextSwitch" ,
254
- } , true ) ;
256
+ } ) ;
255
257
} else if ( cliSpace ) {
256
258
defaultDispatcher . dispatch < ViewRoomPayload > ( {
257
259
action : Action . ViewRoom ,
258
260
room_id : space ,
259
261
context_switch : true ,
260
262
metricsTrigger : "WebSpaceContextSwitch" ,
261
- } , true ) ;
263
+ } ) ;
262
264
} else {
263
265
defaultDispatcher . dispatch < ViewHomePagePayload > ( {
264
266
action : Action . ViewHomePage ,
265
267
context_switch : true ,
266
- } , true ) ;
268
+ } ) ;
267
269
}
268
270
}
269
271
270
- // We can set the space after context switching as the dispatch handler which stores the last viewed room
271
- // specifically no-ops on context_switch=true.
272
- this . _activeSpace = space ;
273
- // Emit after a synchronous dispatch for context switching to prevent racing with SpaceWatcher calling
274
- // Room::loadMembersIfNeeded which could (via onMemberUpdate) call upon switchSpaceIfNeeded causing the
275
- // space to wrongly bounce.
276
272
this . emit ( UPDATE_SELECTED_SPACE , this . activeSpace ) ;
277
273
this . emit ( UPDATE_SUGGESTED_ROOMS , this . _suggestedRooms = [ ] ) ;
278
274
279
- // persist space selected
280
- window . localStorage . setItem ( ACTIVE_SPACE_LS_KEY , space ) ;
281
-
282
275
if ( cliSpace ) {
283
276
this . loadSuggestedRooms ( cliSpace ) ;
277
+
278
+ // Load all members for the selected space and its subspaces,
279
+ // so we can correctly show DMs we have with members of this space.
280
+ SpaceStore . instance . traverseSpace ( space , roomId => {
281
+ this . matrixClient . getRoom ( roomId ) ?. loadMembersIfNeeded ( ) ;
282
+ } , false ) ;
284
283
}
285
284
}
286
285
@@ -683,7 +682,10 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
683
682
this . emit ( space . roomId ) ;
684
683
affectedParentSpaceIds . forEach ( spaceId => this . emit ( spaceId ) ) ;
685
684
686
- this . switchSpaceIfNeeded ( ) ;
685
+ if ( ! inSpace ) {
686
+ // switch space if the DM is no longer considered part of the space
687
+ this . switchSpaceIfNeeded ( ) ;
688
+ }
687
689
} ;
688
690
689
691
private onRoomsUpdate = ( ) => {
@@ -804,12 +806,12 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
804
806
this . updateNotificationStates ( notificationStatesToUpdate ) ;
805
807
} ;
806
808
807
- private switchSpaceIfNeeded = throttle ( ( ) => {
809
+ private switchSpaceIfNeeded = ( ) => {
808
810
const roomId = RoomViewStore . getRoomId ( ) ;
809
811
if ( ! this . isRoomInSpace ( this . activeSpace , roomId ) && ! this . matrixClient . getRoom ( roomId ) ?. isSpaceRoom ( ) ) {
810
812
this . switchToRelatedSpace ( roomId ) ;
811
813
}
812
- } , 100 , { leading : true , trailing : true } ) ;
814
+ } ;
813
815
814
816
private switchToRelatedSpace = ( roomId : string ) => {
815
817
if ( this . suggestedRooms . find ( r => r . room_id === roomId ) ) return ;
0 commit comments