Skip to content

Commit 498cc8a

Browse files
committed
Fix WebReflection#50 - Added uhtml/json export
1 parent a166cb0 commit 498cc8a

4 files changed

Lines changed: 132 additions & 7 deletions

File tree

cjs/json.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
const umap = (m => /* c8 ignore start */ m.__esModule ? m.default : m /* c8 ignore stop */)(require('umap'));
3+
const {render: $render, html, svg} = require('./index.js');
4+
5+
// Sender (SW, Worker, postMessage)
6+
const ids = umap(new WeakMap);
7+
let id = 0;
8+
9+
const tag = type => (template, ...values) => ({
10+
type,
11+
template,
12+
values,
13+
id: ids.get(template) ||
14+
ids.set(template, id++)
15+
});
16+
17+
html.json = tag('html');
18+
svg.json = tag('svg');
19+
20+
// Receiver (onmessage, from SW, Workers, etc)
21+
const templates = umap(new Map);
22+
23+
const unroll = ({type, template, values, id}) => (
24+
(type === 'svg' ? svg : html).apply(
25+
null,
26+
[
27+
templates.get(id) ||
28+
templates.set(id, template)
29+
].concat(values.map(asJSON))
30+
)
31+
);
32+
33+
const asJSON = value => isJSON(value) ? unroll(value) : value;
34+
35+
const isJSON = thing => (
36+
typeof thing === 'object' &&
37+
thing !== null &&
38+
'type' in thing &&
39+
'template' in thing &&
40+
'values' in thing &&
41+
'id' in thing
42+
);
43+
44+
const render = (where, what) => $render(
45+
where,
46+
isJSON(what) ? unroll(what) : what
47+
);
48+
49+
exports.render = render;
50+
exports.html = html;
51+
exports.svg = svg;

esm/json.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import umap from 'umap';
2+
import {render as $render, html, svg} from './index.js';
3+
4+
// Sender (SW, Worker, postMessage)
5+
const ids = umap(new WeakMap);
6+
let id = 0;
7+
8+
const tag = type => (template, ...values) => ({
9+
type,
10+
template,
11+
values,
12+
id: ids.get(template) ||
13+
ids.set(template, id++)
14+
});
15+
16+
html.json = tag('html');
17+
svg.json = tag('svg');
18+
19+
// Receiver (onmessage, from SW, Workers, etc)
20+
const templates = umap(new Map);
21+
22+
const unroll = ({type, template, values, id}) => (
23+
(type === 'svg' ? svg : html).apply(
24+
null,
25+
[
26+
templates.get(id) ||
27+
templates.set(id, template)
28+
].concat(values.map(asJSON))
29+
)
30+
);
31+
32+
const asJSON = value => isJSON(value) ? unroll(value) : value;
33+
34+
const isJSON = thing => (
35+
typeof thing === 'object' &&
36+
thing !== null &&
37+
'type' in thing &&
38+
'template' in thing &&
39+
'values' in thing &&
40+
'id' in thing
41+
);
42+
43+
const render = (where, what) => $render(
44+
where,
45+
isJSON(what) ? unroll(what) : what
46+
);
47+
48+
export {render, html, svg};

package.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@
2424
"license": "ISC",
2525
"devDependencies": {
2626
"@babel/core": "^7.14.6",
27-
"@babel/preset-env": "^7.14.5",
27+
"@babel/preset-env": "^7.14.7",
2828
"@rollup/plugin-babel": "^5.3.0",
2929
"@rollup/plugin-node-resolve": "^13.0.0",
30-
"@ungap/degap": "^0.2.6",
30+
"@ungap/degap": "^0.2.7",
3131
"ascjs": "^5.0.1",
3232
"c8": "^7.7.3",
33-
"coveralls": "^3.1.0",
33+
"coveralls": "^3.1.1",
3434
"drop-babel-typeof": "^1.0.3",
35-
"linkedom": "^0.9.3",
36-
"rollup": "^2.51.2",
35+
"linkedom": "^0.9.7",
36+
"rollup": "^2.52.3",
3737
"rollup-plugin-includepaths": "^0.2.4",
3838
"rollup-plugin-terser": "^7.0.2",
39-
"terser": "^5.7.0"
39+
"terser": "^5.7.1"
4040
},
4141
"dependencies": {
4242
"@ungap/create-content": "^0.3.1",
4343
"async-tag": "^0.2.0",
44-
"jsx2tag": "^0.2.1",
44+
"jsx2tag": "^0.3.0",
4545
"uarray": "^1.0.0",
4646
"udomdiff": "^1.1.0",
4747
"uhandlers": "^0.5.0",
@@ -64,6 +64,10 @@
6464
"import": "./esm/init.js",
6565
"default": "./cjs/init.js"
6666
},
67+
"./json": {
68+
"import": "./esm/json.js",
69+
"default": "./cjs/json.js"
70+
},
6771
"./jsx": {
6872
"import": "./esm/x.js",
6973
"default": "./cjs/x.js"

test/json.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>uhtml/json</title>
8+
<script type="module">
9+
import {render, html} from '../esm/json.js';
10+
const strong = name => html.json`<strong>${name}</strong>`;
11+
const greetings = name => html.json`
12+
<div>Hello ${strong(name)} 👋</div>
13+
`;
14+
15+
console.log(greetings('console'));
16+
render(document.body, greetings('World'));
17+
setTimeout(() => {
18+
render(document.body, greetings('uhtml/json'));
19+
}, 1000);
20+
</script>
21+
</head>
22+
</html>

0 commit comments

Comments
 (0)