Skip to content

Instantly share code, notes, and snippets.

@awto
Last active January 6, 2018 11:52
Show Gist options
  • Select an option

  • Save awto/d415b69ef089c6b842ae367fe348919c to your computer and use it in GitHub Desktop.

Select an option

Save awto/d415b69ef089c6b842ae367fe348919c to your computer and use it in GitHub Desktop.
decoupling article - main no generators
function main(state) {
for(;;) {
switch(state.control) {
case "init":
state.action = "read"
state.control = "loop1"
return
case "loop1":
const i = state.value
if (i.type === "pointerdown") {
const element = state.element = i.target.closest(".draggable")
if (element) {
const box = element.getBoundingClientRect()
state.x = box.x + window.pageXOffset - i.x
state.y = box.y + + window.pageYOffset - i.y
state.control = "loop2"
state.action = "read"
return
}
}
state.control = "loop1"
state.action = "yield"
state.value = i
return
case "loop2":
const j = state.value
if (j.type === "pointerup") {
state.control = "loop1"
break
}
if (j.type === "pointermove") {
state.element.style.left = `${j.x + state.x}px`
state.element.style.top = `${j.y + state.y}px`
}
state.action = "yield"
state.control = "loop1"
state.value = j
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment