Skip to content

Commit

Permalink
support tokens that are not a function (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip authored Jul 30, 2018
1 parent c3cca42 commit 839c838
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
4 changes: 1 addition & 3 deletions packages/electrode-react-webapp/lib/react/token-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,7 @@ module.exports = function setup(handlerContext /* , asyncTemplate */) {
: `<script${context.user.scriptNonce}>${context.user.content.prefetch}</script>`;
},

[META_TAGS_MARKER]: () => {
return iconStats;
},
[META_TAGS_MARKER]: iconStats,

[CRITICAL_CSS_MARKER]: context => {
return criticalCSS ? `<style${context.user.styleNonce}>${criticalCSS}</style>` : "";
Expand Down
8 changes: 8 additions & 0 deletions packages/electrode-react-webapp/lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ class Renderer {

const tkFunc = handler.tokens[tk.id];

// not a function, just add it to output
if (typeof tkFunc !== "function") {
return xt => {
xt.context.output.add(tkFunc);
this._next(xt);
};
}

// token function takes more than one argument, so pass in a callback for async
if (tkFunc.length > 1) {
return xt => tkFunc.call(tk, xt.context, () => this._next(xt));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<!--%{#./test/fixtures/string-only}-->
<!--%{#./test/fixtures/return-undefined}-->
<!--%{internal-test}-->
<!--%{non-func-token}-->
<!--%{#./test/fixtures/async-error}-->
<!--%{#./test/fixtures/wants-next}-->
<!--%{#./test/fixtures/return-undefined}-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ describe("react-webapp", function() {
},
tokens: {
"internal-test": () => "\ninternal-test",
"test-not-found": () => "\nnot found"
"test-not-found": () => "\nnot found",
"non-func-token": ""
}
},
defaultReactHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ describe("render-context", function() {
internalHandler = context.getTokenHandler("internal-test-handler");
},
tokens: {
"internal-test": () => `\nbuilt-in for internal-test`
"internal-test": () => `\nbuilt-in for internal-test`,
"non-func-token": "\ntest non-func token"
}
}
});
Expand All @@ -39,6 +40,7 @@ from async ok module
from async error module
from string only module
built-in for internal-test
test non-func token
from async error module
from wants next module<!-- unhandled token test-not-found -->
from string only module
Expand Down

0 comments on commit 839c838

Please sign in to comment.