Skip to content

Instantly share code, notes, and snippets.

@maxhoffmann
Last active December 7, 2016 16:58
Show Gist options
  • Save maxhoffmann/9e57817bfa0755489633dab46da9dca7 to your computer and use it in GitHub Desktop.
Save maxhoffmann/9e57817bfa0755489633dab46da9dca7 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
</head>
<body>
<!-- put markup and other contents here -->
</body>
</html>
const logger = () => console.log;
function clicks(broadcast) {
window.addEventListener('click', broadcast('click'));
}
function counter(broadcast) {
let state = 0;
return message => {
if (message.type === 'click') {
state += 1;
broadcast('counter')(state);
}
}
}
const services = [logger, clicks, counter];
buzz(services);
function buzz(services) {
const _bus = [];
const _services = [];
const broadcast = type => data => {
_bus.push({ type, data });
}
setupServices(broadcast);
requestAnimationFrame(run);
function setupServices(broadcast) {
for (const service of services) {
_services.push(service(broadcast))
}
}
function run(time) {
requestAnimationFrame(run);
for (const message of _bus.splice(0, 100)) {
for (const service of _services) {
if (typeof service === 'function') {
service(message);
}
}
}
}
}
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"babel-runtime": "6.18.0"
}
}
'use strict';
var _getIterator2 = require('babel-runtime/core-js/get-iterator');
var _getIterator3 = _interopRequireDefault(_getIterator2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var logger = function logger() {
return console.log;
};
function clicks(broadcast) {
window.addEventListener('click', broadcast('click'));
}
function counter(broadcast) {
var state = 0;
return function (message) {
if (message.type === 'click') {
state += 1;
broadcast('counter')(state);
}
};
}
var services = [logger, clicks, counter];
buzz(services);
function buzz(services) {
var _bus = [];
var _services = [];
var broadcast = function broadcast(type) {
return function (data) {
_bus.push({ type: type, data: data });
};
};
setupServices(broadcast);
requestAnimationFrame(run);
function setupServices(broadcast) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = (0, _getIterator3.default)(services), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var service = _step.value;
_services.push(service(broadcast));
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
function run(time) {
requestAnimationFrame(run);
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = (0, _getIterator3.default)(_bus.splice(0, 100)), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var message = _step2.value;
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = (0, _getIterator3.default)(_services), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var service = _step3.value;
if (typeof service === 'function') {
service(message);
}
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment