Skip to content

Commit

Permalink
refactor: store offline users in user store
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious committed Feb 2, 2023
1 parent 7563012 commit 9ad8786
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
34 changes: 9 additions & 25 deletions packages/flat-pages/src/components/UsersButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,15 @@ export const UsersButton = observer<UsersButtonProps>(function UsersButton({ cla
const [open, setOpen] = useState(false);

const users = useComputed(() => {
const { onStageUserUUIDs } = classroom;
const { creator, speakingJoiners, handRaisingJoiners, otherJoiners } = classroom.users;
const users: User[] = [];
const visited = new Set<string>();
const addUser = (user: User): void => {
if (!visited.has(user.userUUID)) {
users.push(user);
visited.add(user.userUUID);
}
};
for (const uuid of onStageUserUUIDs) {
const user = classroom.users.cachedUsers.get(uuid);
user && addUser(user);
}
for (const user of speakingJoiners) {
addUser(user);
}
for (const user of handRaisingJoiners) {
addUser(user);
}
creator && addUser(creator);
for (const user of otherJoiners) {
addUser(user);
}
return users;
const { creator, speakingJoiners, handRaisingJoiners, otherJoiners, offlineJoiners } =
classroom.users;
return [
...speakingJoiners,
...handRaisingJoiners,
...(creator ? [creator] : []),
...offlineJoiners,
...otherJoiners,
];
}).get();

const getDeviceState = useCallback(
Expand Down
1 change: 1 addition & 0 deletions packages/flat-stores/src/classroom-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ export class ClassroomStore {
user.camera = false;
}
});
this.users.updateOfflineJoiners(onStageUsers);

if (!this.isCreator) {
const isJoinerOnStage = Boolean(onStageUsersStorage.state[this.userUUID]);
Expand Down
12 changes: 12 additions & 0 deletions packages/flat-stores/src/user-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export class UserStore {
public handRaisingJoiners = observable.array<User>([]);
/** the rest joiners */
public otherJoiners = observable.array<User>([]);
/** joiners who have left */
public offlineJoiners = observable.array<User>([]);

public get joiners(): User[] {
return [...this.speakingJoiners, ...this.handRaisingJoiners, ...this.otherJoiners];
Expand Down Expand Up @@ -151,6 +153,16 @@ export class UserStore {
unSortedUsers.forEach(this.sortUser);
};

public updateOfflineJoiners(onStageUserUUIDs: string[]): void {
this.offlineJoiners.clear();
for (const userUUID of onStageUserUUIDs) {
const user = this.cachedUsers.get(userUUID);
if (user && user.hasLeft) {
this.offlineJoiners.push(user);
}
}
}

/**
* Fetch info of users who have left the room.
* For RTM message title.
Expand Down

0 comments on commit 9ad8786

Please sign in to comment.