Skip to content

Commit 09361af

Browse files
committed
fix: allow ngtRxStore to trigger change detection on specific state changes
1 parent 3aee531 commit 09361af

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

libs/angular-three/src/lib/stores/rx-store.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Injectable } from '@angular/core';
1+
import { ChangeDetectorRef, Injectable } from '@angular/core';
22
import { RxState } from '@rx-angular/state';
3-
import { MonoTypeOperatorFunction, Observable, startWith, tap } from 'rxjs';
3+
import { combineLatest, MonoTypeOperatorFunction, Observable, startWith, tap } from 'rxjs';
44
import type { NgtAnyRecord } from '../types';
55
import { is } from '../utils/is';
66

@@ -108,4 +108,16 @@ export class NgtRxStore<
108108
effect<S>(obs: Observable<S>, sideEffectFn: EffectFn<S>): void {
109109
return this.hold(obs.pipe(tapEffect(sideEffectFn)));
110110
}
111+
112+
triggerChangeDetection(cdr: ChangeDetectorRef, keys: Array<keyof TRxState> = []) {
113+
let $: Observable<any> = this.$;
114+
115+
if (keys.length) {
116+
$ = combineLatest(keys.map((key) => this.select(key).pipe(startWith(this.get(key) ?? undefined))));
117+
}
118+
119+
this.hold($, () => {
120+
requestAnimationFrame(() => void cdr.detectChanges());
121+
});
122+
}
111123
}

0 commit comments

Comments
 (0)