Skip to content

Commit

Permalink
support stopping mid render
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Aug 2, 2018
1 parent 55c6eec commit e1d890a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/electrode-react-webapp/lib/async-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class AsyncTemplate {
await r.beforeRender(context);
}

const result = context.skip ? context.result : await this._renderer.render(context);
const result = await this._renderer.render(context);

for (const r of this._afterRenders) {
await r.afterRender(context);
Expand Down
14 changes: 9 additions & 5 deletions packages/electrode-react-webapp/lib/react-webapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ const getStatsPath = utils.getStatsPath;

function makeRouteHandler(routeOptions) {
const userTokenHandlers = [].concat(routeOptions.tokenHandler, routeOptions.tokenHandlers);
const reactTokenHandlers = Path.join(__dirname, "react/token-handlers");

const tokenHandlers =
userTokenHandlers.indexOf(reactTokenHandlers) < 0
? [reactTokenHandlers].concat(userTokenHandlers)
: userTokenHandlers;
let tokenHandlers = userTokenHandlers;

if (!routeOptions.replaceTokenHandlers) {
const reactTokenHandlers = Path.join(__dirname, "react/token-handlers");
tokenHandlers =
userTokenHandlers.indexOf(reactTokenHandlers) < 0
? [reactTokenHandlers].concat(userTokenHandlers)
: userTokenHandlers;
}

const asyncTemplate = new AsyncTemplate({
htmlFile: routeOptions.htmlFile,
Expand Down
6 changes: 3 additions & 3 deletions packages/electrode-react-webapp/lib/react/token-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ module.exports = function setup(handlerContext /* , asyncTemplate */) {
}

const prepareContext = content => {
if (content.render === false || content.html === undefined) {
return context.skipRender(content);
}
// if (content.render === false || content.html === undefined) {
// return context.skipRender(content);
// }

let cspScriptNonce;
let cspStyleNonce;
Expand Down
37 changes: 32 additions & 5 deletions packages/electrode-react-webapp/lib/render-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class RenderContext {
this.asyncTemplate = asyncTemplate;
this._handlersMap = asyncTemplate.handlersMap;
this.transform = x => x;
this._stop = 0;
}

// return a token handler by name
Expand All @@ -34,11 +35,33 @@ class RenderContext {
this.transform = result => transform(result, this);
}

// before render starts, call this with any predetermined result
// to use as the output rather than doing actual render for it
// Basically no rendering will occur.
skipRender(result) {
this.skip = true;
get stop() {
return this._stop;
}

//
// set this any time to fully stop and close rendering
// stop modes:
// 1 - only process string tokens
// 2 - fully stop immediately, no more token processing
// 3 - completely void any render output and stop immediately,
// replace output with result. This only works if output
// is buffered and not streaming out immediately.
//
set stop(f) {
this._stop = f;
}

fullStop() {
this.stop(RenderContext.FULL_STOP);
}

softStop() {
this.stop(RenderContext.SOFT_STOP);
}

voidStop(result) {
this.stop(RenderContext.VOID_STOP);
this.result = result;
}

Expand Down Expand Up @@ -67,4 +90,8 @@ class RenderContext {
}
}

RenderContext.VOID_STOP = 3;
RenderContext.FULL_STOP = 2;
RenderContext.SOFT_STOP = 1;

module.exports = RenderContext;
13 changes: 10 additions & 3 deletions packages/electrode-react-webapp/lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable max-statements */

const Promise = require("bluebird");
const { VOID_STOP, FULL_STOP, SOFT_STOP } = require("./render-context");

class Renderer {
constructor(options) {
Expand Down Expand Up @@ -75,9 +76,15 @@ class Renderer {
}

_next(xt) {
return xt._tokenIndex >= this.renderSteps.length
? xt.resolve(xt.context.output.close())
: this.renderSteps[xt._tokenIndex++](xt);
const stop = xt.context.stop;

if (stop === VOID_STOP || stop === FULL_STOP || xt._tokenIndex >= this.renderSteps.length) {
return xt.resolve(xt.context.output.close());
} else if (stop === SOFT_STOP) {
return false; // TODO: support soft stop
} else {
return this.renderSteps[xt._tokenIndex++](xt);
}
}
}

Expand Down

0 comments on commit e1d890a

Please sign in to comment.