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

Add Hyperapp #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion benchmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var enabledBenchmarks = null;
enabledLibs = {
marko: true,
preact: true,
hyperapp: true,
react: true,
vue: true,
inferno: true
Expand Down Expand Up @@ -75,4 +76,4 @@ Object.keys(enabledBenchmarks).forEach((benchmarkName) => {
});
});

module.exports = benchmarks;
module.exports = benchmarks;
8 changes: 8 additions & 0 deletions benchmarks/color-picker/hyperapp/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": [
["es2015", { "loose": true, "modules": false }]
],
"plugins": [
["transform-react-jsx", { "pragma": "h" }]
]
}
29 changes: 29 additions & 0 deletions benchmarks/color-picker/hyperapp/client.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { app } = require('hyperapp');
const { state, actions, view } = require('./components/App');

const mountNode = document.getElementById('mount');

if (mountNode) {
const initialState = Object.assign({}, state, { colors: window.colors });
app(initialState, actions, view, mountNode);
console.log('Re-rendering on client completed');
}

window.addBench('hyperapp', (el, colors) => {
const initialState = Object.assign({}, state, { colors });

let currentDone;
const appActions = Object.assign({}, actions, {
onUpdate() {
currentDone();
}
});

const widget = app(initialState, appActions, view, el);

let selectedColorIndex = 0;
return (done) => {
widget.setColorIndex((++selectedColorIndex) % colors.length);
currentDone = done;
};
});
54 changes: 54 additions & 0 deletions benchmarks/color-picker/hyperapp/components/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { h } = require('hyperapp');

const state = {
selectedColorIndex: 0,
colors: [],
};

const actions = {
setColorIndex(selectedColorIndex) {
return { selectedColorIndex };
},
onUpdate() {
// for benchmark
},
};

function renderColors(state, actions) {
if (state.colors.length) {
return (
<ul>
{state.colors.map((color, index) => (
<li
className={`color${state.selectedColorIndex === index ? ' selected' : ''}`}
style={{ backgroundColor: color.hex }}
onclick={() => actions.setColorIndex(index)
}>
{color.name}
</li>
))}
</ul>
);
} else {
return <div>No colors!</div>
}
}

function view(state, actions) {
const currentColor = state.colors[state.selectedColorIndex];
return (
<div class="colors" onupdate={actions.onUpdate}>
<h1>Choose your favorite color:</h1>
<div class="colors">
{renderColors(state, actions)}
</div>
<div>You chose: <div class="chosen-color">{currentColor.name}</div></div>
</div>
);
}

module.exports = {
state,
actions,
view,
};
23 changes: 23 additions & 0 deletions benchmarks/color-picker/hyperapp/page.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import serverRender from './util/serverRender';
import App from './components/App';

<include(input.pageLayout)>
<@body>
$ var renderedHTML = serverRender(App, input.colors);
<div id="mount">$!{renderedHTML}</div>
<!--
In order for behavior to be attached to the
React UI components rendered on the server, we must re-render the
SearchResults UI component _again_ on the client using the exact same
data that we used to render it on the server.
This means that we have to go through the expensive step of
serializing the data to JSON and add the data to the page (increases
the page weight). The re-rendering on the client happens in "./client.js"
and it uses the data assigned to the global variable produced
by the following inline script:
-->
<script>
window.colors=${JSON.stringify(input.colors)};
</script>
</@body>
</include>
25 changes: 25 additions & 0 deletions benchmarks/color-picker/hyperapp/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import commonjsPlugin from 'rollup-plugin-commonjs';
import browserifyPlugin from 'rollup-plugin-browserify-transform';
import nodeResolvePlugin from 'rollup-plugin-node-resolve';
import babelPlugin from 'rollup-plugin-babel';
import envify from 'envify';
import path from 'path';

process.env.NODE_ENV = 'production';

export default {
input: path.resolve(__dirname, 'client.jsx'),
output: {
format: 'iife',
file: path.join(process.env.BUNDLES_DIR, 'hyperapp.js')
},
mame: 'app',
plugins: [
babelPlugin({
exclude: 'node_modules/**'
}),
browserifyPlugin(envify),
nodeResolvePlugin(),
commonjsPlugin()
],
};
10 changes: 10 additions & 0 deletions benchmarks/color-picker/hyperapp/server.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { app } = require('hyperapp');
const { renderToString } = require('hyperapp-render');
const { state, actions, view } = require('./components/App');

module.exports = function(colors) {
return function benchFn() {
const initialState = Object.assign({}, state, { colors });
return renderToString(view(initialState, actions));
};
};
6 changes: 6 additions & 0 deletions benchmarks/color-picker/hyperapp/util/serverRender.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { renderToString } = require('hyperapp-render');

module.exports = function infernoRender(App, colors) {
const state = Object.assign({}, App.state, { colors });
return renderToString(App.view(state, App.actions));
};
8 changes: 8 additions & 0 deletions benchmarks/search-results/hyperapp/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": [
["es2015", { "loose": true, "modules": false }]
],
"plugins": [
["transform-react-jsx", { "pragma": "h" }]
]
}
30 changes: 30 additions & 0 deletions benchmarks/search-results/hyperapp/client.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { app } = require('hyperapp');
const { state, actions, view } = require('./components/App');

const mountNode = document.getElementById('searchResultsMount');

if (mountNode) {
const initialState = Object.assign({}, state, { colors: window.searchResultsData });
app(initialState, actions, view, mountNode);
console.log('Re-rendering on client completed');
}

window.addBench('hyperapp', (el, getNextSearchResults) => {
const initialState = Object.assign({}, state, {
getNextSearchResults: getNextSearchResults(),
});

let currentDone;
const appActions = Object.assign({}, actions, {
onUpdate() {
currentDone();
}
});

const widget = app(initialState, appActions, view, el);

return (done) => {
widget.setSearchResults(getNextSearchResults());
currentDone = done;
};
});
50 changes: 50 additions & 0 deletions benchmarks/search-results/hyperapp/components/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { h } = require('hyperapp');
const Footer = require('./Footer');
const SearchResultsItem = require('./SearchResultsItem');

const state = {
searchResultsData: {
items: [],
},
purchased: {
// id-1: true,
// id-2: false
},
};

const actions = {
setSearchResults(searchResultsData) {
return { searchResultsData };
},
onUpdate() {
// for benchmark
},
purchased: {
buy(id) {
return { [id]: true };
}
}
};

function view(state, actions) {
return (
<div class="search-results">
<div>
{state.searchResultsData.items.map((item) => (
<SearchResultsItem
item={item}
purchased={state.purchased[item.id]}
buy={actions.purchased.buy}
/>
))}
</div>
<Footer/>
</div>
);
}

module.exports = {
state,
actions,
view,
};
Loading