-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract and consolidate rendering steps execution (#897)
- Loading branch information
Showing
5 changed files
with
130 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"use strict"; | ||
|
||
const Promise = require("bluebird"); | ||
|
||
const { TOKEN_HANDLER } = require("./symbols"); | ||
|
||
const executeSteps = { | ||
STEP_CALLBACK: 0, | ||
STEP_MAYBE_ASYNC: 1, | ||
STEP_STR_TOKEN: 2, | ||
STEP_NO_HANDLER: 3, | ||
STEP_LITERAL_HANDLER: 4 | ||
}; | ||
|
||
const { | ||
STEP_CALLBACK, | ||
STEP_MAYBE_ASYNC, | ||
STEP_STR_TOKEN, | ||
STEP_NO_HANDLER, | ||
STEP_LITERAL_HANDLER | ||
} = executeSteps; | ||
|
||
function renderNext(err, xt) { | ||
const { renderSteps, context } = xt; | ||
if (err) { | ||
// debugger; // eslint-disable-line | ||
context.handleError(err); | ||
} | ||
|
||
if (context.isFullStop || context.isVoidStop || xt.stepIndex >= renderSteps.length) { | ||
xt.next = undefined; | ||
return xt.resolve(context.output.close()); | ||
} else { | ||
// TODO: support soft stop | ||
const step = renderSteps[xt.stepIndex++]; | ||
const tk = step.tk; | ||
switch (step.code) { | ||
case STEP_CALLBACK: | ||
return tk[TOKEN_HANDLER](context, xt.next); | ||
case STEP_MAYBE_ASYNC: | ||
return context.handleTokenResult(tk.id, tk[TOKEN_HANDLER](context), xt.next); | ||
case STEP_STR_TOKEN: | ||
context.output.add(tk.str); | ||
break; | ||
case STEP_NO_HANDLER: | ||
context.output.add(`<!-- unhandled token ${tk.id} -->`); | ||
break; | ||
case STEP_LITERAL_HANDLER: | ||
context.output.add(step.data); | ||
break; | ||
} | ||
return xt.next(); | ||
} | ||
} | ||
|
||
function executeRenderSteps(renderSteps, context) { | ||
return new Promise(resolve => { | ||
const xt = { stepIndex: 0, renderSteps, context, resolve }; | ||
xt.next = err => renderNext(err, xt); | ||
xt.next(); | ||
}); | ||
} | ||
|
||
module.exports = { executeRenderSteps, renderNext, executeSteps }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
"use strict"; | ||
|
||
module.exports = { | ||
TEMPLATE_DIR: Symbol("template dir") | ||
TEMPLATE_DIR: Symbol("template dir"), | ||
TOKEN_HANDLER: Symbol("token handler") | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters