Skip to content

Commit

Permalink
refactor(flat-stores): merge whiteboard members into RTM members
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious committed Mar 6, 2024
1 parent 2eded3b commit 1213da8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/flat-services/src/services/whiteboard/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface IServiceWhiteboardEventData {
scrollPage: number;
maxScrollPage: number;
userScroll: void;
members: string[];
}

export type IServiceWhiteboardEventName = Extract<keyof IServiceWhiteboardEventData, string>;
Expand Down
2 changes: 2 additions & 0 deletions packages/flat-services/src/services/whiteboard/whiteboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export abstract class IServiceWhiteboard {

public abstract joinRoom(config: IServiceWhiteboardJoinRoomConfig): Promise<void>;

public abstract has(uid: string): boolean;

public abstract leaveRoom(): Promise<void>;

public abstract render(el: HTMLElement): void;
Expand Down
74 changes: 49 additions & 25 deletions packages/flat-stores/src/classroom-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class ClassroomStore {
roomUUID: this.roomUUID,
ownerUUID: this.ownerUUID,
userUUID: this.userUUID,
isInRoom: userUUID => this.rtm.members.has(userUUID),
isInRoom: userUUID => this.rtm.members.has(userUUID) || config.whiteboard.has(userUUID),
});

this.whiteboardStore = new WhiteboardStore({
Expand Down Expand Up @@ -515,33 +515,45 @@ export class ClassroomStore {
user.wbOperate = !!whiteboardStorage.state[user.userUUID];
});

const refreshUsers = (userUUIDs: string[]): void => {
const set = new Set(userUUIDs);
this.users.updateUsers(user => {
if (!set.has(user.userUUID)) {
return true;
}
if (user.userUUID === this.ownerUUID || onStageUsersStorage.state[user.userUUID]) {
user.isSpeak = true;
user.wbOperate = !!whiteboardStorage.state[user.userUUID];
user.isRaiseHand = false;
const deviceState = deviceStateStorage.state[user.userUUID];
if (deviceState) {
user.camera = deviceState.camera;
user.mic = deviceState.mic;
} else {
user.camera = false;
user.mic = false;
}
set.delete(user.userUUID);
if (set.size === 0) {
return false;
}
}
// not on stage, but has whiteboard access
if (whiteboardStorage.state[user.userUUID]) {
user.wbOperate = true;
set.delete(user.userUUID);
if (set.size === 0) {
return false;
}
}
return true;
});
};

this.sideEffect.addDisposer(
this.rtm.events.on("member-joined", async ({ userUUID }) => {
await this.users.addUser(userUUID, this.isUsersPanelVisible);
this.users.updateUsers(user => {
if (user.userUUID === userUUID) {
if (userUUID === this.ownerUUID || onStageUsersStorage.state[userUUID]) {
user.isSpeak = true;
user.wbOperate = !!whiteboardStorage.state[userUUID];
user.isRaiseHand = false;
const deviceState = deviceStateStorage.state[user.userUUID];
if (deviceState) {
user.camera = deviceState.camera;
user.mic = deviceState.mic;
} else {
user.camera = false;
user.mic = false;
}
return false;
}
// not on stage, but has whiteboard access
if (whiteboardStorage.state[userUUID]) {
user.wbOperate = true;
return false;
}
}
return true;
});
refreshUsers([userUUID]);
}),
);

Expand All @@ -551,6 +563,18 @@ export class ClassroomStore {
}),
);

this.sideEffect.addDisposer(
this.whiteboardStore.whiteboard.events.on("members", async (userUUIDs: string[]) => {
userUUIDs = userUUIDs.filter(userUUID => !this.users.cachedUsers.has(userUUID));
if (userUUIDs.length > 0) {
for (const userUUID of userUUIDs) {
await this.users.addUser(userUUID, this.isUsersPanelVisible);
}
refreshUsers(userUUIDs);
}
}),
);

this.sideEffect.addDisposer(
classroomStorage.on("stateChanged", diff => {
if (diff.raiseHandUsers) {
Expand Down
21 changes: 21 additions & 0 deletions service-providers/fastboard/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ export class Fastboard extends IServiceWhiteboard {
onPhaseChanged: phase => {
this._roomPhase$.setValue(phase);
},
onRoomStateChanged: state => {
if (state.roomMembers) {
const members: string[] = [];
for (const member of state.roomMembers) {
const uid = member.payload?.uid;
if (uid) {
members.push(uid);
}
}
this.events.emit("members", members);
}
},
onDisconnectWithError: error => {
this.toaster.emit("error", this.flatI18n.t("on-disconnect-with-error"));
console.error(error);
Expand Down Expand Up @@ -343,6 +355,15 @@ export class Fastboard extends IServiceWhiteboard {
);
}

public override has(uid: string): boolean {
const app = this._app$.value;
if (app) {
return app.room.state.roomMembers.some(member => member.payload?.uid === uid);
} else {
return false;
}
}

public async leaveRoom(): Promise<void> {
const app = this._app$.value;
if (app) {
Expand Down

0 comments on commit 1213da8

Please sign in to comment.