8000 Merge pull request #1828 from michaelbe812/f/refactor-rx-state-to-use… · rx-angular/rx-angular@70a590d · GitHub
[go: up one dir, main page]

Skip to content

Commit 70a590d

Browse files
authored
Merge pull request #1828 from michaelbe812/f/refactor-rx-state-to-use-destroy-ref
refactor(state): use DestroyRef instead of OnDestroy life cycle hook
2 parents 07570f5 + c5e8d2d commit 70a590d

File tree

8 files changed

+20
-42
lines changed

8 files changed

+20
-42
lines changed

apps/demos/src/app/features/tutorials/basics/5-side-effects/side-effects.solution.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
ChangeDetectionStrategy,
33
Component,
44
Input,
5-
OnDestroy,
65
OnInit,
76
Output,
87
} from '@angular/core';
@@ -74,7 +73,7 @@ const initComponentState = {
7473
})
7574
export class SideEffectsSolution
7675
extends RxState<ComponentState>
77-
implements OnInit, OnDestroy
76+
implements OnInit
7877
{
7978
model$ = this.select();
8079

apps/demos/src/app/shared/debug-helper/value-provider/primitives-provider.service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export class PrimitivesProviderService {
5959
};
6060

6161
private resetObservables = () => {
62-
this.state.ngOnDestroy();
6362
runInInjectionContext(
6463
this.injector,
6564
() => (this.state = new RxState<ProvidedValues>()),

apps/demos/src/app/shared/viewport.service.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { BreakpointObserver, Breakpoints, BreakpointState } from '@angular/cdk/layout';
2-
import { Injectable, OnDestroy } from '@angular/core';
1+
import {
2+
BreakpointObserver,
3+
Breakpoints,
4+
BreakpointState,
5+
} from '@angular/cdk/layout';
6+
import { Injectable } from '@angular/core';
37
import { RxState } from '@rx-angular/state';
48
import { distinctUntilChanged, map } from 'rxjs/operators';
59

@@ -20,7 +24,7 @@ interface ViewportServiceState {
2024
@Injectable({
2125
providedIn: 'root',
2226
})
23-
export class ViewportService implements OnDestroy {
27+
export class ViewportService {
2428
private readonly state = new RxState<ViewportServiceState>();
2529

2630
readonly viewport$ = this.state.select('viewport');
@@ -52,15 +56,10 @@ export class ViewportService implements OnDestroy {
5256
);
5357
this.state.connect(viewport$, (oldState, viewportChange) => ({
5458
viewport: viewportChange,
55-
isHandset:
56-
viewportChange === 'mobile' || viewportChange === 'tablet',
59+
isHandset: viewportChange === 'mobile' || viewportChange === 'tablet',
5760
isMobile: viewportChange === 'mobile',
5861
isTablet: viewportChange === 'tablet',
5962
isDesktop: viewportChange === 'desktop',
6063
}));
6164
}
62-
63-
ngOnDestroy() {
64-
this.state.ngOnDestroy();
65-
}
6665
}

libs/state/spec/rx-state.component 1E0A .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: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
22
computed,
3+
DestroyRef,
34
inject,
45
Injectable,
56
Injector,
67
isSignal,
7-
OnDestroy,
88
Signal,
99
} from '@angular/core';
1010
import { toSignal } from '@angular/core/rxjs-interop';
@@ -75,9 +75,7 @@ export type ReadOnly = 'get' | 'select' | 'computed' | 'signal';
7575
* @docsPage RxState
7676
*/
7777
@Injectable()
78-
export class RxState<State extends object>
79-
implements OnDestroy, Subscribable<State>
80-
{
78+
export class RxState<State extends object> implements Subscribable<State> {
8179
private subscription = new Subscription();
8280

8381
protected scheduler = inject(RX_STATE_SCHEDULER, { optional: true });
@@ -109,13 +107,8 @@ export class RxState<State extends object>
109107
*/
110108
constructor() {
111109
this.subscription.add(this.subscribe());
112-
}
113110

114-
/**
115-
* @internal
116-
*/
117-
ngOnDestroy(): void {
118-
this.subscription.unsubscribe();
111+
inject(DestroyRef).onDestroy(() => this.subscription.unsubscribe());
119112
}
120113

121114
/**

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