From b765833dabad737a3bbb501ed12f46f2eb394fe0 Mon Sep 17 00:00:00 2001 From: Andrew Kushnir <43554145+AndrewKushnir@users.noreply.github.com> Date: Wed, 28 Sep 2022 10:36:49 -0700 Subject: [PATCH] feat: Remove ToastInjector use Injector.create (#947) --- src/lib/public_api.ts | 2 +- .../toastr/{toast-injector.ts => toast-ref.ts} | 17 ----------------- src/lib/toastr/toastr-config.ts | 2 +- src/lib/toastr/toastr.service.ts | 8 ++++++-- 4 files changed, 8 insertions(+), 21 deletions(-) rename src/lib/toastr/{toast-injector.ts => toast-ref.ts} (80%) diff --git a/src/lib/public_api.ts b/src/lib/public_api.ts index 007ed712..13d3e3af 100644 --- a/src/lib/public_api.ts +++ b/src/lib/public_api.ts @@ -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'; diff --git a/src/lib/toastr/toast-injector.ts b/src/lib/toastr/toast-ref.ts similarity index 80% rename from src/lib/toastr/toast-injector.ts rename to src/lib/toastr/toast-ref.ts index fa398f91..9d8a0e88 100644 --- a/src/lib/toastr/toast-injector.ts +++ b/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. @@ -86,18 +84,3 @@ export class ToastRef { } } } - -/** Custom injector type specifically for instantiating components with a toast. */ -export class ToastInjector implements Injector { - constructor( - private _toastPackage: ToastPackage, - private _parentInjector: Injector - ) {} - - get(token: any, notFoundValue?: T, flags?: InjectFlags): T | ToastPackage { - if (token === ToastPackage) { - return this._toastPackage; - } - return this._parentInjector.get(token, notFoundValue, flags); - } -} diff --git a/src/lib/toastr/toastr-config.ts b/src/lib/toastr/toastr-config.ts index 9adeab3f..b5b57451 100644 --- a/src/lib/toastr/toastr-config.ts +++ b/src/lib/toastr/toastr-config.ts @@ -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'; diff --git a/src/lib/toastr/toastr.service.ts b/src/lib/toastr/toastr.service.ts index d936dfdc..64e39b21 100644 --- a/src/lib/toastr/toastr.service.ts +++ b/src/lib/toastr/toastr.service.ts @@ -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, @@ -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;