Skip to content

Instantly share code, notes, and snippets.

View jooyunghan's full-sized avatar

Jooyung Han jooyunghan

View GitHub Profile
@jooyunghan
jooyunghan / index.html
Created December 11, 2017 09:27
Knock Code Demo
<h1>Knock code : 0113</h1>
<div id="knock-code-2" class="knock">
</div>
function success() {
run(showToast("pass"));
}
function* showToast(message) {
var div = document.createElement("div");
div.className = "toast in";
div.textContent = message;
document.body.appendChild(div);
yield delay(1000);
function* map(values, gf) {
const result = [];
for (let i = 0; i < values.length; i++) {
result.push(yield* gf(values[i], i, values));
}
return result;
}
function* knockCodeHandler(preset) {
while (true) {
function* knockCodeHandler(preset) {
while (true) {
const input = [];
// after read first input
input.push(getCode(yield));
// keep reading input with timeout(1s)
while (true) {
const e = yield timeout(1000);
if (e) {
function coro(g) {
let gId = 0;
wireTimeout(g.next(), gId);
function resume(value) {
wireTimeout(g.next(value), ++gId);
};
function wireTimeout(result, id) {
if (result && result.value instanceof Promise) {
var noop = [];
function stream(arr){
if(arr.length === 0) return noop;
return cons(function (){return arr[0]}, function (){ return stream(arr.slice(1))})
}
function cons(head, tail){
return [head, tail];
}
function createAsyncIterator() {
const promises = [];
this.subscribe({
next(value) {
promises.shift().resolve({ value, done: false });
}
});
return {
next() {
return new Promise((resolve, reject) => {
async function consume(obs) {
for await (const n of obs) {
console.log(n);
if (n >= 5) break;
}
}
consume(Observable.interval(100));
function createAsyncIterator() {
const promises = [];
const values = [];
this.subscribe({
next(value) {
if (promises.length > 0) {
promises.shift().resolve({ value, done: false });
} else {
values.push(value);
function createAsyncIterator() {
const promises = [];
const values = [];
let done = false;
const subscription = this.subscribe({
next(value) {
if (promises.length > 0) {
promises.shift().resolve({ value, done: false });
} else {