Skip to content
Permalink
Browse files
feat: Remove ToastInjector use Injector.create (#947)
  • Loading branch information
AndrewKushnir committed Sep 28, 2022
1 parent 8a0a9bf commit b765833dabad737a3bbb501ed12f46f2eb394fe0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 21 deletions.
@@ -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';
@@ -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<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);
}
}
@@ -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';

@@ -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;

0 comments on commit b765833

Please sign in to comment.