Skip to content
This repository was archived by the owner on Apr 4, 2019. It is now read-only.

Commit db6462f

Browse files
author
Godhuda
committed
Use the templateId instead of template identity
This allows us to use a unified code path between rehydration and normal rendering. We also discovered that we don’t use `lastYielded.self` (it was an accidental holdover from the code that was refactored into lastYielded), so we removed it (along with a vestigial `shadowTemplate`, removed in a previous commit).
1 parent 0305e4a commit db6462f

File tree

3 files changed

+13
-34
lines changed

3 files changed

+13
-34
lines changed

packages/htmlbars-compiler/tests/dirtying-test.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ test("it is possible to rehydrate a template with blocks", function() {
650650
let childMorph2 = env.dom.createMorphAt(span, 0, 0);
651651

652652
let obj = { bool: true, name: "Yehuda" };
653-
childMorph1.lastYielded = new LastYielded(obj, template.raw.templates[0], null);
653+
childMorph1.lastYielded = new LastYielded(template.raw.templates[0].id);
654654

655655
rootMorph.childNodes = [childMorph1];
656656
childMorph1.childNodes = [childMorph2];
@@ -691,14 +691,6 @@ test("it is possible to serialize a render node tree", function() {
691691
});
692692

693693
test("it is possible to serialize a render node tree with recursive templates", function() {
694-
env.hooks.rehydrateLastYielded = function(env, morph) {
695-
morph.lastYielded.template = morph.lastYielded.templateId;
696-
};
697-
698-
env.hooks.serializeLastYielded = function(env, morph) {
699-
return morph.lastYielded.template;
700-
};
701-
702694
let template = compile('<p title="{{title}}">{{#if bool}}<span>{{name}}</span>{{/if}}</p>');
703695
let obj = { title: 'chancancode', name: 'Godfrey', bool: true };
704696
let result = template.render(obj, env);

packages/htmlbars-runtime/lib/hooks.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ import { linkParams } from "../htmlbars-util/morph-utils";
7979
*/
8080

8181
export function wrap(template) {
82-
if (template === null) { return null; }
82+
if (template === null) { return null; }
8383

8484
return {
8585
meta: template.meta,
@@ -140,11 +140,10 @@ function yieldTemplate(template, env, parentScope, morph, renderState, visitor)
140140

141141
var scope = parentScope;
142142

143-
if (morph.lastYielded && morph.lastYielded.isStableTemplate(template)) {
143+
if (morph.lastResult && morph.lastYielded.isStableTemplate(template)) {
144144
return morph.lastResult.revalidateWith(env, undefined, self, blockArguments, visitor);
145145
}
146146

147-
148147
// Check to make sure that we actually **need** a new scope, and can't
149148
// share the parent scope. Note that we need to move this check into
150149
// a host hook, because the host's notion of scope may require a new
@@ -153,17 +152,14 @@ function yieldTemplate(template, env, parentScope, morph, renderState, visitor)
153152
scope = env.hooks.createChildScope(parentScope);
154153
}
155154

156-
if (morph.lastYielded && morph.lastYielded.templateId) {
157-
env.hooks.rehydrateLastYielded(env, morph);
158-
159-
if (morph.lastYielded.isStableTemplate(template)) {
160-
let renderResult = RenderResult.rehydrate(env, scope, template, { renderNode: morph, self, blockArguments });
161-
renderResult.render();
162-
return;
163-
}
155+
// rehydration
156+
if (morph.lastYielded && morph.lastYielded.isStableTemplate(template)) {
157+
let renderResult = RenderResult.rehydrate(env, scope, template, { renderNode: morph, self, blockArguments });
158+
renderResult.render();
159+
return;
164160
}
165161

166-
morph.lastYielded = new LastYielded(self, template, null);
162+
morph.lastYielded = new LastYielded(template.id);
167163

168164
// Render the template that was selected by the helper
169165
render(template, env, scope, { renderNode: morph, self: self, blockArguments: blockArguments });
@@ -1048,8 +1044,6 @@ export default {
10481044
linkRenderNode: linkRenderNode,
10491045
partial: partial,
10501046
subexpr: subexpr,
1051-
rehydrateLastYielded: null,
1052-
serializeLastYielded: null,
10531047

10541048
// fundamental hooks with good default behavior
10551049
bindBlock: bindBlock,

packages/htmlbars-runtime/lib/render.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ function _rehydrateNode(owner, renderNode, dom, context, cache) {
348348
case 'range':
349349
element = elementFromId(dom, context, renderNode.parentNode, cache);
350350
node = dom.createMorphAt(element, renderNode.firstNode, renderNode.lastNode);
351-
node.lastYielded = LastYielded.fromTemplateId(renderNode.templateId);
351+
node.lastYielded = new LastYielded(renderNode.templateId);
352352
node.childNodes = renderNode.childNodes && renderNode.childNodes.map(childNode => _rehydrateNode(node, childNode, dom, context, cache));
353353
break;
354354
}
@@ -395,7 +395,7 @@ function _serializeNode(env, renderNode, serializationContext) {
395395
type: 'range',
396396
childNodes: renderNode.childNodes && renderNode.childNodes.map(childNode => _serializeNode(env, childNode, serializationContext)),
397397
parentNode: idFromElement(dom, parent, serializationContext),
398-
templateId: renderNode.lastYielded && env.hooks.serializeLastYielded(env, renderNode),
398+
templateId: renderNode.lastYielded && renderNode.lastYielded.templateId,
399399
firstNode,
400400
lastNode
401401
};
@@ -441,18 +441,11 @@ function idFromElement(dom, element, serializationContext) {
441441
return id;
442442
}
443443

444-
export function LastYielded(self, template, shadowTemplate, templateId) {
445-
this.self = self;
446-
this.template = template;
447-
this.shadowTemplate = shadowTemplate;
444+
export function LastYielded(templateId) {
448445
this.templateId = templateId;
449446
}
450447

451-
LastYielded.fromTemplateId = function(templateId) {
452-
return new LastYielded(null, null, null, templateId);
453-
};
454-
455448
LastYielded.prototype.isStableTemplate = function(nextTemplate) {
456-
return !this.shadowTemplate && nextTemplate === this.template;
449+
return nextTemplate.id === this.templateId;
457450
};
458451

0 commit comments

Comments
 (0)