8000 refactor(state): remove ngOnDestroy in RxStateService · rx-angular/rx-angular@b5a893a · GitHub
[go: up one dir, main page]

Skip to content

Commit b5a893a

Browse files
committed
refactor(state): remove ngOnDestroy in RxStateService
1 parent 9e16487 commit b5a893a

File tree

5 files changed

+9
-29
lines changed

5 files changed

+9
-29
lines changed

libs/state/spec/rx-state.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('InheritanceTestComponent', () => {
136136

137137
it('should create', () => {
138138
stateChecker.checkSubscriptions(component, 1);
139-
component.ngOnDestroy();
139+
fixture.destroy();
140140
stateChecker.checkSubscriptions(component, 0);
141141
});
142142
});

libs/state/spec/rx-state.service.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('RxStateService', () => {
6363

6464
it('should unsubscribe on ngOnDestroy call', () => {
6565
stateChecker.checkSubscriptions(service, 1);
66-
service.ngOnDestroy();
66+
TestBed.resetTestingModule();
6767
stateChecker.checkSubscriptions(service, 0);
6868
});
6969

@@ -550,7 +550,7 @@ describe('RxStateService', () => {
550550
const tick$ = hot('aaaaaaaaaaaaaaa|', { a: 1 });
551551
const subs = '(^!)';
552552
state.connect(tick$.pipe(map((num) => ({ num }))));
553-
state.ngOnDestroy();
553+
TestBed.resetTestingModule();
554554
expectObservable(state.select()).toBe('');
555555
expectSubscriptions(tick$.subscriptions).toBe(subs);
556556
});
@@ -562,7 +562,7 @@ describe('RxStateService', () => {
562562
const tick$ = hot('aaaaaaaaaaaaaaa|', { a: 1 });
563563
const subs = '(^!)';
564564
state.connect('num' as any, tick$);
565-
state.ngOnDestroy();
565+
TestBed.resetTestingModule();
566566
expectSubscriptions(tick$.subscriptions).toBe(subs);
567567
expectObservable(state.select()).toBe('');
568568
});
@@ -574,7 +574,7 @@ describe('RxStateService', () => {
574574
const tick$ = hot('aaaaaaaaaaaaaaa|', { a: 1 });
575575
const subs = '(^!)';
576576
state.connect(tick$, (s, v) => ({ num: v * 42 }));
577-
state.ngOnDestroy();
577+
TestBed.resetTestingModule();
578578
expectObservable(state.select()).toBe('');
579579
expectSubscriptions(tick$.subscriptions).toBe(subs);
580580
});
@@ -586,7 +586,7 @@ describe('RxStateService', () => {
586586
const tick$ = hot('aaaaaaaaaaaaaaa|', { a: 1 });
587587
const subs = '(^!)';
588588
state.connect('num', tick$, (s, v) => v * 42);
589-
state.ngOnDestroy();
589+
TestBed.resetTestingModule();
590590
expectObservable(state.select()).toBe('');
591591
expectSubscriptions(tick$.subscriptions).toBe(subs);
592592
});
@@ -608,7 +608,7 @@ describe('RxStateService', () => {
608608
),
609609
).toBe('');
610610
expectSubscriptions(interval$.subscriptions).toBe(subs);
611-
state.ngOnDestroy();
611+
TestBed.resetTestingModule();
612612
});
613613
});
614614

libs/state/spec/rx-state.spec.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
rxState,
2222
RxStateSetupFn,
2323
} from '../src/lib/rx-state';
24-
import { RxState } from '../src/lib/rx-state.service';
2524

2625
describe(rxState, () => {
2726
it('should create rxState', () => {
@@ -136,14 +135,6 @@ describe(rxState, () => {
136135
});
137136
});
138137

139-
it('should call ngOnDestroy', () => {
140-
RxState.prototype.ngOnDestroy = jest.fn();
141-
const { fixture } = setupComponent();
142-
expect(RxState.prototype.ngOnDestroy).not.toHaveBeenCalled();
143-
fixture.destroy();
144-
expect(RxState.prototype.ngOnDestroy).toHaveBeenCalled();
145-
});
146-
147138
describe('signals', () => {
148139
it('should be undefined when key is undefined', () => {
149140
const { component } = setupComponent<{ count: number }>();

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ export type ReadOnly = 'get' | 'select' | 'computed' | 'signal';
7676
*/
7777
@Injectable()
7878
export class RxState<State extends object> implements Subscribable<State> {
79-
private readonly destroyRef = inject(DestroyRef);
8079
private subscription = new Subscription();
8180

8281
protected scheduler = inject(RX_STATE_SCHEDULER, { optional: true });
@@ -109,14 +108,7 @@ export class RxState<State extends object> implements Subscribable<State> {
109108
constructor() {
110109
this.subscription.add(this.subscribe());
111110

112-
this.destroyRef.onDestroy(() => this.ngOnDestroy());
113-
}
114-
115-
/**
116-
* @internal
117-
*/
118-
ngOnDestroy(): void {
119-
this.subscription.unsubscribe();
111+
inject(DestroyRef).onDestroy(() => this.subscription.unsubscribe());
120112
}
121113

122114
/**

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertInInjectionContext, DestroyRef, inject } from '@angular/core';
1+
import { assertInInjectionContext } from '@angular/core';
22
import { RxState as LegacyState } from './rx-state.service';
33

44
export type RxState<T extends object> = Pick<
@@ -52,9 +52,6 @@ export function rxState<State extends object>(
5252
assertInInjectionContext(rxState);
5353

5454
const legacyState = new LegacyState<State>();
55-
const destroyRef = inject(DestroyRef);
56-
57-
destroyRef.onDestroy(() => legacyState.ngOnDestroy());
5855

5956
const state: RxState<State> = {
6057
get: legacyState.get.bind(legacyState),

0 commit comments

Comments
 (0)
0