Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
gkalpak committed Nov 21, 2018
1 parent ad60714 commit e1865d1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
25 changes: 21 additions & 4 deletions packages/upgrade/src/common/downgrade_component.ts
Expand Up @@ -107,6 +107,7 @@ export function downgradeComponent(info: {

const ngModel: angular.INgModelController = required[1];
let parentInjector: Injector|Thenable<Injector>|undefined = required[0];
let moduleInjector: Injector|Thenable<Injector>|undefined = undefined;
let ranAsync = false;

if (!parentInjector || hasMultipleDowngradedModules) {
Expand All @@ -117,9 +118,25 @@ export function downgradeComponent(info: {
validateInjectionKey($injector, downgradedModule, lazyModuleRefKey, attemptedAction);

const lazyModuleRef = $injector.get(lazyModuleRefKey) as LazyModuleRef;
parentInjector = lazyModuleRef.injector || lazyModuleRef.promise as Promise<Injector>;
moduleInjector = lazyModuleRef.injector || lazyModuleRef.promise as Promise<Injector>;
}

// `mergeInjector` is a special injector that ... <<----- TODO
const mergeInjector: Injector|Thenable<Injector> = !moduleInjector ?
parentInjector! : !parentInjector ?
moduleInjector : Promise.
all([parentInjector, moduleInjector]).
then(([pInjector, mInjector]) => ({
get: (token: any, notFoundValue: any, flags: any) => {
let value;
try {
value = pInjector.get(token, notFoundValue, flags);
} catch {
}
return value || mInjector.get(token, notFoundValue, flags);
},
}));

const doDowngrade = (injector: Injector) => {
const componentFactoryResolver: ComponentFactoryResolver =
injector.get(ComponentFactoryResolver);
Expand Down Expand Up @@ -158,10 +175,10 @@ export function downgradeComponent(info: {
wrapCallback(() => doDowngrade(injector))();
};

if (isThenable<Injector>(parentInjector)) {
parentInjector.then(downgradeFn);
if (isThenable<Injector>(mergeInjector)) {
mergeInjector.then(downgradeFn);
} else {
downgradeFn(parentInjector);
downgradeFn(mergeInjector);
}

ranAsync = true;
Expand Down
Expand Up @@ -312,7 +312,7 @@ withEachNg1Version(() => {
});
}));

it('should correctly traverse the injector tree of downgraded components (from different modules)',
fit('should correctly traverse the injector tree of downgraded components (from different modules)',
async(() => {
@Component({
selector: 'ng2A',
Expand Down Expand Up @@ -417,11 +417,11 @@ withEachNg1Version(() => {

// Wait for module B to be bootstrapped.
setTimeout(() => {
// This is arguably incorrect behavior, since the parent component injector is not
// taken into account.
// Ideally the order of traversal should be: CompB > CompA > ModB > ModA > Plat
// (This is similar to how lazy-loaded components work with `@angular/router`.)
expect(multiTrim(element.children[0].textContent))
.toBe(
'ng2A( FOO:CompB-foo BAR:ModB-bar BAZ:ModB-baz QUX:ModB-qux QUUX:Plat-quux )');
'ng2A( FOO:CompB-foo BAR:CompA-bar BAZ:ModA-baz QUX:ModB-qux QUUX:Plat-quux )');

// Standalone component B.
$rootScope.$apply('showB2 = true');
Expand Down

0 comments on commit e1865d1

Please sign in to comment.