Skip to content

Commit

Permalink
make project be possibly undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmattig committed Nov 26, 2024
1 parent f76c356 commit e533c0f
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 55 deletions.
8 changes: 8 additions & 0 deletions projects/common/src/lib/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ export class UserService {
return this.session$.pipe(filter(isDefined));
}

/**
* @returns Retrieve a stream that notifies about the current session.
* May be undefined if there is no current session.
*/
getSessionOrUndefinedStream(): Observable<UserSession | undefined> {
return this.session$;
}

isLoggedIn(): Observable<boolean> {
return this.session$.pipe(first(), map(isDefined));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ export class MapContainerComponent implements AfterViewInit, OnChanges, OnDestro
}

private redrawLayers(projection: SpatialReference): void {
if (!this.mapLayersRaw) {
return;
}
this.mapLayers = this.mapLayersRaw.filter((mapLayer) => mapLayer.isVisible);

this.calculateGrid();
Expand Down
45 changes: 29 additions & 16 deletions projects/core/src/lib/project/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export interface FeatureSelection {
*/
@Injectable()
export class ProjectService implements OnDestroy {
private project$ = new ReplaySubject<Project>(1);
private project$ = new ReplaySubject<Project | undefined>(1);

private readonly layers = new Map<number, ReplaySubject<Layer>>();
private readonly layerState$ = new Map<number, Observable<LoadingState>>();
Expand Down Expand Up @@ -121,6 +121,7 @@ export class ProjectService implements OnDestroy {
this.userService
.getSessionStream()
.pipe(
tap(() => this.project$.next(undefined)),
mergeMap((session) =>
config.PROJECT.CREATE_TEMPORARY_PROJECT_AT_STARTUP
? this.createTemporaryProject(session.sessionToken)
Expand Down Expand Up @@ -278,14 +279,14 @@ export class ProjectService implements OnDestroy {
* Get a stream of Projects. This way compments can react to new Projects.
*/
getProjectStream(): Observable<Project> {
return this.project$;
return this.project$.pipe(filter(isDefined));
}

/**
* Get the current project and no further updates, e.g. for requests.
*/
getProjectOnce(): Observable<Project> {
return this.project$.pipe(first());
return this.getProjectStream().pipe(first());
}

/**
Expand Down Expand Up @@ -378,14 +379,14 @@ export class ProjectService implements OnDestroy {
* Get a stream of the projects projection.
*/
getSpatialReferenceStream(): Observable<SpatialReference> {
return this.project$.pipe(
return this.getProjectStream().pipe(
map((project: Project) => project.spatialReference),
distinctUntilChanged((x, y) => x.srsString === y.srsString),
);
}

getSpatialReferenceOnce(): Observable<SpatialReference> {
return this.project$.pipe(
return this.getProjectStream().pipe(
first(),
map((project: Project) => project.spatialReference),
);
Expand All @@ -395,15 +396,15 @@ export class ProjectService implements OnDestroy {
* Get a stream of the projects time.
*/
getTimeStream(): Observable<Time> {
return this.project$.pipe(
return this.getProjectStream().pipe(
map((project) => project.time),
distinctUntilChanged((t1, t2) => t1.isSame(t2)),
);
}

getTimeOnce(): Promise<Time> {
return firstValueFrom(
this.project$.pipe(
this.getProjectStream().pipe(
first(),
map((project) => project.time),
),
Expand All @@ -414,7 +415,7 @@ export class ProjectService implements OnDestroy {
* Get a stream of the projects time step size.
*/
getTimeStepDurationStream(): Observable<TimeStepDuration> {
return this.project$.pipe(
return this.getProjectStream().pipe(
map((project) => project.timeStepDuration),
distinctUntilChanged(),
);
Expand Down Expand Up @@ -645,7 +646,7 @@ export class ProjectService implements OnDestroy {
* Retrieve the layer models array as a stream.
*/
getLayerStream(): Observable<Array<Layer>> {
return this.project$.pipe(
return this.getProjectStream().pipe(
map((project) => project.layers),
distinctUntilChanged(),
);
Expand All @@ -655,7 +656,7 @@ export class ProjectService implements OnDestroy {
* Retrieve the plot models array as a stream.
*/
getPlotStream(): Observable<Array<Plot>> {
return this.project$.pipe(
return this.getProjectStream().pipe(
map((project) => project.plots),
distinctUntilChanged(),
);
Expand Down Expand Up @@ -793,7 +794,9 @@ export class ProjectService implements OnDestroy {

const result = this.getProjectOnce().pipe(
mergeMap((project) => {
const layers = project.layers.filter((l) => l.id !== layer.id);
const layers = project.layers.filter((l) => {
return l.id !== layer.id;
});

if (project.layers.length === layers.length) {
// nothing filtered, so no request
Expand Down Expand Up @@ -862,11 +865,13 @@ export class ProjectService implements OnDestroy {
* Sets the layers
*/
setLayers(layers: Array<Layer>): void {
this.project$.pipe(first()).subscribe((project) => {
if (project.layers !== layers) {
this.changeProjectConfig({layers});
}
});
this.getProjectStream()
.pipe(first())
.subscribe((project) => {
if (project.layers !== layers) {
this.changeProjectConfig({layers});
}
});
}

changeLayer(
Expand Down Expand Up @@ -1658,3 +1663,11 @@ export class ProjectService implements OnDestroy {
this.addLayer(layer);
}
}

/**
* Used as filter argument for T | undefined
*/
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
function isDefined<T>(arg: T | null | undefined): arg is T {
return arg !== null && arg !== undefined;
}
1 change: 1 addition & 0 deletions projects/core/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export * from './lib/util/pipes/trim.pipe';
export * from './lib/backend/backend.model';

export * from './lib/project/loading-state.model';
export * from './lib/project/project.model';
export * from './lib/users/session.model';
export * from './lib/users/user.model';

Expand Down
5 changes: 1 addition & 4 deletions projects/dashboards/ecometrics/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@
"types": []
},
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"],
"paths": {
"@geoengine/core": ["dist/core"]
}
"include": ["src/**/*.d.ts"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,6 @@ export class DashboardComponent implements AfterViewInit, AfterContentInit {
this.usageLoading.set(false);
}

async reset(): Promise<void> {
await firstValueFrom(this.dataSelectionService.clearPolygonLayer());
this.changeDetectorRef.markForCheck();
}

logout(): void {
this.userService.logout();
this.router.navigate(['/signin']);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Injectable} from '@angular/core';
import {LoadingState, ProjectService} from '@geoengine/core';
import {LoadingState, ProjectService, UserService, Project} from '@geoengine/core';
import {first, map, mergeMap, tap} from 'rxjs/operators';
import {BehaviorSubject, combineLatest, firstValueFrom, Observable, of} from 'rxjs';
import moment from 'moment';
Expand Down Expand Up @@ -30,16 +30,21 @@ export class DataSelectionService {
protected readonly _rasterLayer = new BehaviorSubject<RasterLayer | undefined>(undefined);
protected readonly _polygonLayer = new BehaviorSubject<VectorLayer | undefined>(undefined);

constructor(private readonly projectService: ProjectService) {
protected oldProject: Project | undefined;

constructor(
private readonly projectService: ProjectService,
readonly userService: UserService,
) {
this.rasterLayer = this._rasterLayer.pipe(
mergeMap((rasterLayer) =>
rasterLayer ? (projectService.getLayerChangesStream(rasterLayer) as Observable<RasterLayer>) : of(undefined),
),
mergeMap((rasterLayer) => {
return rasterLayer ? (projectService.getLayerChangesStream(rasterLayer) as Observable<RasterLayer>) : of(undefined);
}),
);
this.polygonLayer = this._polygonLayer.pipe(
mergeMap((polygonLayer) =>
polygonLayer ? (projectService.getLayerChangesStream(polygonLayer) as Observable<VectorLayer>) : of(undefined),
),
mergeMap((polygonLayer) => {
return polygonLayer ? (projectService.getLayerChangesStream(polygonLayer) as Observable<VectorLayer>) : of(undefined);
}),
);

this.layers = combineLatest([this.rasterLayer, this.polygonLayer]).pipe(
Expand All @@ -66,6 +71,12 @@ export class DataSelectionService {
}),
map((loadingState) => loadingState === LoadingState.LOADING),
);

this.userService.getSessionOrUndefinedStream().subscribe(async (_session) => {
this.oldProject = await firstValueFrom(this.projectService.getProjectOnce());
this._rasterLayer.next(undefined);
this._polygonLayer.next(undefined);
});
}

setRasterLayer(layer: RasterLayer, timeSteps: Array<Time>, dataRange: DataRange): Observable<void> {
Expand Down Expand Up @@ -94,21 +105,6 @@ export class DataSelectionService {
);
}

clearPolygonLayer(): Observable<void> {
return this._polygonLayer.pipe(
first(),
mergeMap((currentLayer) => {
if (currentLayer) {
const o = this.projectService.removeLayer(currentLayer);
this._polygonLayer.next(undefined);
return o;
} else {
return of(undefined);
}
}),
);
}

setPolygonLayer(layer: VectorLayer): Observable<void> {
return this._polygonLayer.pipe(
first(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {BehaviorSubject, Subscription} from 'rxjs';
import {BehaviorSubject, firstValueFrom, Subscription} from 'rxjs';

import {AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
import {UntypedFormControl, UntypedFormGroup, Validators} from '@angular/forms';

import {NotificationService, UserService, User, CoreModule} from '@geoengine/core';
import {NotificationService, UserService, User, CoreModule, ProjectService} from '@geoengine/core';
import {first} from 'rxjs/operators';
import {Router} from '@angular/router';
import {AsyncPipe} from '@angular/common';
Expand Down Expand Up @@ -46,6 +46,7 @@ export class LoginComponent implements OnInit, AfterViewInit, OnDestroy {
private readonly changeDetectorRef: ChangeDetectorRef,
private readonly userService: UserService,
private readonly notificationService: NotificationService,
private readonly projectService: ProjectService,
private readonly router: Router,
) {
this.loginForm = new UntypedFormGroup({
Expand Down Expand Up @@ -106,11 +107,13 @@ export class LoginComponent implements OnInit, AfterViewInit, OnDestroy {
password: this.loginForm.controls['password'].value,
})
.subscribe(
(session) => {
async (session) => {
this.user = session.user;
this.invalidCredentials$.next(false);
this.formStatus$.next(FormStatus.LoggedIn);

// wait for project to be loaded before redirecting
await firstValueFrom(this.projectService.getProjectOnce());
this.redirectToMainView();
},
() => {
Expand Down
5 changes: 1 addition & 4 deletions projects/dashboards/esg-indicator-service/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@
"types": []
},
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"],
"paths": {
"@geoengine/core": ["dist/core"]
}
"include": ["src/**/*.d.ts"]
}

0 comments on commit e533c0f

Please sign in to comment.