Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the initializer into the constructor #850

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ import {ActivatedRoute, NavigationEnd, Router} from '@angular/router';
styleUrls: ['./device-pane.component.scss'],
})
export class DevicePaneComponent {
groups = this.groupService.getGroups();
devices = this.deviceService.getDevices();
groups;
devices;

constructor(
private deviceService: DeviceService,
public displaysService: DisplaysService,
private groupService: GroupService,
private activatedRoute: ActivatedRoute,
private router: Router
) {}
) {
this.groups = this.groupService.getGroups();
this.devices = this.deviceService.getDevices();
}

ngOnInit(): void {
this.router.events
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/operator/webui/src/app/device.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ export class DeviceService {
shareReplay(1)
);

private allDeviceFromServer = this.httpClient
.get<DeviceItem[]>('./devices')
.pipe(map(this.sortDevices));
private allDeviceFromServer;

private getDevicesByGroupIdFromServer(groupId: string) {
return this.httpClient
Expand All @@ -38,7 +36,11 @@ export class DeviceService {
constructor(
private readonly httpClient: HttpClient,
private sanitizer: DomSanitizer
) {}
) {
this.allDeviceFromServer = this.httpClient
.get<DeviceItem[]>('./devices')
.pipe(map(this.sortDevices));
}

private sortDevices(devices: DeviceItem[]) {
return devices.sort((a: DeviceItem, b: DeviceItem) =>
Expand Down
34 changes: 19 additions & 15 deletions frontend/src/operator/webui/src/app/displays.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@ import {DeviceService} from './device.service';
providedIn: 'root',
})
export class DisplaysService {
private devices = this.deviceService.getDevices();
private devices;

private visibleDeviceIds: string[] = [];
private visibleDevicesChanged = new Subject<void>();
private visibleDevicesChanged;

private deviceVisibilityInfos = merge(
this.devices,
this.visibleDevicesChanged.pipe(mergeMap(() => this.devices))
).pipe(
map(devices =>
devices.map(({device_id: deviceId}) => {
return {id: deviceId, visible: this.isVisibleDevice(deviceId)};
})
)
);

private displayInfoChanged = new Subject<any>();
private deviceVisibilityInfos;
private displayInfoChanged;

constructor(private deviceService: DeviceService) {}
constructor(private deviceService: DeviceService) {
this.devices = this.deviceService.getDevices();
this.visibleDevicesChanged = new Subject<void>();
this.deviceVisibilityInfos = merge(
this.devices,
this.visibleDevicesChanged.pipe(mergeMap(() => this.devices))
).pipe(
map(devices =>
devices.map(({device_id: deviceId}) => {
return {id: deviceId, visible: this.isVisibleDevice(deviceId)};
})
)
);
this.displayInfoChanged = new Subject<any>();
}

toggleVisibility(deviceId: string): void {
if (this.isVisibleDevice(deviceId)) {
Expand Down
26 changes: 14 additions & 12 deletions frontend/src/operator/webui/src/app/group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ import {DomSanitizer} from '@angular/platform-browser';
})
export class GroupService {
private refreshSubject = new Subject<void>();
private groupsFromServer = this.httpClient
private groupsFromServer;
private groups;

constructor(
private readonly httpClient: HttpClient,
) {
this.groupsFromServer = this.httpClient
.get<string[]>('./groups')
.pipe(
map((deviceIds: string[]) =>
deviceIds.sort((a, b) => a.localeCompare(b, undefined, {numeric: true}))
)
deviceIds.sort((a, b) => a.localeCompare(b, undefined, {numeric: true}))
)
);

private groups = merge(
this.groupsFromServer,
this.refreshSubject.pipe(mergeMap(() => this.groupsFromServer))
).pipe(shareReplay(1));

constructor(
private readonly httpClient: HttpClient,
) {}
this.groups = merge(
this.groupsFromServer,
this.refreshSubject.pipe(mergeMap(() => this.groupsFromServer))
).pipe(shareReplay(1));
}

refresh(): void {
this.refreshSubject.next();
Expand Down
210 changes: 105 additions & 105 deletions frontend/src/operator/webui/src/app/view-pane/view-pane.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,52 +105,22 @@ export class ViewPaneComponent implements OnInit, OnDestroy, AfterViewInit {
constructor(
public displaysService: DisplaysService,
@Inject(DOCUMENT) public document: Document
) {}

ngOnInit(): void {
this.resizeObserver = new ResizeObserver(entries => {
this.cols$.next(entries[0].contentRect.width);
});
this.resizeObserver.observe(this.viewPane.nativeElement);
this.cols$.subscribe(cols => {
this.cols = cols;
});
}

ngAfterViewInit(): void {
this.resizeSubscription = merge(
fromEvent(window, 'resize'),
fromEvent(window, 'orientationchange')
)
.pipe(debounceTime(50))
.subscribe(() => {
this.grid.resize();
});
}

ngOnDestroy(): void {
this.resizeSubscription.unsubscribe();
this.resizeObserver.unobserve(this.viewPane.nativeElement);
}

private visibleDevicesChanged: Observable<DeviceGridItemUpdate[]> =
this.displaysService.getDeviceVisibilities().pipe(
) {
this.visibleDevicesChanged = this.displaysService.getDeviceVisibilities().pipe(
map(deviceVisibilityInfos =>
deviceVisibilityInfos.map(info => {
return {
values: {
...this.createDeviceGridItem(info.id),
visible: info.visible,
},
overwrites: ['visible'],
source: this.visibleDeviceSource,
};
})
)
deviceVisibilityInfos.map(info => {
return {
values: {
...this.createDeviceGridItem(info.id),
visible: info.visible,
},
overwrites: ['visible'],
source: this.visibleDeviceSource,
};
})
)
);

private displayInfoChanged: Observable<DeviceGridItemUpdate[]> =
this.displaysService.getDisplayInfoChanged().pipe(
this.displayInfoChanged = this.displaysService.getDisplayInfoChanged().pipe(
map(displayInfo => {
const updateValues = this.createDeviceGridItem(displayInfo.device_id);
const overwrites = [];
Expand Down Expand Up @@ -185,9 +155,7 @@ export class ViewPaneComponent implements OnInit, OnDestroy, AfterViewInit {
];
})
);

private layoutUpdated: Observable<DeviceGridItemUpdate[]> =
this.layoutUpdated$.pipe(
this.layoutUpdated = this.layoutUpdated$.pipe(
map(newLayout => {
return newLayout.map(layoutItem => {
const updateValues = this.createDeviceGridItem(layoutItem.id);
Expand All @@ -206,6 +174,95 @@ export class ViewPaneComponent implements OnInit, OnDestroy, AfterViewInit {
});
})
);
this.resultLayout = merge(
this.visibleDevicesChanged,
this.displayInfoChanged,
this.layoutUpdated
).pipe(
scan(
(
currentLayout: DeviceGridItem[],
updateInfos: DeviceGridItemUpdate[]
) => {
const layoutById = new Map(currentLayout.map(item => [item.id, item]));

updateInfos.forEach(updateItem => {
const id = updateItem.values.id;
let item = layoutById.has(id)
? layoutById.get(id)!
: updateItem.values;

updateItem.overwrites.forEach(prop => {
// Ignore force show display message when display size is already set
if ((prop === 'display_width' || prop === 'display_height')
&& item[prop] !== null && item[prop] !== this.freeScale
&& updateItem.values[prop] === this.freeScale) {
return;
}

item[prop] = updateItem.values[prop]!;
});

switch (updateItem.source) {
case this.visibleDeviceSource: {
if (!item.placed && item.visible) {
// When a new item is set visible
item = this.placeNewItem(item, currentLayout);
}
break;
}
case this.layoutUpdateSource: {
// When layout is changed
item = this.adjustNewLayout(item);
break;
}
case this.displayInfoSource: {
// When device display info is changed
item = this.adjustDisplayInfo(item);
break;
}
}

layoutById.set(id, item);
});

return Array.from(layoutById, ([, value]) => value);
},
[]
),
map(items => items.filter(item => item.visible))
);
}

ngOnInit(): void {
this.resizeObserver = new ResizeObserver(entries => {
this.cols$.next(entries[0].contentRect.width);
});
this.resizeObserver.observe(this.viewPane.nativeElement);
this.cols$.subscribe(cols => {
this.cols = cols;
});
}

ngAfterViewInit(): void {
this.resizeSubscription = merge(
fromEvent(window, 'resize'),
fromEvent(window, 'orientationchange')
)
.pipe(debounceTime(50))
.subscribe(() => {
this.grid.resize();
});
}

ngOnDestroy(): void {
this.resizeSubscription.unsubscribe();
this.resizeObserver.unobserve(this.viewPane.nativeElement);
}

private visibleDevicesChanged: Observable<DeviceGridItemUpdate[]>;
private displayInfoChanged: Observable<DeviceGridItemUpdate[]>;
private layoutUpdated: Observable<DeviceGridItemUpdate[]>;

private adjustNewLayout(item: DeviceGridItem): DeviceGridItem {
if (item.display_width === null || item.display_height === null)
Expand Down Expand Up @@ -285,64 +342,7 @@ export class ViewPaneComponent implements OnInit, OnDestroy, AfterViewInit {
return item;
}

resultLayout: Observable<DeviceGridItem[]> = merge(
this.visibleDevicesChanged,
this.displayInfoChanged,
this.layoutUpdated
).pipe(
scan(
(
currentLayout: DeviceGridItem[],
updateInfos: DeviceGridItemUpdate[]
) => {
const layoutById = new Map(currentLayout.map(item => [item.id, item]));

updateInfos.forEach(updateItem => {
const id = updateItem.values.id;
let item = layoutById.has(id)
? layoutById.get(id)!
: updateItem.values;

updateItem.overwrites.forEach(prop => {
// Ignore force show display message when display size is already set
if ((prop === 'display_width' || prop === 'display_height')
&& item[prop] !== null && item[prop] !== this.freeScale
&& updateItem.values[prop] === this.freeScale) {
return;
}

item[prop] = updateItem.values[prop]!;
});

switch (updateItem.source) {
case this.visibleDeviceSource: {
if (!item.placed && item.visible) {
// When a new item is set visible
item = this.placeNewItem(item, currentLayout);
}
break;
}
case this.layoutUpdateSource: {
// When layout is changed
item = this.adjustNewLayout(item);
break;
}
case this.displayInfoSource: {
// When device display info is changed
item = this.adjustDisplayInfo(item);
break;
}
}

layoutById.set(id, item);
});

return Array.from(layoutById, ([, value]) => value);
},
[]
),
map(items => items.filter(item => item.visible))
);
resultLayout: Observable<DeviceGridItem[]>;

forceShowDevice(deviceId: string) {
this.displaysService.onDeviceDisplayInfo({
Expand Down
Loading