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: Remove ToastInjector use Injector.create #947

Merged
merged 2 commits into from Sep 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/public_api.ts
Expand Up @@ -3,7 +3,7 @@ export * from './toastr/toast.component';
export * from './toastr/toastr.service';
export * from './toastr/toastr-config';
export * from './toastr/toastr.module';
export * from './toastr/toast-injector';
export * from './toastr/toast-ref';
export * from './toastr/toast-noanimation.component';

export * from './portal/portal';
Expand Down
17 changes: 0 additions & 17 deletions src/lib/toastr/toast-injector.ts → src/lib/toastr/toast-ref.ts
@@ -1,7 +1,5 @@
import { Injector, InjectFlags } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { OverlayRef } from '../overlay/overlay-ref';
import { ToastPackage } from './toastr-config';

/**
* Reference to a toast opened via the Toastr service.
Expand Down Expand Up @@ -86,18 +84,3 @@ export class ToastRef<T> {
}
}
}

/** Custom injector type specifically for instantiating components with a toast. */
export class ToastInjector implements Injector {
constructor(
private _toastPackage: ToastPackage,
private _parentInjector: Injector
) {}

get<T>(token: any, notFoundValue?: T, flags?: InjectFlags): T | ToastPackage {
if (token === ToastPackage) {
return this._toastPackage;
}
return this._parentInjector.get<T>(token, notFoundValue, flags);
}
}
2 changes: 1 addition & 1 deletion src/lib/toastr/toastr-config.ts
Expand Up @@ -3,7 +3,7 @@ import { InjectionToken } from '@angular/core';
import { Observable, Subject } from 'rxjs';

import { ComponentType } from '../portal/portal';
import { ToastRef } from './toast-injector';
import { ToastRef } from './toast-ref';

export type ProgressAnimationType = 'increasing' | 'decreasing';

Expand Down
8 changes: 6 additions & 2 deletions src/lib/toastr/toastr.service.ts
Expand Up @@ -5,7 +5,7 @@ import { Observable } from 'rxjs';

import { Overlay } from '../overlay/overlay';
import { ComponentPortal } from '../portal/portal';
import { ToastInjector, ToastRef } from './toast-injector';
import { ToastRef } from './toast-ref';
import { ToastContainerDirective } from './toast.directive';
import {
GlobalConfig,
Expand Down Expand Up @@ -231,7 +231,11 @@ export class ToastrService {
toastType,
toastRef,
);
const toastInjector = new ToastInjector(toastPackage, this._injector);

/** New injector that contains an instance of `ToastPackage`. */
const providers = [{provide: ToastPackage, useValue: toastPackage}];
const toastInjector = Injector.create({providers, parent: this._injector});

const component = new ComponentPortal(config.toastComponent, toastInjector);
const portal = overlayRef.attach(component, this.toastrConfig.newestOnTop);
toastRef.componentInstance = portal.instance;
Expand Down