Skip to content

Commit

Permalink
feat: Remove ToastInjector use Injector.create (#947)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewKushnir authored Sep 28, 2022
1 parent 8a0a9bf commit b765833
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/lib/public_api.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit b765833

Please sign in to comment.