diff --git a/libs/ngxtension/signal-slice/src/signal-slice.spec.ts b/libs/ngxtension/signal-slice/src/signal-slice.spec.ts index 3228fa659..83dc1f2e5 100644 --- a/libs/ngxtension/signal-slice/src/signal-slice.spec.ts +++ b/libs/ngxtension/signal-slice/src/signal-slice.spec.ts @@ -1,3 +1,4 @@ +import { DestroyRef } from '@angular/core'; import { toSignal } from '@angular/core/rxjs-interop'; import { TestBed, fakeAsync, flush, tick } from '@angular/core/testing'; import { Observable, Subject, of, timer } from 'rxjs'; @@ -245,6 +246,32 @@ describe(signalSlice.name, () => { }); }); + it('should not register a new permanent subscription teardown on every call with a plain value', () => { + TestBed.runInInjectionContext(() => { + const destroyRef = TestBed.inject(DestroyRef); + const onDestroySpy = jest.spyOn(destroyRef, 'onDestroy'); + + const state = signalSlice({ + initialState, + actionSources: { + increaseAge: (state, $: Observable) => + $.pipe(map((amount) => ({ age: state().age + amount }))), + }, + }); + + // one permanent subscription is made at slice-creation time by + // the internal `connect(state, sharedObservable)` call + const baseline = onDestroySpy.mock.calls.length; + expect(baseline).toBeGreaterThan(0); + + state.increaseAge(1); + state.increaseAge(1); + state.increaseAge(1); + + expect(onDestroySpy.mock.calls.length).toEqual(baseline); + }); + }); + it('should resolve the updated state as a promise after reducer is invoked', (done) => { TestBed.runInInjectionContext(() => { const state = signalSlice({ diff --git a/libs/ngxtension/signal-slice/src/signal-slice.ts b/libs/ngxtension/signal-slice/src/signal-slice.ts index 7735a79db..e46b33341 100644 --- a/libs/ngxtension/signal-slice/src/signal-slice.ts +++ b/libs/ngxtension/signal-slice/src/signal-slice.ts @@ -209,7 +209,6 @@ export function signalSlice< destroyRef, subject, subs, - sharedObservable, ); } } @@ -293,7 +292,6 @@ function addReducerProperties( destroyRef: DestroyRef, subject: Subject, subs: Subject[], - observableFromActionSource?: Observable, ) { const version = createNotifier(); Object.defineProperties(readonlyState, { @@ -319,12 +317,6 @@ function addReducerProperties( }); } - if (observableFromActionSource) { - observableFromActionSource - .pipe(takeUntilDestroyed(destroyRef)) - .subscribe(); - } - return new Promise((res) => { state$.pipe(take(1)).subscribe((val) => { res(val);