Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created March 26, 2021 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/573f8eee31e3770e45d5879b580f313b to your computer and use it in GitHub Desktop.
Save barneycarroll/573f8eee31e3770e45d5879b580f313b to your computer and use it in GitHub Desktop.
What if we could yield & await as discrete mechanisms for stepping through lifecycle?
// Async generators don't actually work this way: all yields implictly mandate asynchronous (non-synchronously-blocking) resolution under the hood; so in practice it's probably better to be honest and make this unambiguously wasteful & imprecise and have all steps as explicit awaits. 🙄
const KitchenSink = steps(async function * ($){
$.view(v =>
m('p.counter', $.attrs.value, '!')
)
yield $.create
style({
transition : 'none',
opacity : 0,
})
yield $.layout
style({
transition : 'opacity 1s ease-in-out',
opacity : 1,
})
let previous = $.attrs.value
$.attrs.map(({value}) => {
const colour = (
value > previous ? 'green' :
value < previous ? 'red' :
'grey'
)
previous = value
$.dom.animate({
color : [color, 'unset'],
}, {
duration : 500,
easing : 'ease-out',
})
})
await $.onbeforeremove
style({opacity: 0})
await new Promise(done => {
$.dom.addEventListener('transitionend', done)
})
function style(styles){
Object.assign($.dom.style, styles)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment