Skip to content

Commit 5336adf

Browse files
committed
refactor(lib): move timeout logic into ElementBoundaryService
1 parent c035792 commit 5336adf

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

projects/ngx-element-boundary/src/lib/cross-boundary-ng-element-strategy.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ComponentRef, Injector } from '@angular/core';
22
import { NgElementStrategy, NgElementStrategyFactory } from '@angular/elements';
3-
import { of, Subject } from 'rxjs';
4-
import { take, takeUntil, timeoutWith } from 'rxjs/operators';
3+
import { Subject } from 'rxjs';
4+
import { take, takeUntil } from 'rxjs/operators';
55

66
import {
77
ElementBoundaryNgElementStrategy,
@@ -96,15 +96,8 @@ export class CrossBoundaryNgElementStrategy implements NgElementStrategy {
9696
}
9797

9898
this.elementBoundaryService
99-
.whenBoundaryExist(element)
100-
.pipe(
101-
(o$) =>
102-
this.options.boundaryTimeoutMs > 0
103-
? o$.pipe(timeoutWith(this.options.boundaryTimeoutMs, of(null)))
104-
: o$,
105-
take(1),
106-
takeUntil(this.disconnect$),
107-
)
99+
.whenBoundaryExist(element, this.options.boundaryTimeoutMs)
100+
.pipe(take(1), takeUntil(this.disconnect$))
108101
.subscribe((boundary) => {
109102
if (boundary) {
110103
this.hookableInjector.hook(boundary.injector);

projects/ngx-element-boundary/src/lib/element-boundary.service.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export class ElementBoundaryService {
3535
/**
3636
* Wait until appropriate "element boundary" exists for HTML Element
3737
*/
38-
whenBoundaryExist(element: HTMLElement): Observable<ElementBoundary> {
38+
whenBoundaryExist(
39+
element: HTMLElement,
40+
timeoutMs: number,
41+
): Observable<ElementBoundary | null> {
3942
return this.boundaries$.pipe(
4043
map((boundaries) =>
4144
boundaries.find(
@@ -45,6 +48,8 @@ export class ElementBoundaryService {
4548
),
4649
),
4750
filter(isDefined),
51+
(o$) => (timeoutMs > 0 ? o$.pipe(timeoutWith(timeoutMs, of(null))) : o$),
52+
shareReplay({ bufferSize: 1, refCount: false }),
4853
);
4954
}
5055

0 commit comments

Comments
 (0)