Skip to content

Commit

Permalink
refactor(upgrade): simplify special handling of ngUpgradeLite in `dow…
Browse files Browse the repository at this point in the history
…ngradeComponent()`
  • Loading branch information
gkalpak committed Dec 14, 2018
1 parent ad26cd6 commit f2887f4
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 24 deletions.
23 changes: 13 additions & 10 deletions packages/upgrade/src/common/downgrade_component.ts
Expand Up @@ -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> {
Expand Down Expand Up @@ -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
// Angular zone.
let needsNgZone = false;
// 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.
// 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;

Expand All @@ -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>;
}

Expand All @@ -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();

Expand All @@ -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) => () =>
Expand Down
4 changes: 2 additions & 2 deletions packages/upgrade/src/common/downgrade_component_adapter.ts
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions packages/upgrade/src/common/util.ts
Expand Up @@ -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>;
}
Expand Down
5 changes: 1 addition & 4 deletions packages/upgrade/src/dynamic/upgrade_adapter.ts
Expand Up @@ -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([
Expand Down
1 change: 0 additions & 1 deletion packages/upgrade/src/static/downgrade_module.ts
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions packages/upgrade/src/static/upgrade_module.ts
Expand Up @@ -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,
Expand Down

0 comments on commit f2887f4

Please sign in to comment.