Skip to content

Commit

Permalink
refactor: remove ToastInjector in favor of creating an instance via I…
Browse files Browse the repository at this point in the history
…njector.create

This commit removes a custom ToastInjector (that provided a single token) in favor of creating a new injector instance using the `Injector.create` call.

The goal of this change is to make the library forward-compatible with an upcoming change to the Injector interface, see angular/angular#46761.
  • Loading branch information
AndrewKushnir committed Sep 26, 2022
1 parent 8a0a9bf commit 9d16233
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
17 changes: 0 additions & 17 deletions src/lib/toastr/toast-injector.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);
}
}
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-injector';
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 9d16233

Please sign in to comment.