From b83ec47f120153df05c36da19e60e96102066e0c Mon Sep 17 00:00:00 2001 From: alecarn <133774929+alecarn@users.noreply.github.com> Date: Wed, 14 Jun 2023 17:38:44 -0400 Subject: [PATCH] fix(LayerContext): subscribe once with first operator (#1272) * fix(LayerContext): subscribe once with first operator * fix(ObjectUtils): remove negative variant #1271 --- .../shared/layer-context.directive.ts | 14 +++++++------- packages/utils/src/lib/object-utils.ts | 4 ++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/context/src/lib/context-manager/shared/layer-context.directive.ts b/packages/context/src/lib/context-manager/shared/layer-context.directive.ts index d52b143212..82deaa588b 100644 --- a/packages/context/src/lib/context-manager/shared/layer-context.directive.ts +++ b/packages/context/src/lib/context-manager/shared/layer-context.directive.ts @@ -1,7 +1,7 @@ import { Directive, OnInit, OnDestroy, Optional, Input } from '@angular/core'; import { Subscription, merge } from 'rxjs'; -import { buffer, debounceTime, filter } from 'rxjs/operators'; +import { buffer, debounceTime, filter, first } from 'rxjs/operators'; import { RouteService, ConfigService } from '@igo2/core'; import { @@ -13,6 +13,7 @@ import { StyleService } from '@igo2/geo'; import type { IgoMap } from '@igo2/geo'; +import { ObjectUtils } from '@igo2/utils'; import { ContextService } from './context.service'; import { DetailedContext } from './context.interface'; @@ -58,12 +59,11 @@ export class LayerContextDirective implements OnInit, OnDestroy { this.route.options.visibleOffLayersKey && this.route.options.contextKey ) { - const queryParams$$ = this.route.queryParams.subscribe((params) => { - if (Object.keys(params).length > 0) { - this.queryParams = params; - queryParams$$.unsubscribe(); - } - }); + this.route.queryParams + .pipe(first(params => !ObjectUtils.isEmpty(params))) + .subscribe((params) => { + this.queryParams = params; + }); } } diff --git a/packages/utils/src/lib/object-utils.ts b/packages/utils/src/lib/object-utils.ts index c4b942c925..5fcbe7cf73 100644 --- a/packages/utils/src/lib/object-utils.ts +++ b/packages/utils/src/lib/object-utils.ts @@ -281,4 +281,8 @@ export class ObjectUtils { return newObj; } + + static isEmpty(obj: object): boolean { + return Object.keys(obj).length === 0; + } }