Created
November 30, 2018 06:28
-
-
Save kriskowal/e79e96e712c549c516eb6ddee9fb2444 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
idle.then(series([ | |
delay(1000), | |
() => console.log('A'), | |
parallel([ | |
series([delay(333), () => console.log('1/3')]), | |
series([delay(666), () => console.log('2/3')]), | |
series([delay(999), () => console.log('3/3')]), | |
]), | |
delay(1000), | |
() => console.log('B'), | |
])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var raf = require('raf'); | |
const noop = () => {}; | |
const Idle = (value) => ({ | |
then: (action) => action(value) || idle, | |
}); | |
const idle = Idle(); | |
const Wait = () => { | |
let next, action; | |
return { | |
resume(_next) { | |
if (next) { | |
return; | |
} | |
next = _next || idle; | |
if (action) { | |
next.then(action); | |
} | |
}, | |
then(_action) { | |
if (action) { | |
return; | |
} | |
let wait = Wait(); | |
action = (value) => wait.resume(_action(value) || idle); | |
if (next) { | |
next.then(action); | |
} | |
return wait; | |
}, | |
}; | |
}; | |
const delay = (duration) => (value) => { | |
let wait = Wait(); | |
setTimeout(() => wait.resume(Idle(value)), duration); | |
return wait; | |
}; | |
const series = (actions) => (value) => { | |
return actions.reduce((tail, action) => tail.then(action) || idle, Idle(value)); | |
}; | |
const parallel = (actions) => (value) => { | |
return all(actions.map((action) => action(value))); | |
}; | |
const all = (tails) => { | |
if (tails.length === 0) { | |
return idle; | |
} | |
var wait = Wait(); | |
var group = WaitGroup(wait.resume, tails.length); | |
tails.forEach((tail) => tail.then(group)); | |
return wait; | |
} | |
const WaitGroup = (resume, count) => () => { | |
count--; | |
if (count == 0) { | |
resume(); | |
} | |
}; | |
const addClass = (element, className) => () => element.classList.add(className); | |
const removeClass = (element, className) => () => element.classList.remove(className); | |
const awaitTransitionEnd = (element, debugName) => () => { | |
let wait = Wait(); | |
let timeoutHandle; | |
let handleEvent = () => { | |
clearTimeout(timeoutHandle); | |
element.removeListener('transitionend', handleEvent); | |
wait.resume(); | |
}; | |
element.addEventListener('transitionend', handleEvent); | |
timeoutHandle = setTimeout(handleEvent); | |
return wait; | |
}; | |
const awaitDraw = () => { | |
let wait = Wait(); | |
raf(wait.resume); | |
return wait; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment