Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 339b2e3

Browse files
committed
fix: replace toObservableMicrotask private API with proper solution
1 parent 767e96b commit 339b2e3

File tree

6 files changed

+64
-11
lines changed

6 files changed

+64
-11
lines changed

libs/cdk/internals/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { accumulateObservables } from './lib/accumulateObservables';
22
export { getZoneUnPatchedApi } from './lib/get-zone-unpatched-api';
33
export { ObservableAccumulation, ObservableMap } from './lib/model';
44
export { timeoutSwitchMapWith } from './lib/timeout';
5+
export { toObservableMicrotaskInternal } from './lib/toObservableMicrotask';
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {
2+
assertInInjectionContext,
3+
DestroyRef,
4+
effect,
5+
inject,
6+
Injector,
7+
Signal,
8+
untracked,
9+
} from '@angular/core';
10+
import { toObservable, ToObservableOptions } from '@angular/core/rxjs-interop';
11+
import { Observable, ReplaySubject } from 'rxjs';
12+
13+
// Copied from angular/core/rxjs-interop/src/to_observable.ts -> because it's a private API
14+
// https://github.com/angular/angular/blob/46f00f951842dd117653df6cca3bfd5ee5baa0f1/packages/core/rxjs-interop/src/to_observable.ts#L72
15+
export function toObservableMicrotaskInternal<T>(
16+
source: Signal<T>,
17+
options?: ToObservableOptions,
18+
): Observable<T> {
19+
if (!options?.injector) {
20+
assertInInjectionContext(toObservable);
21+
}
22+
23+
const injector = options?.injector ?? inject(Injector);
24+
const subject = new ReplaySubject<T>(1);
25+
26+
const watcher = effect(
27+
() => {
28+
let value: T;
29+
try {
30+
value = source();
31+
} catch (err) {
32+
untracked(() => subject.error(err));
33+
return;
34+
}
35+
untracked(() => subject.next(value));
36+
},
37+
// forceRoot will ensure that the effect will be scheduled as a microtask
38+
{ injector, manualCleanup: true, forceRoot: true },
39+
);
40+
41+
injector.get(DestroyRef).onDestroy(() => {
42+
watcher.destroy();
43+
subject.complete();
44+
});
45+
46+
return subject.asObservable();
47+
}

libs/state/src/lib/rx-state.service.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
OnDestroy,
88
Signal,
99
} from '@angular/core';
10-
import { ɵtoObservableMicrotask, toSignal } from '@angular/core/rxjs-interop';
10+
import { toSignal } from '@angular/core/rxjs-interop';
11+
import { toObservableMicrotaskInternal } from '@rx-angular/cdk/internals/core';
1112
import {
1213
AccumulationFn,
1314
createAccumulationObservable,
@@ -570,7 +571,9 @@ export class RxState<State extends object>
570571

571572
if (isSignal(keyOrInputOrSlice$) && !projectOrSlices$ && !projectValueFn) {
572573
this.accumulator.nextSliceObservable(
573-
ɵtoObservableMicrotask(keyOrInputOrSlice$, { injector: this.injector }),
574+
toObservableMicrotaskInternal(keyOrInputOrSlice$, {
575+
injector: this.injector,
576+
}),
574577
);
575578
return;
576579
}
@@ -596,7 +599,7 @@ export class RxState<State extends object>
596599
!projectValueFn
597600
) {
598601
const projectionStateFn = projectOrSlices$;
599-
const slice$ = ɵtoObservableMicrotask(keyOrInputOrSlice$, {
602+
const slice$ = toObservableMicrotaskInternal(keyOrInputOrSlice$, {
600603
injector: this.injector,
601604
}).pipe(
602605
map((v) => projectionStateFn(this.accumulator.state, v as Value)),
@@ -622,7 +625,7 @@ export class RxState<State extends object>
622625
isSignal(projectOrSlices$) &&
623626
!projectValueFn
624627
) {
625-
const slice$ = ɵtoObservableMicrotask(projectOrSlices$, {
628+
const slice$ = toObservableMicrotaskInternal(projectOrSlices$, {
626629
injector: this.injector,
627630
}).pipe(map((value) => ({ ...{}, [keyOrInputOrSlice$]: value })));
628631
this.accumulator.nextSliceObservable(slice$);
@@ -653,7 +656,7 @@ export class RxState<State extends object>
653656
isSignal(projectOrSlices$)
654657
) {
655658
const key: Key = keyOrInputOrSlice$;
656-
const slice$ = ɵtoObservableMicrotask(projectOrSlices$, {
659+
const slice$ = toObservableMicrotaskInternal(projectOrSlices$, {
657660
injector: this.injector,
658661
}).pipe(
659662
map((value) => ({

libs/template/for/src/lib/for.directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import {
1818
TrackByFunction,
1919
ViewContainerRef,
2020
} from '@angular/core';
21-
import { ɵtoObservableMicrotask } from '@angular/core/rxjs-interop';
2221
import {
2322
coerceDistinctWith,
2423
coerceObservableWith,
2524
} from '@rx-angular/cdk/coercing';
25+
import { toObservableMicrotaskInternal } from '@rx-angular/cdk/internals/core';
2626
import {
2727
RxStrategyNames,
2828
RxStrategyProvider,
@@ -132,7 +132,7 @@ export class RxFor<T, U extends NgIterable<T> = NgIterable<T>>
132132
this.staticValue = undefined;
133133
this.renderStatic = false;
134134
this.observables$.next(
135-
ɵtoObservableMicrotask(potentialSignalOrObservable, {
135+
toObservableMicrotaskInternal(potentialSignalOrObservable, {
136136
injector: this.injector,
137137
}),
138138
);

libs/template/if/src/lib/if.directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import {
1414
TemplateRef,
1515
ViewContainerRef,
1616
} from '@angular/core';
17-
import { ɵtoObservableMicrotask } from '@angular/core/rxjs-interop';
1817
import { coerceAllFactory } from '@rx-angular/cdk/coercing';
18+
import { toObservableMicrotaskInternal } from '@rx-angular/cdk/internals/core';
1919
import {
2020
createTemplateNotifier,
2121
RxNotificationKind,
@@ -558,7 +558,7 @@ export class RxIf<T = unknown>
558558
if (changes.rxIf) {
559559
if (isSignal(this.rxIf)) {
560560
this.templateNotifier.next(
561-
ɵtoObservableMicrotask(this.rxIf, { injector: this.injector }),
561+
toObservableMicrotaskInternal(this.rxIf, { injector: this.injector }),
562562
);
563563
} else {
564564
this.templateNotifier.next(this.rxIf);

libs/template/let/src/lib/let.directive.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import {
1616
TemplateRef,
1717
ViewContainerRef,
1818
} from '@angular/core';
19-
import { ɵtoObservableMicrotask } from '@angular/core/rxjs-interop';
2019
import { coerceAllFactory } from '@rx-angular/cdk/coercing';
20+
import { toObservableMicrotaskInternal } from '@rx-angular/cdk/internals/core';
2121
import {
2222
createTemplateNotifier,
2323
RxNotification,
@@ -590,7 +590,9 @@ export class RxLet<U> implements OnInit, OnDestroy, OnChanges {
590590
if (changes.rxLet) {
591591
if (isSignal(this.rxLet)) {
592592
this.observablesHandler.next(
593-
ɵtoObservableMicrotask(this.rxLet, { injector: this.injector }),
593+
toObservableMicrotaskInternal(this.rxLet, {
594+
injector: this.injector,
595+
}),
594596
);
595597
} else {
596598
this.observablesHandler.next(this.rxLet);

0 commit comments

Comments
 (0)