Skip to content

Instantly share code, notes, and snippets.

@amirouche
Created March 17, 2017 19:30
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 amirouche/3ff396f0f5ccc9dd0c316add0873fd7d to your computer and use it in GitHub Desktop.
Save amirouche/3ff396f0f5ccc9dd0c316add0873fd7d to your computer and use it in GitHub Desktop.
from functools import wraps
import leloop
def mount(scene, init, view):
state = init()
def spawn(timeout, callback, *args, **kwargs):
# spawn is easier to test that leloop.call_later
leloop.call_later(lambda : yield from callback(*args, **kwargs), timeout)
def make_reducer(func): # called make action in forward project
"""This calls `func` with the current `state` as argument and `spawn`"""
@wraps(func)
def wrapped(*args, **kwargs):
new = func(state, spawn, *args, **kwargs)
if new != state:
state = new
render()
return wrapped
def render():
new = view(state)
new.update_callback(make_reducer)
scene.patch(new)
return new
def change(func, *args, **kwargs):
func(state, spawn, *args, **kwargs)
return make_reducer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment