Created
March 26, 2021 15:26
-
-
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?
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
// 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