From b956f4e425fe39f8c97902459570c746488b8e85 Mon Sep 17 00:00:00 2001 From: Vladimir Potekhin <46284632+vladimirpotekhin@users.noreply.github.com> Date: Sat, 16 May 2020 17:21:58 +0300 Subject: [PATCH] fixdirective): fix box factory to IVY support * chore(directive): extract option from nativeElement * chore(all): fix comments --- .../src/directives/resize-observer.directive.ts | 17 ++++++++++++++--- .../src/services/resize-observer.service.ts | 11 ++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/projects/resize-observer/src/directives/resize-observer.directive.ts b/projects/resize-observer/src/directives/resize-observer.directive.ts index 454c951..3b2f661 100644 --- a/projects/resize-observer/src/directives/resize-observer.directive.ts +++ b/projects/resize-observer/src/directives/resize-observer.directive.ts @@ -1,8 +1,19 @@ -import {Attribute, Directive, Inject, Output} from '@angular/core'; +import {Attribute, Directive, ElementRef, Inject, Output} from '@angular/core'; import {Observable} from 'rxjs'; import {ResizeObserverService} from '../services/resize-observer.service'; import {RESIZE_OPTION_BOX, RESIZE_OPTION_BOX_DEFAULT} from '../tokens/resize-option-box'; +// TODO switch to Attribute once https://github.com/angular/angular/issues/36479 is fixed +export function boxExtractor({ + nativeElement, +}: ElementRef): ResizeObserverOptions['box'] { + const attribute = nativeElement.getAttribute( + 'waResizeBox', + ) as ResizeObserverOptions['box']; + + return boxFactory(attribute); +} + export function boxFactory( box: ResizeObserverOptions['box'] | null, ): ResizeObserverOptions['box'] { @@ -16,8 +27,8 @@ export function boxFactory( ResizeObserverService, { provide: RESIZE_OPTION_BOX, - deps: [[new Attribute('waResizeBox')]], - useFactory: boxFactory, + deps: [ElementRef], + useFactory: boxExtractor, }, ], }) diff --git a/projects/resize-observer/src/services/resize-observer.service.ts b/projects/resize-observer/src/services/resize-observer.service.ts index e28f425..cea77e3 100644 --- a/projects/resize-observer/src/services/resize-observer.service.ts +++ b/projects/resize-observer/src/services/resize-observer.service.ts @@ -1,6 +1,6 @@ import {ElementRef, Inject, Injectable, NgZone} from '@angular/core'; import {Observable} from 'rxjs'; -import {finalize, share} from 'rxjs/operators'; +import {share} from 'rxjs/operators'; import {RESIZE_OPTION_BOX} from '../tokens/resize-option-box'; import {RESIZE_OBSERVER_SUPPORT} from '../tokens/support'; @@ -28,11 +28,12 @@ export class ResizeObserverService extends Observable< }); }); observer.observe(nativeElement, {box}); + + return () => { + observer.disconnect(); + }; }); - return this.pipe( - finalize(() => observer.disconnect()), - share(), - ); + return this.pipe(share()); } }