Skip to content

Instantly share code, notes, and snippets.

@ryasmi
Created March 3, 2018 14:19
Show Gist options
  • Save ryasmi/79749666de2a7d0c59448c9b9bec21ae to your computer and use it in GitHub Desktop.
Save ryasmi/79749666de2a7d0c59448c9b9bec21ae to your computer and use it in GitHub Desktop.
app = async ({count,fraction}) => {
const seconds = Math.floor(count / fraction);
const div = document.createElement('div');
const span = document.createElement('span');
span.innerText = `${seconds}.${count % fraction}`;
div.style.color = 'white'
div.appendChild(span);
return div;
};
client = () => {
const render = (target, elem) => {
target.innerHTML = '';
target.appendChild(elem);
};
const maxSeconds = 4;
const fraction = 100;
const interval = 1000 / fraction;
const maxCount = maxSeconds * fraction;
const renderApp = async (props) => {
render(document.body, await app(props));
};
const update = (count = 0) => {
renderApp({count, fraction});
setTimeout(() => {
if (count < maxCount) {
update(count + 1);
}
}, interval);
};
update();
};
server = async () => {
const render = (elem) => {
elem.outerHTML();
};
console.log(await app({count:0, fraction:100}));
};
app = async ({ getText }) => {
const text = await getText();
const div = document.createElement('div');
div.innerText = text;
div.style.color = 'white'
return div;
};
getText = async () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('hello');
}, 2000);
});
};
render = (target, elem) => {
target.innerHTML = '';
target.appendChild(elem);
};
loader = async (elemPromise) => {
const div = document.createElement('div');
const loading = document.createElement('div');
loading.innerText = 'Loading...';
loading.style.color = 'white';
render(div, loading);
elemPromise.then((elem) => {
render(div, elem);
});
return div;
};
client = () => {
const renderApp = async () => {
render(document.body, await loader(app({ getText })));
};
renderApp();
};
server = async () => {
const render = (elem) => {
elem.outerHTML();
};
console.log(await app({ getText }));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment