Skip to content

Commit

Permalink
rename LoadTokenHandler to RegisterTokenIds
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Jul 16, 2020
1 parent f8777d3 commit 1c88de1
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 23 deletions.
5 changes: 3 additions & 2 deletions packages/subapp-server/src/index-page.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* @jsx createElement */

import { IndexPage, createElement, Token, Require, LoadTokenHandler } from "@xarc/jsx-renderer";
import { IndexPage, createElement, Token, Require, RegisterTokenIds } from "@xarc/jsx-renderer";
import { ReserveSpot } from "subapp-web";
import { tokenHandler } from "@xarc/index-page";

const RenderSubApps = (props, context) => {
const { routeOptions } = context.user;
Expand Down Expand Up @@ -42,7 +43,7 @@ const RenderSubApps = (props, context) => {

const Template = (
<IndexPage DOCTYPE="html">
<LoadTokenHandler handler="@xarc/index-page" call="tokenHandler" />
<RegisterTokenIds handler={tokenHandler} />
<Token _id="INITIALIZE" />
<html lang="en">
<head>
Expand Down
4 changes: 3 additions & 1 deletion packages/xarc-jsx-renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@
"munchy": "^1.0.7",
"nyc": "^15.0.0",
"prettier": "^2.0.4",
"run-verify": "^1.2.5",
"sinon": "^7.2.6",
"sinon-chai": "^3.3.0",
"source-map-support": "^0.5.16",
"stream-to-array": "^2.3.0",
"ts-node": "^8.6.2",
"typedoc": "^0.17.4",
"typedoc-plugin-external-module-name": "^3.1.0",
"typescript": "^3.8.3"
"typescript": "^3.8.3",
"xstdout": "^0.1.1"
},
"files": [
"dist",
Expand Down
4 changes: 2 additions & 2 deletions packages/xarc-jsx-renderer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { Token } from "./tags/Token";
import { IndexPage } from "./tags/IndexPage";
import { Require } from "./tags/Require";
import { Literal } from "./tags/Literal";
import { LoadTokenHandler } from "./tags/LoadTokenHandler";
import { RegisterTokenIds } from "./tags/RegisterTokenIds";

/** @ignore */
export { Component, Token, IndexPage, Require, Literal, JsxRenderer, LoadTokenHandler };
export { Component, Token, IndexPage, Require, Literal, JsxRenderer, RegisterTokenIds };

let ELEMENT_ID = 0;

Expand Down
5 changes: 5 additions & 0 deletions packages/xarc-jsx-renderer/src/tags/IndexPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

import { Component } from "../Component";

/**
* Start the template with DOCTYPE tag
*
*
*/
export class IndexPage extends Component {
static memoize(props) {
return `<!DOCTYPE ${props.DOCTYPE || "html"}>`;
Expand Down
6 changes: 6 additions & 0 deletions packages/xarc-jsx-renderer/src/tags/Literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import * as _ from "lodash";

const MEMOIZE = {};

/**
* Load literal data from a file and insert into the output
*
* @param props
* @param _context
*/
export function Literal(props: any, _context: any) {
// const renderer = context.asyncTemplate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { JsxRenderer } from "../JsxRenderer";
*
* Usage: (*This is a JSX tag, don't call the function directly.*)
*
* - With function: `<LoadTokenHandler name="handler1" handler={() => {...}}`
* - Load from source file: `<LoadTokenHandler handler="./token-handlers" />`
* - With function: `<RegisterTokenIds name="handler1" handler={() => {...}}`
* - Load from source file: `<RegisterTokenIds handler="./token-handlers" />`
* - If the handler string starts with `"."` then it will be resolved to the full
* path relative to the template's location, else it's a module for require.
* - The full path of the handler will be used as `name`, but you can provide
Expand All @@ -24,10 +24,10 @@ import { JsxRenderer } from "../JsxRenderer";
* Example:
*
* ```js
* import { IndexPage, Token, LoadTokenHandler } from "@xarc/jsx-renderer"
* import { IndexPage, Token, RegisterTokenIds } from "@xarc/jsx-renderer"
*
* export const Template = () => (<IndexPage>
* <LoadTokenHandler name="handler1" handler={setupContext => {
* <RegisterTokenIds name="handler1" handler={setupContext => {
* return {
* TOKEN1: context => {
* return "result from TOKEN1"
Expand All @@ -41,7 +41,7 @@ import { JsxRenderer } from "../JsxRenderer";
* @param props - JSX tag props
* @param context - rendering context
*/
export const LoadTokenHandler = (props, context) => {
export const RegisterTokenIds = (props, context) => {
const renderer: JsxRenderer = context.asyncTemplate;
renderer.registerTokenHandler(props.name, props.handler, props.call);
};
7 changes: 7 additions & 0 deletions packages/xarc-jsx-renderer/src/tags/Require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

import { processToken } from "../process-token";

/**
* Require and invoke a token module with process handler
*
* @param props
* @param context
* @param scope
*/
export function Require(props: any, context: any, scope: any) {
return processToken(props, context, scope, true);
}
7 changes: 7 additions & 0 deletions packages/xarc-jsx-renderer/src/tags/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

import { processToken } from "../process-token";

/**
* Invoke a token by its ID, which can be registered with RegisterTokenIds.
*
* @param props
* @param context
* @param scope
*/
export function Token(props: any, context: any, scope: any) {
return processToken(props, context, scope);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
Require,
Literal,
Component,
LoadTokenHandler
RegisterTokenIds
} from "../../src";

const Template = () => {
return (
<IndexPage DOCTYPE="html">
<LoadTokenHandler
<RegisterTokenIds
name="test1"
handler={() => {
return {
Expand All @@ -22,11 +22,11 @@ const Template = () => {
}}
/>
<Token _id="FOO" />
<LoadTokenHandler handler="../fixtures/token-handler" />
<RegisterTokenIds handler="../fixtures/token-handler" />
<Token _id="user-token-1" />
<LoadTokenHandler name="name_with_call" call="handler2" handler="../fixtures/token-handler" />
<RegisterTokenIds name="name_with_call" call="handler2" handler="../fixtures/token-handler" />
{/* Test re-entry */}
<LoadTokenHandler name="name_with_call" call="handler2" handler="../fixtures/token-handler" />
<RegisterTokenIds name="name_with_call" call="handler2" handler="../fixtures/token-handler" />
<Token _id="TOKEN_HANDLER2" />
</IndexPage>
);
Expand Down

This file was deleted.

22 changes: 15 additions & 7 deletions packages/xarc-jsx-renderer/test/spec/jsx-renderer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import Template7 from "../jsx-templates/test7";
import Template8 from "../jsx-templates/test8";
import Template9 from "../jsx-templates/test9";
import Template91 from "../jsx-templates/test91";
import TemplateLoadTokenHandler from "../jsx-templates/test-load-token-handler";
import TemplateRegisterTokenIds from "../jsx-templates/test-register-token-ids";
import * as xstdout from "xstdout";
import { asyncVerify, runFinally } from "run-verify";

describe("IndexPage", function () {
it("should have static memoize", () => {
Expand Down Expand Up @@ -167,11 +169,17 @@ describe("Jsx Renderer", function () {

renderer.initializeRenderer();

const promise = renderer.render({});
return promise.then(context => {
verify(context);
return renderer.render({}).then(verify);
});
const intercept = xstdout.intercept(true);

return asyncVerify(
() => renderer.render({}),
verify,
() => renderer.render({}),
verify,
runFinally(() => {
intercept.restore();
})
);
});

it("should handle failure in nesting async components", async () => {
Expand Down Expand Up @@ -387,7 +395,7 @@ World`
const renderer = new JsxRenderer({
insertTokenIds: true,
templateFullPath: Path.dirname(require.resolve("../jsx-templates/test91")),
template: TemplateLoadTokenHandler
template: TemplateRegisterTokenIds
});

renderer.initializeRenderer();
Expand Down

0 comments on commit 1c88de1

Please sign in to comment.