Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added inject-destroy #22

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions libs/ngxtension/computed-from/src/computed-from.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,6 @@ describe(computedFrom.name, () => {
let fixture: ComponentFixture<TestComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [TestComponent],
}).compileComponents();
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
});
Expand Down Expand Up @@ -209,9 +206,6 @@ describe(computedFrom.name, () => {
let component: InInitComponent;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [InInitComponent],
}).compileComponents();
const fixture = TestBed.createComponent(InInitComponent);
component = fixture.componentInstance;
});
Expand Down
3 changes: 3 additions & 0 deletions libs/ngxtension/inject-destroy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ngxtension/inject-destroy

Secondary entry point of `ngxtension`. It can be used by importing from `ngxtension/inject-destroy`.
5 changes: 5 additions & 0 deletions libs/ngxtension/inject-destroy/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lib": {
"entryFile": "src/index.ts"
}
}
1 change: 1 addition & 0 deletions libs/ngxtension/inject-destroy/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './inject-destroy';
48 changes: 48 additions & 0 deletions libs/ngxtension/inject-destroy/src/inject-destroy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Component, OnInit } from '@angular/core';
import {
ComponentFixture,
TestBed,
fakeAsync,
tick,
} from '@angular/core/testing';
import { interval, takeUntil } from 'rxjs';
import { injectDestroy } from './inject-destroy';

describe(injectDestroy.name, () => {
describe('emits when the component is destroyed', () => {
@Component({ standalone: true, template: '' })
class TestComponent implements OnInit {
destroy$ = injectDestroy();
count = 0;

ngOnInit() {
interval(1000)
.pipe(takeUntil(this.destroy$))
.subscribe(() => this.count++);
}
}

let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;

beforeEach(async () => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
});

it('should handle async stuff', fakeAsync(() => {
component.ngOnInit();

expect(component.count).toBe(0);
tick(1000);
expect(component.count).toBe(1);
tick(1000);
expect(component.count).toBe(2);

fixture.destroy(); // destroy the component here

tick(1000);
expect(component.count).toBe(2);
}));
});
});
44 changes: 44 additions & 0 deletions libs/ngxtension/inject-destroy/src/inject-destroy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
DestroyRef,
inject,
Injector,
runInInjectionContext,
} from '@angular/core';
import { assertInjector } from 'ngxtension/assert-injector';
import { ReplaySubject } from 'rxjs';

/**
* Injects the `DestroyRef` service and returns a `ReplaySubject` that emits
* when the component is destroyed.
*
* @throws {Error} If no `DestroyRef` is found.
* @returns {ReplaySubject<void>} A `ReplaySubject` that emits when the component is destroyed.
*
* @example
* // In your component:
* export class MyComponent {
* private destroy$ = injectDestroy();
*
* getData() {
* return this.service.getData()
* .pipe(takeUntil(this.destroy$))
* .subscribe(data => { ... });
* }
* }
*/
export const injectDestroy = (injector?: Injector) => {
injector = assertInjector(injectDestroy, injector);

return runInInjectionContext(injector, () => {
const destroyRef = inject(DestroyRef);

const subject$ = new ReplaySubject<void>(1);

destroyRef.onDestroy(() => {
subject$.next();
subject$.complete();
});

return subject$;
});
};
4 changes: 3 additions & 1 deletion libs/ngxtension/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
"libs/ngxtension/repeat/**/*.ts",
"libs/ngxtension/repeat/**/*.html",
"libs/ngxtension/computed-from/**/*.ts",
"libs/ngxtension/computed-from/**/*.html"
"libs/ngxtension/computed-from/**/*.html",
"libs/ngxtension/inject-destroy/**/*.ts",
"libs/ngxtension/inject-destroy/**/*.html"
]
}
},
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"ngxtension/create-injection-token": [
"libs/ngxtension/create-injection-token/src/index.ts"
],
"ngxtension/inject-destroy": [
"libs/ngxtension/inject-destroy/src/index.ts"
],
"ngxtension/repeat": ["libs/ngxtension/repeat/src/index.ts"],
"ngxtension/resize": ["libs/ngxtension/resize/src/index.ts"]
}
Expand Down