Skip to content

Commit

Permalink
add option to revert to old hydrate (pre 4.9) behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mlrawlings committed Mar 14, 2019
1 parent 3b63b51 commit 6598c0c
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/components/ComponentDef.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ var extend = require("raptor-util/extend");
var KeySequence = require("./KeySequence");

var FLAG_WILL_RERENDER_IN_BROWSER = 1;
/*
var FLAG_HAS_BODY_EL = 2;
var FLAG_HAS_HEAD_EL = 4;
*/
// var FLAG_HAS_BODY_EL = 2;
// var FLAG_HAS_HEAD_EL = 4;
var FLAG_OLD_HYDRATE_NO_CREATE = 8;

/**
* A ComponentDef is used to hold the metadata collected at runtime for
Expand Down Expand Up @@ -106,7 +105,11 @@ ComponentDef.___deserialize = function(o, types, global, registry) {
// just building it from the server info
component.___updateQueued = true;

if (!isLegacy && flags & FLAG_WILL_RERENDER_IN_BROWSER) {
if (
!isLegacy &&
flags & FLAG_WILL_RERENDER_IN_BROWSER &&
!(flags & FLAG_OLD_HYDRATE_NO_CREATE)
) {
if (component.onCreate) {
component.onCreate(input, { global: global });
}
Expand Down
5 changes: 5 additions & 0 deletions src/components/beginComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ComponentDef = require("./ComponentDef");
var FLAG_WILL_RERENDER_IN_BROWSER = 1;
// var FLAG_HAS_BODY_EL = 2;
// var FLAG_HAS_HEAD_EL = 4;
var FLAG_OLD_HYDRATE_NO_CREATE = 8;

module.exports = function beginComponent(
componentsContext,
Expand Down Expand Up @@ -51,6 +52,10 @@ module.exports = function beginComponent(
componentDef.___flags |= FLAG_WILL_RERENDER_IN_BROWSER;
}

if (out.global.oldHydrateNoCreate === true) {
componentDef.___flags |= FLAG_OLD_HYDRATE_NO_CREATE;
}

if (ownerComponentDef && key != null) {
out.w(
"<!--" +
Expand Down
25 changes: 25 additions & 0 deletions src/taglibs/core/marko-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

module.exports = function codeGenerator(elNode, codegen) {
var builder = codegen.builder;
var context = codegen.context;

if (elNode.hasAttribute("no-browser-rerender")) {
context.deprecate("no-browser-rerender is deprecated");
let lhs = builder.memberExpression(
builder.identifier("out"),
builder.identifier("global")
Expand All @@ -19,5 +21,28 @@ module.exports = function codeGenerator(elNode, codegen) {
return builder.assignment(lhs, rhs);
}

if (
elNode.hasAttribute(
"deprecated-no-create-or-input-lifecycle-for-top-level-hydrate"
)
) {
context.deprecate(
"deprecated-no-create-or-input-lifecycle-for-top-level-hydrate is deprecated"
);
let lhs = builder.memberExpression(
builder.identifier("out"),
builder.identifier("global")
);

lhs = builder.memberExpression(
lhs,
builder.identifier("oldHydrateNoCreate")
);

let rhs = builder.literal(true);

return builder.assignment(lhs, rhs);
}

return null;
};
3 changes: 2 additions & 1 deletion src/taglibs/core/marko.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@
"deprecated": true,
"code-generator": "./marko-tag",
"open-tag-only": true,
"@no-browser-rerender": "boolean"
"@no-browser-rerender": "boolean",
"@deprecated-no-create-or-input-lifecycle-for-top-level-hydrate": "boolean"
},
"<marko-preserve-whitespace>": {
"deprecated": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class {
onCreate() {
if (typeof window !== 'undefined') {
window.childCreateCalled = true;
}
}
onInput() {
if (typeof window !== 'undefined') {
window.childInputCalled = true;
}
}
onRender() {
if (typeof window !== 'undefined') {
window.childRenderCalled = true;
}
}
}

<div/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class {
onCreate(input) {
if (typeof window !== 'undefined') {
throw new Error('onCreate should not be called in the browser');
}
this.state = { count:input.start }
}
onInput(input) {
if (typeof window !== 'undefined') {
throw new Error('onInput should not be called in the browser');
}
this.state = { count:input.start }
}
increment() {
this.state.count++;
}
onMount() {
window.testComponent = this;
}
}

<div>
Hello <span.count>${state.count}</span>!
<child-component key="child"/>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<marko deprecated-no-create-or-input-lifecycle-for-top-level-hydrate />

<!DOCTYPE html>
html lang="en"
head
meta charset="UTF-8"
title -- Marko Components Tests
body

div id="test"
div id="mocha"
div id="testsTarget"

<test-component start=10 />

init-components immediate
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var path = require("path");
var expect = require("chai").expect;

describe(path.basename(__dirname), function() {
it("should mount, not call the onCreate/onInput methods and update", function() {
var component = window.testComponent;

expect(component.input.start).to.equal(10);
expect(component.state.count).to.equal(10);
expect(component.el.querySelector(".count").innerHTML).to.equal("10");

component.increment();
component.update();

expect(component.input.start).to.equal(10);
expect(component.state.count).to.equal(11);
expect(component.el.querySelector(".count").innerHTML).to.equal("11");
});

it("should still call all lifecycle methods on child components", function() {
expect(window.childCreateCalled).to.equal(true);
expect(window.childInputCalled).to.equal(true);
expect(window.childRenderCalled).to.equal(true);
});
});

0 comments on commit 6598c0c

Please sign in to comment.