Skip to content

Commit

Permalink
fix(geo): offline import dynamically
Browse files Browse the repository at this point in the history
(cherry picked from commit 7f63a56aa3256c660d77c0d5cbd8649e54415195)

fix: update deps

fix: update deps

fix(geo): offline revert change for undefined param

fix: types

fix(geo): offline - ngx-indexed-db take an mapped object

refactor(geo): move the offline configuration in integration options

fix: resync lock file
  • Loading branch information
alecarn committed Nov 11, 2024
1 parent 94148a5 commit 4d7e630
Show file tree
Hide file tree
Showing 16 changed files with 461 additions and 1,349 deletions.
1,591 changes: 340 additions & 1,251 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ export class LayerContextDirective implements OnInit, OnDestroy {
}
});

this.layerService
.createAsyncIdbLayers(context.uri)
.pipe(debounceTime(500))
.subscribe((layers: Layer[]) => this.handleAddLayers(layers));
if (this.configService.getConfig('offline')?.enable) {
this.layerService
.createAsyncIdbLayers(context.uri)
.pipe(debounceTime(500))
.subscribe((layers) => this.handleAddLayers(layers));
}
}

private handleAddLayers(layers: Layer[]) {
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
"@angular/router": "^18.0.0",
"@igo2/utils": "*",
"@sentry/angular": "^8.26.0",
"ngx-indexed-db": "^19.0.0",
"ngx-toastr": "^19.0.0",
"@ngx-translate/core": "^15.0.0",
"@ngx-translate/http-loader": "^8.0.0",
Expand Down
34 changes: 1 addition & 33 deletions packages/core/src/lib/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,13 @@ import { IgoLanguageModule, provideTranslation } from '@igo2/core/language';
import { provideMessage } from '@igo2/core/message';
import { IgoErrorModule } from '@igo2/core/request';

import { DBConfig, NgxIndexedDBModule } from 'ngx-indexed-db';

const dbConfig: DBConfig = {
name: 'igo2DB',
version: 2,
objectStoresMeta: [
{
store: 'geoData',
storeConfig: { keyPath: 'url', autoIncrement: false },
storeSchema: [
{ name: 'regionID', keypath: 'regionID', options: { unique: false } }
]
},
{
store: 'layerData',
storeConfig: { keyPath: 'layerId', autoIncrement: false },
storeSchema: [
{
name: 'layerOptions',
keypath: 'layerOptions',
options: { unique: false }
},
{
name: 'sourceOptions',
keypath: 'sourceOptions',
options: { unique: false }
}
]
}
]
};
@NgModule({
declarations: [],
exports: [IgoActivityModule, IgoErrorModule, IgoLanguageModule],
imports: [
CommonModule,
IgoActivityModule.forRoot(),
IgoErrorModule.forRoot(),
NgxIndexedDBModule.forRoot(dbConfig)
IgoErrorModule.forRoot()
],
providers: [provideMessage(), provideHttpClient(withInterceptorsFromDi())]
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
ItemStyleOptions,
LegendOptions
} from '../../layer/shared/layers/legend.interface';
import type { MapService } from '../../map/shared/map.service';
import { MapService } from '../../map/shared/map.service';
import { getResolutionFromScale } from '../../map/shared/map.utils';
import {
QueryFormat,
Expand Down
14 changes: 7 additions & 7 deletions packages/geo/src/lib/layer/shared/layer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ export class LayerService {
private http: HttpClient,
private styleService: StyleService,
private dataSourceService: DataSourceService,
private geoNetworkService: GeoNetworkService,
private messageService: MessageService,
private layerDBService: LayerDBService,
@Optional() private authInterceptor: AuthInterceptor
@Optional() private geoNetworkService?: GeoNetworkService,
@Optional() private layerDBService?: LayerDBService,
@Optional() private authInterceptor?: AuthInterceptor
) {}

createLayer(layerOptions: AnyLayerOptions): Layer {
Expand Down Expand Up @@ -204,7 +204,7 @@ export class LayerService {
this.messageService,
this.authInterceptor,
this.geoNetworkService,
this.geoNetworkService.geoDBService,
this.geoNetworkService?.geoDBService,
this.layerDBService
);
}
Expand All @@ -225,7 +225,7 @@ export class LayerService {
this.messageService,
this.authInterceptor,
this.geoNetworkService,
this.geoNetworkService.geoDBService,
this.geoNetworkService?.geoDBService,
this.layerDBService
);
}
Expand All @@ -240,7 +240,7 @@ export class LayerService {
this.messageService,
this.authInterceptor,
this.geoNetworkService,
this.geoNetworkService.geoDBService,
this.geoNetworkService?.geoDBService,
this.layerDBService
);
}
Expand Down Expand Up @@ -388,7 +388,7 @@ export class LayerService {
}

createAsyncIdbLayers(contextUri = '*'): Observable<Layer[]> {
return this.layerDBService.getAll().pipe(
return this.layerDBService?.getAll().pipe(
concatMap((res) => {
const idbLayers =
contextUri !== '*'
Expand Down
6 changes: 1 addition & 5 deletions packages/geo/src/lib/layer/shared/layers/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import { DataSource } from '../../../datasource/shared/datasources/datasource';
import { Legend } from '../../../datasource/shared/datasources/datasource.interface';
import type { MapBase } from '../../../map/shared/map.abstract';
import { getResolutionFromScale } from '../../../map/shared/map.utils';
import { GeoDBService } from '../../../offline/geoDB/geoDB.service';
import { LayerDBService } from '../../../offline/layerDB/layerDB.service';
import { LayerOptions } from './layer.interface';

export abstract class Layer {
Expand Down Expand Up @@ -141,9 +139,7 @@ export abstract class Layer {
constructor(
public options: LayerOptions,
protected messageService?: MessageService,
protected authInterceptor?: AuthInterceptor,
protected geoDBService?: GeoDBService,
public layerDBService?: LayerDBService
protected authInterceptor?: AuthInterceptor
) {
this.dataSource = options.source;

Expand Down
8 changes: 1 addition & 7 deletions packages/geo/src/lib/layer/shared/layers/vector-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,7 @@ export class VectorLayer extends Layer {
public geoDBService?: GeoDBService,
public layerDBService?: LayerDBService
) {
super(
options,
messageService,
authInterceptor,
geoDBService,
layerDBService
);
super(options, messageService, authInterceptor);
this.watcher = new VectorWatcher(this);
this.status$ = this.watcher.status$;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import { InsertSourceInsertDBEnum } from './geoDB.enums';
import { DatasToIDB, GeoDBData } from './geoDB.interface';
import { GeoDBService } from './geoDB.service';

@Injectable({
providedIn: 'root'
})
@Injectable()
export class ConfigFileToGeoDBService {
constructor(
private http: HttpClient,
Expand Down
35 changes: 18 additions & 17 deletions packages/geo/src/lib/offline/geoDB/geoDB.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@angular/core';
import { Injectable, Optional } from '@angular/core';

import { Compression } from '@igo2/utils';

Expand All @@ -9,16 +9,17 @@ import { concatMap, first, map, take } from 'rxjs/operators';
import { InsertSourceInsertDBEnum } from './geoDB.enums';
import { GeoDBData } from './geoDB.interface';

@Injectable({
providedIn: 'root'
})
@Injectable()
export class GeoDBService {
readonly dbName: string = 'geoData';
public collisionsMap = new Map<number, string[]>();
public _newData = 0;
private compression = new Compression();

constructor(private ngxIndexedDBService: NgxIndexedDBService) {}
constructor(
@Optional()
private ngxIndexedDBService: NgxIndexedDBService
) {}

/**
* Only blob can be compressed
Expand Down Expand Up @@ -60,12 +61,12 @@ export class GeoDBService {
insertSource,
insertEvent
};
return this.ngxIndexedDBService.getByID(this.dbName, url);
return this.ngxIndexedDBService?.getByID(this.dbName, url);
}),
concatMap((dbObject: GeoDBData) => {
if (!dbObject) {
this._newData++;
return this.ngxIndexedDBService.add(this.dbName, geoDBData);
return this.ngxIndexedDBService?.add(this.dbName, geoDBData);
} else {
const currentRegionID = dbObject.regionID;
if (currentRegionID !== regionID) {
Expand All @@ -90,15 +91,15 @@ export class GeoDBService {

private customUpdate(geoDBData: GeoDBData): Observable<GeoDBData> {
const subject = new Subject<GeoDBData>();
const deleteRequest = this.ngxIndexedDBService.deleteByKey(
const deleteRequest = this.ngxIndexedDBService?.deleteByKey(
this.dbName,
geoDBData.url
);
deleteRequest
.pipe(
concatMap((isDeleted) =>
isDeleted
? this.ngxIndexedDBService.add(this.dbName, geoDBData)
? this.ngxIndexedDBService?.add(this.dbName, geoDBData)
: of(undefined)
)
)
Expand All @@ -112,7 +113,7 @@ export class GeoDBService {
}

get(url: string): Observable<any> {
return this.ngxIndexedDBService.getByID(this.dbName, url).pipe(
return this.ngxIndexedDBService?.getByID(this.dbName, url).pipe(
map((data: GeoDBData) => {
if (data) {
const object = data.object;
Expand All @@ -126,11 +127,11 @@ export class GeoDBService {
}

getByID(url: string): Observable<any> {
return this.ngxIndexedDBService.getByID(this.dbName, url);
return this.ngxIndexedDBService?.getByID(this.dbName, url);
}

deleteByKey(url: string): Observable<any> {
return this.ngxIndexedDBService.deleteByKey(this.dbName, url);
return this.ngxIndexedDBService?.deleteByKey(this.dbName, url);
}

getRegionCountByID(id: number): Observable<number> {
Expand All @@ -148,7 +149,7 @@ export class GeoDBService {
}

const IDBKey: IDBKeyRange = IDBKeyRange.only(id);
const dbRequest = this.ngxIndexedDBService.getAllByIndex(
const dbRequest = this.ngxIndexedDBService?.getAllByIndex(
this.dbName,
'regionID',
IDBKey
Expand All @@ -162,14 +163,14 @@ export class GeoDBService {
}

const IDBKey: IDBKeyRange = IDBKeyRange.only(id);
const dbRequest = this.ngxIndexedDBService.getAllByIndex(
const dbRequest = this.ngxIndexedDBService?.getAllByIndex(
this.dbName,
'regionID',
IDBKey
);
dbRequest.subscribe((datas: GeoDBData[]) => {
datas.forEach((data) => {
this.ngxIndexedDBService.deleteByKey(this.dbName, data.url);
this.ngxIndexedDBService?.deleteByKey(this.dbName, data.url);
});
});
return dbRequest;
Expand All @@ -179,7 +180,7 @@ export class GeoDBService {
keyRange: IDBKeyRange = IDBKeyRange.lowerBound(0),
mode: DBMode = DBMode.readonly
) {
const request = this.ngxIndexedDBService.openCursorByIndex(
const request = this.ngxIndexedDBService?.openCursorByIndex(
this.dbName,
'regionID',
keyRange,
Expand All @@ -202,7 +203,7 @@ export class GeoDBService {
for (const [regionID, collisions] of this.collisionsMap) {
for (const url of collisions) {
this.ngxIndexedDBService
.getByKey(this.dbName, url)
?.getByKey(this.dbName, url)
.pipe(take(1))
.subscribe((dbObject: GeoDBData) => {
const updatedObject = dbObject;
Expand Down
2 changes: 2 additions & 0 deletions packages/geo/src/lib/offline/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './geoDB';
export * from './layerDB';
export * from './shared';
export * from './offline.interface';
export * from './offline.provider';
22 changes: 10 additions & 12 deletions packages/geo/src/lib/offline/layerDB/layerDB.service.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { Injectable } from '@angular/core';
import { Injectable, Optional } from '@angular/core';

import { NgxIndexedDBService } from 'ngx-indexed-db';
import { Observable, Subject, of } from 'rxjs';
import { concatMap, first } from 'rxjs/operators';

import { LayerDBData } from './layerDB.interface';

@Injectable({
providedIn: 'root'
})
@Injectable()
export class LayerDBService {
readonly dbName: string = 'layerData';

constructor(private ngxIndexedDBService: NgxIndexedDBService) {}
constructor(@Optional() private ngxIndexedDBService: NgxIndexedDBService) {}

/**
* This method allow to update the stored layer into the indexeddb (layerData)
Expand All @@ -22,12 +20,12 @@ export class LayerDBService {
update(layerDBData: LayerDBData): Observable<any> {
const subject = new Subject<LayerDBData>();
this.ngxIndexedDBService
.getByID(this.dbName, layerDBData.layerId)
?.getByID(this.dbName, layerDBData.layerId)
.pipe(
first(),
concatMap((dbObject: LayerDBData) => {
if (!dbObject) {
return this.ngxIndexedDBService.add(this.dbName, layerDBData);
return this.ngxIndexedDBService?.add(this.dbName, layerDBData);
} else {
return this.customUpdate(layerDBData);
}
Expand All @@ -42,15 +40,15 @@ export class LayerDBService {

private customUpdate(layerDBData: LayerDBData): Observable<LayerDBData> {
const subject = new Subject<LayerDBData>();
const deleteRequest = this.ngxIndexedDBService.deleteByKey(
const deleteRequest = this.ngxIndexedDBService?.deleteByKey(
this.dbName,
layerDBData.layerId
);
deleteRequest
.pipe(
concatMap((isDeleted) =>
isDeleted
? this.ngxIndexedDBService.add(this.dbName, layerDBData)
? this.ngxIndexedDBService?.add(this.dbName, layerDBData)
: of(undefined)
)
)
Expand All @@ -69,7 +67,7 @@ export class LayerDBService {
* @returns
*/
getByID(layerId: string): Observable<LayerDBData> {
return this.ngxIndexedDBService.getByID(this.dbName, layerId);
return this.ngxIndexedDBService?.getByID(this.dbName, layerId);
}

/**
Expand All @@ -78,7 +76,7 @@ export class LayerDBService {
* @returns
*/
deleteByKey(layerId: string): Observable<boolean> {
return this.ngxIndexedDBService.deleteByKey(this.dbName, layerId);
return this.ngxIndexedDBService?.deleteByKey(this.dbName, layerId);
}

/**
Expand All @@ -87,6 +85,6 @@ export class LayerDBService {
* @returns
*/
getAll(): Observable<LayerDBData[]> {
return this.ngxIndexedDBService.getAll(this.dbName);
return this.ngxIndexedDBService?.getAll(this.dbName);
}
}
3 changes: 3 additions & 0 deletions packages/geo/src/lib/offline/offline.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IOfflineOptions {
enable: boolean;
}
Loading

0 comments on commit 4d7e630

Please sign in to comment.