Skip to content

Commit

Permalink
new tag-renderer base on ES6 strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Jul 16, 2020
1 parent 6a06efb commit b35ae20
Show file tree
Hide file tree
Showing 24 changed files with 844 additions and 878 deletions.
2 changes: 1 addition & 1 deletion packages/xarc-render-context/src/RenderContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class RenderContext {
*/
intercept({ responseHandler }) {
this._intercepted = { responseHandler };
throw new Error("electrode-react-webapp: render-context - user intercepted");
throw new Error("@xarc/render-context: user intercepted response");
}

fullStop() {
Expand Down
16 changes: 12 additions & 4 deletions packages/xarc-render-context/src/TokenModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class TokenModule {
wantsNext: any;
props: any;
_modCall: any;
_tokenMod: any;

constructor(id, pos, props, templateDir) {
this.id = id;
Expand Down Expand Up @@ -43,10 +44,14 @@ export class TokenModule {
this[TEMPLATE_DIR] = this.props[TEMPLATE_DIR] || templateDir || process.cwd();
}

set tokenMod(tm) {
this._tokenMod = tm;
}

// if token is a module, then load it
load(options = {}) {
load(options: any = {}) {
if (!this.isModule || this.custom !== undefined) return;
let tokenMod = viewTokenModules[this.id];
let tokenMod = this._tokenMod || viewTokenModules[this.id];

if (tokenMod === undefined) {
if (this._modCall) {
Expand All @@ -56,20 +61,23 @@ export class TokenModule {
}
viewTokenModules[this.id] = tokenMod;
}

if (this._modCall) {
// call setup function to get an instance
const params = [options, this].concat(this._modCall[1] || []);
assert(
tokenMod[this._modCall[0]],
`electrode-react-webapp: _call of token ${this.id} - '${this._modCall[0]}' not found`
`@xarc/render-context: _call of token ${this.id} - '${this._modCall[0]}' not found`
);
this.custom = tokenMod[this._modCall[0]](...params);
} else {
this.custom = tokenMod(options, this);
}

/* if token doesn't provide any code (null) then there's no handler to set for it */
if (this.custom === null) return;
if (this.custom === null) {
return;
}

assert(
this.custom && this.custom.process,
Expand Down
5 changes: 4 additions & 1 deletion packages/xarc-render-context/src/load-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const failLoadTokenModule = (msg: string, err: Error) => {
};

const notFoundLoadTokenModule = (msg: string, err: Error) => {
console.error(`error: @xarc/render-context can't find token process module ${msg}`, err);
console.error(
`error: @xarc/render-context can't find token process module ${msg}`,
err
);
return () => ({
process: () => `\n@xarc/render-context: token process module ${msg} not found\n`
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe("token handler in render context", function () {
responseHandler: resp => resp
});
} catch (e) {
expect(e.message).to.equal("electrode-react-webapp: render-context - user intercepted");
expect(e.message).to.equal("@xarc/render-context: user intercepted response");
}
});

Expand Down
47 changes: 25 additions & 22 deletions packages/xarc-render-context/test/spec/token-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,39 @@ describe("TokenModule ", function () {

expect(tk[TEMPLATE_DIR]).to.equal(process.cwd()); //deep.equal({});
});

it("should allow explicitly setting the token module", () => {
const tk = new TokenModule("#testSetModule", 0, null, null);
tk.tokenMod = () => null;
expect(tk.custom).equal(undefined);
tk.load();
expect(tk.custom).equal(null);
});

it("should handle token module that's null (no handler)", function () {
const tk = new TokenModule("require(./token-module-null.ts)", 0, {}, templateDir);
tk.load();
expect(tk.custom).equal(null);
});
});

describe("require from modPath", function () {
const tk = new TokenModule("require(./token-module-01.ts)", 0, {}, templateDir);

it("should load as modPath if token.id contains string 'require'", function () {
tk.load({});
expect(tk[TOKEN_HANDLER]()).to.equal("hello from token 01");
});

it("should load as modPath if token.id contains string 'require'", function () {
tk.load({});
expect(tk[TOKEN_HANDLER]()).to.equal("hello from token 01");
});

it("should have same result when called with load()", function () {
tk.load();
expect(tk[TOKEN_HANDLER]()).to.equal("hello from token 01");
});
});

describe("_call in options ", function () {
Expand Down Expand Up @@ -106,29 +131,7 @@ describe("_call in options ", function () {
expect(tk[TEMPLATE_DIR]).to.equal(process.cwd()); //deep.equal({});
tk.load(null);
});
});

describe("require from modPath", function () {
const tk = new TokenModule("require(./token-module-01.ts)", 0, {}, templateDir);

it("should load as modPath if token.id contains string 'require'", function () {
tk.load({});
expect(tk[TOKEN_HANDLER]()).to.equal("hello from token 01");
});

it("should have same result when called with load()", function () {
tk.load();
expect(tk[TOKEN_HANDLER]()).to.equal("hello from token 01");
});
});

it("should handle token module that's null (no handler)", function () {
const tk = new TokenModule("require(./token-module-null.ts)", 0, {}, templateDir);
tk.load();
expect(tk.custom).equal(null);
});

describe("_call in options ", function () {
it("should load custom _modCall", function () {
const tk = new TokenModule("#./custom-call.ts", 0, { _call: "setup" }, templateDir);
expect(tk.props).to.deep.equal({ _call: "setup" });
Expand Down
30 changes: 0 additions & 30 deletions packages/xarc-tag-renderer/config/test/setup.js

This file was deleted.

17 changes: 8 additions & 9 deletions packages/xarc-tag-renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"keywords": [],
"author": "Electrode <https://github.com/electrode-io/electrode>",
"license": "Apache-2.0",
"dependencies": {
"@xarc/render-context": "^1.0.0"
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@types/chai": "^4.2.11",
Expand All @@ -20,7 +23,7 @@
"@types/sinon-chai": "^3.2.4",
"@typescript-eslint/eslint-plugin": "^2.21.0",
"@typescript-eslint/parser": "^2.21.0",
"@xarc/module-dev": "^2.1.1",
"@xarc/module-dev": "^2.1.2",
"babel-eslint": "^10.1.0",
"chai": "^4.2.0",
"eslint": "^6.8.0",
Expand All @@ -35,12 +38,10 @@
"ts-node": "^8.6.2",
"typedoc": "^0.17.4",
"typescript": "^3.8.3",
"xstdout": "^0.1.1",
"@xarc/render-context": "^1.0.0"
"xstdout": "^0.1.1"
},
"mocha": {
"require": [
"@babel/register",
"ts-node/register",
"source-map-support/register",
"@xarc/module-dev/config/test/setup.js"
Expand All @@ -51,9 +52,6 @@
"dist",
"src"
],
"dependencies": {
"ts-node": "^8.10.2"
},
"nyc": {
"extends": [
"@istanbuljs/nyc-config-typescript"
Expand Down Expand Up @@ -81,8 +79,9 @@
"cache": false
},
"fyn": {
"devDependencies": {
"dependencies": {
"@xarc/render-context": "../xarc-render-context"
}
},
"devDependencies": {}
}
}
7 changes: 7 additions & 0 deletions packages/xarc-tag-renderer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export { TagRenderer } from "./tag-renderer";
export {
TagTemplate,
createTemplateTags,
RegisterTokenIds,
Token,
TokenInvoke
} from "./tag-template";
44 changes: 30 additions & 14 deletions packages/xarc-tag-renderer/src/render-execute.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
/* eslint-disable complexity */
/* eslint-disable complexity, max-statements */

import { TOKEN_HANDLER } from "@xarc/render-context";

const executeSteps = {
export const executeSteps = {
STEP_HANDLER: 0,
STEP_STR_TOKEN: 1,
STEP_NO_HANDLER: 2,
STEP_LITERAL_HANDLER: 3
STEP_LITERAL_HANDLER: 3,
STEP_FUNC_HANDLER: 4
};

const { STEP_HANDLER, STEP_STR_TOKEN, STEP_NO_HANDLER, STEP_LITERAL_HANDLER } = executeSteps;
const {
STEP_HANDLER,
STEP_STR_TOKEN,
STEP_NO_HANDLER,
STEP_LITERAL_HANDLER,
STEP_FUNC_HANDLER
} = executeSteps;

function renderNext(err: Error, xt) {
export function renderNext(err: Error, xt) {
const { renderSteps, context } = xt;
if (err) {
context.handleError(err);
Expand All @@ -35,34 +42,43 @@ function renderNext(err: Error, xt) {
const tk = step.tk;
const withId = step.insertTokenId;
switch (step.code) {
case STEP_FUNC_HANDLER:
return context.handleTokenResult("", tk.func(context), e => {
return renderNext(e, xt);
});
case STEP_HANDLER:
if (withId) insertTokenId(tk);
if (withId) {
insertTokenId(tk);
}
return context.handleTokenResult(tk.id, tk[TOKEN_HANDLER](context, tk), e => {
if (withId) insertTokenIdEnd(tk);
if (withId) {
insertTokenIdEnd(tk);
}
return renderNext(e, xt);
});
case STEP_STR_TOKEN:
context.output.add(tk.str);
break;
case STEP_NO_HANDLER:
context.output.add(`<!-- unhandled token ${tk.id} -->`);
context.output.add(`<!-- unhandled token ${tk.id} -->\n`);
break;
case STEP_LITERAL_HANDLER:
if (withId) insertTokenId(tk);
if (withId) {
insertTokenId(tk);
}
context.output.add(step.data);
if (withId) insertTokenIdEnd(tk);
if (withId) {
insertTokenIdEnd(tk);
}
break;
}
return renderNext(null, xt);
}
}

function executeRenderSteps(renderSteps, context) {
export function executeRenderSteps(renderSteps, context) {
return new Promise(resolve => {
const xt = { stepIndex: 0, renderSteps, context, resolve };
return renderNext(null, xt);
});
}

const RenderExecute = { executeRenderSteps, renderNext, executeSteps };
export default RenderExecute;
Loading

0 comments on commit b35ae20

Please sign in to comment.