반응형
rxjs 단위로 구독당 한 번 관측 가능한 파이프에서 초기화 로직을 수행하는 방법
그래서 나는 이 관찰 가능한 파이프를 가지고 있는데, 당신이 사용할 수 있는 것처럼, 구독 초기에 한 번 수술을 해야 한다.finalize()
구독이 끝날 때 한 번 수술을 하다
그래서 이렇게 시작했는데 불행히도 스타트업은 한 사람당 한 번씩 할 겁니다.next()
그 문제를 논하다
const notificationSubject = new BehaviorSubject<Notification | undefined>(undefined);
const notifications$ = this.notificationSubject.pipe(
tap(() => startup()),
filter(isValueDefined),
finalize(() => shutdown())
);
notifications$.subscribe(noti => foo(noti));
notifications$.subscribe(noti => bar(noti));
그리고 우리는 이 변종을 얻었다:
let isStartedUp = false;
const internalStartup = () => {
if(!isStartedUp){
isStartedUp = true;
startup();
}
}
const notifications$ = notificationSubject.pipe(
tap(() => internalStartup()),
filter(isValueDefined),
finalize(() => shutdown())
);
notifications$.subscribe(noti => foo(noti));
notifications$.subscribe(noti => bar(noti));
... 지금 스타트업은 1회 구독이 아니라 1회 구독에 불과하기 때문에, 그것은 어떤 일이든 잘 할 수 있는 일이다.
나는 이것과 관련된 무언가가 있다고 상상하지만 나는 그것을 발견하지 못했다.
const notifications$ = notificationSubject.pipe(
initialize(() => startup()),
finalize(() => shutdown())
);
당신은 모든 구독에서 몇 가지 코드를 실행하는 데 사용할 수 있다.
export function initialize<T>(initializer: () => void): MonoTypeOperatorFunction<T> {
return (source: Observable<T>) => defer(() => {
initializer();
return source;
});
}
const notifications$ = notificationSubject.pipe(
initialize(() => startup()),
finalize(() => shutdown())
);
반응형
'programing' 카테고리의 다른 글
효소 jest ReactiveRedex Connected in Typecript (0) | 2022.03.25 |
---|---|
Quasar Framework에서 환경 변수를 가져오는 방법 (0) | 2022.03.25 |
라라벨과 함께 뷔에티파 (0) | 2022.03.25 |
Python 패키지에 버전을 포함시키는 표준 방법? (0) | 2022.03.25 |
PDF를 텍스트로 변환하기 위한 Python 모듈 (0) | 2022.03.25 |