Skip to content

Commit 3a691d9

Browse files
fix(signals): run rxMethod outside of reactive context (#4224)
Co-authored-by: markostanimirovic <markostanimirovic95@gmail.com>
1 parent 1df0eb5 commit 3a691d9

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

modules/signals/rxjs-interop/spec/rx-method.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,17 @@ describe('rxMethod', () => {
218218
/NG0203: rxMethod\(\) can only be used within an injection context/
219219
);
220220
});
221+
222+
it('allows signal updates', () => {
223+
const counter = signal(1);
224+
const increment = TestBed.runInInjectionContext(() =>
225+
rxMethod<number>(tap((n) => counter.update((value) => value + n)))
226+
);
227+
228+
const num = signal(3);
229+
increment(num);
230+
231+
TestBed.flushEffects();
232+
expect(counter()).toBe(4);
233+
});
221234
});

modules/signals/rxjs-interop/src/rx-method.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Injector,
77
isSignal,
88
Signal,
9+
untracked,
910
} from '@angular/core';
1011
import { isObservable, noop, Observable, Subject, Unsubscribable } from 'rxjs';
1112

@@ -31,7 +32,13 @@ export function rxMethod<Input>(
3132

3233
const rxMethodFn = (input: RxMethodInput<Input>) => {
3334
if (isSignal(input)) {
34-
const watcher = effect(() => source$.next(input()), { injector });
35+
const watcher = effect(
36+
() => {
37+
const value = input();
38+
untracked(() => source$.next(value));
39+
},
40+
{ injector }
41+
);
3542
const instanceSub = { unsubscribe: () => watcher.destroy() };
3643
sourceSub.add(instanceSub);
3744

0 commit comments

Comments
 (0)