refactor(upgrade): simplify special handling of ngUpgradeLite in `downgradeComponent()` (#27217)
PR Close #27217
This commit is contained in:
parent
07ada7f3d9
commit
8a7498e0ef
|
@ -11,7 +11,7 @@ import {ComponentFactory, ComponentFactoryResolver, Injector, NgZone, Type} from
|
|||
import * as angular from './angular1';
|
||||
import {$COMPILE, $INJECTOR, $PARSE, INJECTOR_KEY, LAZY_MODULE_REF, REQUIRE_INJECTOR, REQUIRE_NG_MODEL} from './constants';
|
||||
import {DowngradeComponentAdapter} from './downgrade_component_adapter';
|
||||
import {LazyModuleRef, controllerKey, getTypeName, isFunction, validateInjectionKey} from './util';
|
||||
import {LazyModuleRef, UpgradeAppType, controllerKey, getTypeName, getUpgradeAppType, isFunction, validateInjectionKey} from './util';
|
||||
|
||||
|
||||
interface Thenable<T> {
|
||||
|
@ -78,12 +78,15 @@ export function downgradeComponent(info: {
|
|||
$compile: angular.ICompileService,
|
||||
$injector: angular.IInjectorService,
|
||||
$parse: angular.IParseService): angular.IDirective {
|
||||
// When using `UpgradeModule`, we don't need to ensure callbacks to Angular APIs (e.g. change
|
||||
// detection) are run inside the Angular zone, because `$digest()` will be run inside the zone
|
||||
// (except if explicitly escaped, in which case we shouldn't force it back in).
|
||||
// When using `downgradeModule()` though, we need to ensure such callbacks are run inside the
|
||||
// When using `downgradeModule()`, we need to handle certain things specially. For example:
|
||||
// - We always need to attach the component view to the `ApplicationRef` for it to be
|
||||
// dirty-checked.
|
||||
// - We need to ensure callbacks to Angular APIs (e.g. change detection) are run inside the
|
||||
// Angular zone.
|
||||
let needsNgZone = false;
|
||||
// NOTE: This is not needed, when using `UpgradeModule`, because `$digest()` will be run
|
||||
// inside the Angular zone (except if explicitly escaped, in which case we shouldn't
|
||||
// force it back in).
|
||||
let isNgUpgradeLite = false;
|
||||
let wrapCallback = <T>(cb: () => T) => cb;
|
||||
let ngZone: NgZone;
|
||||
|
||||
|
@ -109,7 +112,7 @@ export function downgradeComponent(info: {
|
|||
validateInjectionKey($injector, downgradedModule, lazyModuleRefKey, attemptedAction);
|
||||
|
||||
const lazyModuleRef = $injector.get(lazyModuleRefKey) as LazyModuleRef;
|
||||
needsNgZone = lazyModuleRef.needsNgZone;
|
||||
isNgUpgradeLite = getUpgradeAppType($injector) === UpgradeAppType.Lite;
|
||||
parentInjector = lazyModuleRef.injector || lazyModuleRef.promise as Promise<Injector>;
|
||||
}
|
||||
|
||||
|
@ -130,7 +133,7 @@ export function downgradeComponent(info: {
|
|||
|
||||
const projectableNodes = facade.compileContents();
|
||||
facade.createComponent(projectableNodes);
|
||||
facade.setupInputs(needsNgZone, info.propagateDigest);
|
||||
facade.setupInputs(isNgUpgradeLite, info.propagateDigest);
|
||||
facade.setupOutputs();
|
||||
facade.registerCleanup();
|
||||
|
||||
|
@ -143,7 +146,7 @@ export function downgradeComponent(info: {
|
|||
}
|
||||
};
|
||||
|
||||
const downgradeFn = !needsNgZone ? doDowngrade : (injector: Injector) => {
|
||||
const downgradeFn = !isNgUpgradeLite ? doDowngrade : (injector: Injector) => {
|
||||
if (!ngZone) {
|
||||
ngZone = injector.get(NgZone);
|
||||
wrapCallback = <T>(cb: () => T) => () =>
|
||||
|
|
|
@ -81,7 +81,7 @@ export class DowngradeComponentAdapter {
|
|||
hookupNgModel(this.ngModel, this.component);
|
||||
}
|
||||
|
||||
setupInputs(needsNgZone: boolean, propagateDigest = true): void {
|
||||
setupInputs(manuallyAttachView: boolean, propagateDigest = true): void {
|
||||
const attrs = this.attrs;
|
||||
const inputs = this.componentFactory.inputs || [];
|
||||
for (let i = 0; i < inputs.length; i++) {
|
||||
|
@ -159,7 +159,7 @@ export class DowngradeComponentAdapter {
|
|||
|
||||
// If necessary, attach the view so that it will be dirty-checked.
|
||||
// (Allow time for the initial input values to be set and `ngOnChanges()` to be called.)
|
||||
if (needsNgZone || !propagateDigest) {
|
||||
if (manuallyAttachView || !propagateDigest) {
|
||||
let unwatch: Function|null = this.componentScope.$watch(() => {
|
||||
unwatch !();
|
||||
unwatch = null;
|
||||
|
|
|
@ -109,9 +109,6 @@ export class Deferred<R> {
|
|||
}
|
||||
|
||||
export interface LazyModuleRef {
|
||||
// Whether the AngularJS app has been bootstrapped outside the Angular zone
|
||||
// (in which case calls to Angular APIs need to be brought back in).
|
||||
needsNgZone: boolean;
|
||||
injector?: Injector;
|
||||
promise?: Promise<Injector>;
|
||||
}
|
||||
|
|
|
@ -510,10 +510,7 @@ export class UpgradeAdapter {
|
|||
.factory(INJECTOR_KEY, () => this.moduleRef !.injector.get(Injector))
|
||||
.factory(
|
||||
LAZY_MODULE_REF,
|
||||
[
|
||||
INJECTOR_KEY,
|
||||
(injector: Injector) => ({ injector, needsNgZone: false } as LazyModuleRef)
|
||||
])
|
||||
[INJECTOR_KEY, (injector: Injector) => ({ injector } as LazyModuleRef)])
|
||||
.constant(NG_ZONE_KEY, this.ngZone)
|
||||
.factory(COMPILER_KEY, () => this.moduleRef !.injector.get(Compiler))
|
||||
.config([
|
||||
|
|
|
@ -139,7 +139,6 @@ export function downgradeModule<T>(
|
|||
($injector: angular.IInjectorService) => {
|
||||
setTempInjectorRef($injector);
|
||||
const result: LazyModuleRef = {
|
||||
needsNgZone: true,
|
||||
promise: bootstrapFn(angular1Providers).then(ref => {
|
||||
injector = result.injector = new NgAdapterInjector(ref.injector);
|
||||
injector.get($INJECTOR);
|
||||
|
|
|
@ -179,10 +179,7 @@ export class UpgradeModule {
|
|||
|
||||
.factory(
|
||||
LAZY_MODULE_REF,
|
||||
[
|
||||
INJECTOR_KEY,
|
||||
(injector: Injector) => ({ injector, needsNgZone: false } as LazyModuleRef)
|
||||
])
|
||||
[INJECTOR_KEY, (injector: Injector) => ({ injector } as LazyModuleRef)])
|
||||
|
||||
.config([
|
||||
$PROVIDE, $INJECTOR,
|
||||
|
|
Loading…
Reference in New Issue