Skip to content

Commit

Permalink
WIP: cleanup of defer runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewKushnir committed Jul 30, 2023
1 parent a72c7bc commit fbcfc9d
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 155 deletions.
15 changes: 10 additions & 5 deletions packages/compiler/src/jit_compiler_facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ import {makeBindingParser, parseTemplate} from './render3/view/template';
import {ResourceLoader} from './resource_loader';
import {DomElementSchemaRegistry} from './schema/dom_element_schema_registry';

let enabledBlockTypes: Set<string>|undefined;

/** Temporary utility that enables specific block types in JIT compilations. */
export function ɵsetEnabledBlockTypes(types: string[]) {
enabledBlockTypes = types.length > 0 ? new Set(types) : undefined;
}

export class CompilerFacadeImpl implements CompilerFacade {
FactoryTarget = FactoryTarget;
ResourceLoader = ResourceLoader;
Expand Down Expand Up @@ -534,11 +541,9 @@ function parseJitTemplate(
const interpolationConfig =
interpolation ? InterpolationConfig.fromArray(interpolation) : DEFAULT_INTERPOLATION_CONFIG;
// Parse the template and check for errors.
const parsed = parseTemplate(template, sourceMapUrl, {
preserveWhitespaces,
interpolationConfig,
enabledBlockTypes: new Set(), // TODO: enable deferred blocks when testing in JIT mode.
});
const parsed = parseTemplate(
template, sourceMapUrl, {preserveWhitespaces, interpolationConfig, enabledBlockTypes});

if (parsed.errors !== null) {
const errors = parsed.errors.map(err => err.toString()).join(', ');
throw new Error(`Errors during JIT compilation of template for ${typeName}: ${errors}`);
Expand Down
17 changes: 17 additions & 0 deletions packages/compiler/src/render3/view/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,23 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
o.literal(templateVisitor.getVarCount()));
return trimTrailingNulls(parameters);
});

if (deferred.triggers.length > 0) {
// TODO: move this to a separate fn
for (const condition of deferred.triggers) {
if (condition instanceof t.BoundDeferredTrigger) {
const params: InstructionParams = () =>
this.convertPropertyBinding(condition.value as AST);
this.allocateBindingSlots(null);
this.updateInstructionWithAdvance(
templateIndex, deferred.sourceSpan, R3.deferWhen, params);
}
}
} else {
// If no other conditions are setup, generate the `deferOnIdle` instruction.
// TODO: consider including it at runtime instead?
this.creationInstruction(deferred.sourceSpan, R3.deferOnIdle, []);
}
}

visitDeferredBlockPlaceholder(block: t.DeferredBlockPlaceholder): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export {ElementRef} from './linker/element_ref';
export {NgModuleFactory, NgModuleRef} from './linker/ng_module_factory';
export {getModuleFactory, getNgModuleById} from './linker/ng_module_factory_loader';
export {QueryList} from './linker/query_list';
export {LazyTemplateRef, TemplateRef} from './linker/template_ref';
export {TemplateRef} from './linker/template_ref';
export {ViewContainerRef} from './linker/view_container_ref';
export {EmbeddedViewRef, ViewRef} from './linker/view_ref';
47 changes: 2 additions & 45 deletions packages/core/src/linker/template_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {Injector} from '../di/injector';
import {DehydratedContainerView} from '../hydration/interfaces';
import {TContainerNode, TNode, TNodeType} from '../render3/interfaces/node';
import {LView} from '../render3/interfaces/view';
import {LView, TView} from '../render3/interfaces/view';
import {getCurrentTNode, getLView} from '../render3/state';
import {createAndRenderEmbeddedLView} from '../render3/view_manipulation';
import {ViewRef as R3_ViewRef} from '../render3/view_ref';
Expand Down Expand Up @@ -120,7 +120,7 @@ const R3TemplateRef = class TemplateRef<T> extends ViewEngineTemplateRef<T> {
context: T, injector?: Injector,
hydrationInfo?: DehydratedContainerView): EmbeddedViewRef<T> {
const embeddedLView = createAndRenderEmbeddedLView(
this._declarationLView, this._declarationTContainer.index, context,
this._declarationLView, this._declarationTContainer, context,
{injector: injector, hydrationInfo: hydrationInfo});
return new R3_ViewRef<T>(embeddedLView);
}
Expand Down Expand Up @@ -154,46 +154,3 @@ export function createTemplateRef<T>(hostTNode: TNode, hostLView: LView): Templa
}
return null;
}

export class LazyTemplateRef<T> {
private embeddedViewTView: TView;

constructor(private declarationLView: LView, private declarationTContainer: TContainerNode) {
this.embeddedViewTView = declarationTContainer.tView as TView;
}

async load(): Promise<TemplateRef<T>> {
// TODO: access `loadingPromise` from TNode and make sure
// we don't re-create it here each time.
if (this.embeddedViewTView.dependencies instanceof Function) {
const results = await Promise.allSettled(this.embeddedViewTView.dependencies());
// TODO: handle 'rejected' status too
this.embeddedViewTView.dependencies =
results.map(result => (result.status === 'fulfilled') ? result.value : null!);
}
return new R3TemplateRef(
this.declarationLView, this.declarationTContainer,
createElementRef(this.declarationTContainer, this.declarationLView));
}

/**
* @internal
* @nocollapse
*/
static __NG_ELEMENT_ID__: () => LazyTemplateRef<any>| null = injectLazyTemplateRef;
}

export function injectLazyTemplateRef<T>(): LazyTemplateRef<T>|null {
return createLazyTemplateRef<T>(getCurrentTNode()!, getLView());
}

export function createLazyTemplateRef<T>(hostTNode: TNode, hostLView: LView): LazyTemplateRef<T>|
null {
if (hostTNode.type & TNodeType.Container) {
ngDevMode && assertDefined(hostTNode.tView, 'TView must be allocated');
ngDevMode &&
assertDefined(hostTNode.tView!.dependencies, 'Creating LazyTemplateRef for non-lazy view');
return new LazyTemplateRef<T>(hostLView, hostTNode as TContainerNode);
}
return null;
}
Loading

0 comments on commit fbcfc9d

Please sign in to comment.