Skip to content

Commit

Permalink
refactor(flat-stores): user windows treat empty array as normal mode (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious authored Mar 9, 2023
1 parent f4c4370 commit 216f579
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/flat-stores/src/classroom-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export class ClassroomStore {
downlink: 0,
};

/** will never be empty array [] */
public userWindowsGrid: string[] | null = null;
public readonly userWindows = observable.map<string, UserWindow>();
public readonly userWindowsPortal = observable.map<string, HTMLDivElement>();
Expand Down Expand Up @@ -338,6 +339,10 @@ export class ClassroomStore {
return this.userWindowsGrid ? "maximized" : "normal";
}

private emptyArrayAsNull<T>(a: T[] | null): T[] | null {
return a && a.length > 0 ? a : null;
}

public async init(): Promise<void> {
await roomStore.syncOrdinaryRoomInfo(this.roomUUID);

Expand Down Expand Up @@ -582,7 +587,7 @@ export class ClassroomStore {
}
}
runInAction(() => {
this.userWindowsGrid = userWindowsStorage.state.grid;
this.userWindowsGrid = this.emptyArrayAsNull(userWindowsStorage.state.grid);
this.userWindows.replace(initialUserWindows);
});

Expand Down Expand Up @@ -611,7 +616,7 @@ export class ClassroomStore {
runInAction(() => {
for (const key in diff) {
if (key === "grid") {
this.userWindowsGrid = diff[key]!.newValue;
this.userWindowsGrid = this.emptyArrayAsNull(diff[key]!.newValue);
} else {
const userWindow: UserWindow | undefined = (diff as any)[key].newValue;
if (userWindow) {
Expand Down Expand Up @@ -798,7 +803,7 @@ export class ClassroomStore {

public createAvatarWindow = (userUUID: string, window: Omit<UserWindow, "z">): void => {
if (this.isCreator && this.userWindowsStorage) {
if (this.userWindowsStorage.state.grid) {
if (this.emptyArrayAsNull(this.userWindowsStorage.state.grid)) {
this.createMaximizedAvatarWindow(userUUID);
} else {
const maxZ = this.maxZOfUserWindows();
Expand All @@ -809,7 +814,10 @@ export class ClassroomStore {

public createMaximizedAvatarWindow = (userUUID: string): void => {
if (this.isCreator && this.userWindowsStorage) {
let grid = this.userWindowsStorage.state.grid || this.windowedUserUUIDs();
let grid = this.userWindowsStorage.state.grid;
if (!grid || grid.length === 0) {
grid = this.windowedUserUUIDs();
}
if (!grid.includes(userUUID)) {
grid = [...grid, userUUID];
}
Expand All @@ -828,7 +836,7 @@ export class ClassroomStore {
}

public updateAvatarWindow = (userUUID: string, window: UserWindow): void => {
if (this.isCreator && this.userWindowsStorage && !this.userWindowsStorage.state.grid) {
if (this.isCreator && this.userWindowsStorage && !this.userWindowsGrid) {
const maxZ = this.maxZOfUserWindows(userUUID);
const newValue = { ...window, z: maxZ + 1 };
const oldValue = (this.userWindowsStorage.state as any)[userUUID];
Expand Down

0 comments on commit 216f579

Please sign in to comment.