Skip to content

Instantly share code, notes, and snippets.

View zdychacek's full-sized avatar

Ondřej Ždych zdychacek

View GitHub Profile
@zdychacek
zdychacek / app.html
Last active February 15, 2017 20:12
Inline Web Worker
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Inline Web Worker</title>
</head>
<body>
</body>
<script type="javascript/worker" id="my-worker">
function coPromise (genFun, ctx) {
return new Promise(function (resolve, reject) {
co(genFun).call(ctx, function (err, data) {
if (err) {
return reject(err);
}
resolve(data);
});
});
@zdychacek
zdychacek / proxy.js
Last active November 11, 2016 11:19
Proxy for remote method calls
// require Proxy API normalization polyfill because current V8's implementation doesn't support standardized API
const Reflect = require('harmony-reflect');
function createProxy (action) {
// create the callable proxy
function _createCallableProxy (name) {
const methodNames = [ name ];
return new Proxy(function () {}, {
@zdychacek
zdychacek / negative_array.js
Last active February 15, 2017 20:13
Negative array using Proxy
function negArray (arr) {
return Proxy.create({
get: function (rcvr, index) {
index = +index;
return arr[index < 0? arr.length + index : index];
},
set: function (rcvr, index, value) {
index = +index;
return arr[index < 0? arr.length + index : index] = value;
@zdychacek
zdychacek / async_calls.js
Last active February 15, 2017 20:15
Async calls with self implemented async function
'use strict';
function async (generator, callback) {
if (generator.constructor.name !== 'GeneratorFunction') {
throw new Error('Function async accept only generator function.');
}
function handleResult (result) {
let resultValue = result.value;
@zdychacek
zdychacek / q_async.js
Last active February 15, 2017 20:15
Q.aync and ES6 generators
Q.async(function* () {
try {
var value1 = yield step1();
var value2 = yield step2(value1);
var value3 = yield step3(value2);
var value4 = yield step4(value3);
} catch (e) {
// Handle any error from step1 through step4
}
});