Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support comments and _call prop for tokens #809

Merged
merged 1 commit into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/electrode-react-webapp/lib/async-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class AsyncTemplate {
const pos = template.indexOf(tokenOpenTag, pt);
if (pos >= pt) {
const str = template.substring(pt, pos).trim();
// if there are text between a close tag and an open tag, then consider
// that as plain HTML string
if (str) tokens.push({ str });

const ex = template.indexOf(tokenCloseTag, pos);
Expand All @@ -81,7 +83,8 @@ class AsyncTemplate {
.trim()
.split("\n")
.map(x => x.trim())
.filter(x => x)
// remove empty and comment lines that start with "//"
.filter(x => x && !x.startsWith("//"))
.join(" ");

const token = remain.split(" ", 1)[0];
Expand Down
16 changes: 15 additions & 1 deletion packages/electrode-react-webapp/lib/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Token {
this.custom = undefined;
this.wantsNext = undefined;
this.props = props || {};
if (this.props._call) {
this._modCall = [].concat(this.props._call);
}
}

// if token is a module, then load it
Expand All @@ -29,7 +32,18 @@ class Token {
viewTokenModules[this.id] = tokenMod;
}

this.custom = tokenMod(options || {}); // call setup function to get an instance
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`
);
this.custom = tokenMod[this._modCall[0]].apply(undefined, params);
} else {
this.custom = tokenMod(options || {}, this);
}

assert(
this.custom && this.custom.process,
`custom token ${this.id} module doesn't have process method`
Expand Down
4 changes: 4 additions & 0 deletions packages/electrode-react-webapp/test/data/template3.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@
"test": [1,2,3]
}
}-->
<!--%{
#./test/fixtures/custom-call
_call="setup"
}-->
14 changes: 14 additions & 0 deletions packages/electrode-react-webapp/test/fixtures/custom-call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

function setup() {
return {
name: "custom-call",
process: function() {
return `_call`;
}
};
}

module.exports = {
setup
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const AsyncTemplate = require("../../lib/async-template");
const Path = require("path");
const expect = require("chai").expect;
const _ = require("lodash");

describe("AsyncTemplate._parseTemplate", function() {
it("should parse template into tokens", () => {
Expand Down Expand Up @@ -211,8 +212,23 @@ describe("AsyncTemplate._parseTemplate", function() {
test: [1, 2, 3]
},
wantsNext: undefined
},
{
_modCall: ["setup"],
custom: {
name: "custom-call"
},
id: "#./test/fixtures/custom-call",
isModule: true,
pos: 364,
props: {
_call: "setup"
},
wantsNext: false
}
];
expect(typeof _.last(asyncTemplate.tokens).custom.process).to.equal("function");
delete _.last(asyncTemplate.tokens).custom.process;
expect(asyncTemplate.tokens).to.deep.equal(expected);
});

Expand Down
8 changes: 8 additions & 0 deletions packages/electrode-react-webapp/test/spec/token.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ describe("token", function() {
expect(tk.custom).to.equal(undefined);
});

it("should invoke _call of a module", () => {
const tk = new Token("#./test/fixtures/custom-call", 0, { _call: "setup" });
expect(tk.id).to.equal("#./test/fixtures/custom-call");
expect(tk.isModule).to.equal(true);
tk.load();
expect(tk.process()).to.equal("_call");
});

it("should create token as custom", () => {
const tk = new Token("#./test/fixtures/custom-count");
expect(tk.id).to.equal("#./test/fixtures/custom-count");
Expand Down