Skip to content

Commit

Permalink
update to current API
Browse files Browse the repository at this point in the history
This commit was sponsored by Jason Mills, hacklschorsch, Jason Walker,
and my other patrons.  If you want to join them, you can support my
work at https://glyph.im/patrons/.
  • Loading branch information
glyph committed Jul 28, 2024
1 parent 195a176 commit 0018fd2
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions docs/examples/coffee_expanded.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,21 @@ def ready_factory(
return Ready(beans, water, carafe)

def mixture_factory(brewer: _BrewerInternals, core: BrewCore) -> Mixture:
# we already do have a 'ready' but it's state-specific data which makes
# We already do have a 'ready' but it's State-Specific Data which makes
# it really annoying to relay on to the *next* state without passing it
# through the state core. requiring the factory to take SSD inherently
# means that it could only work with transitions away from a single
# state, which would not be helpful, although that *is* what we want here.
return None # type:ignore[return-value]
# state, which would not be helpful, although that *is* what we want
# here.

ready = builder.data_state("Ready", ready_factory)
brewing = builder.data_state("Brewing", mixture_factory)
assert core.beans is not None
assert core.water is not None
assert core.carafe is not None

return Mixture(core.beans, core.water)

ready = builder.state("Ready", ready_factory)
brewing = builder.state("Brewing", mixture_factory)

def ready_check(brewer: _BrewerInternals, core: BrewCore) -> None:
if (
Expand All @@ -117,23 +123,22 @@ def ready_check(brewer: _BrewerInternals, core: BrewCore) -> None:
):
brewer._ready(core.beans, core.water, core.carafe)

@not_ready.transition(Brewer.put_in_beans, not_ready)
@not_ready.loop().upon(Brewer.put_in_beans)
def put_beans(brewer: _BrewerInternals, core: BrewCore, beans: Beans) -> None:
core.beans = beans
ready_check(brewer, core)

@not_ready.transition(Brewer.put_in_water, not_ready)
@not_ready.loop().upon(Brewer.put_in_water)
def put_water(brewer: _BrewerInternals, core: BrewCore, water: Water) -> None:
core.water = water
ready_check(brewer, core)

@not_ready.transition(Brewer.put_in_carafe, not_ready)
@not_ready.loop().upon(Brewer.put_in_carafe)
def put_carafe(brewer: _BrewerInternals, core: BrewCore, carafe: Carafe) -> None:
core.carafe = carafe
ready_check(brewer, core)

@not_ready.to(ready).upon(_BrewerInternals._ready)
@not_ready.transition(_BrewerInternals._ready, ready)
def get_ready(
brewer: _BrewerInternals,
core: BrewCore,
Expand All @@ -156,13 +161,13 @@ def not_ready_anymore(
) -> None:
core.ready_light.on = False

@ready.transition(Brewer.brew_button, brewing)
@ready.to(brewing).upon(Brewer.brew_button)
def brew(brewer: _BrewerInternals, core: BrewCore, ready: Ready) -> None:
core.brew_light.on = True
print("BREW CALLED")
core.brewing = ready.brew()

@brewing.transition(_BrewerInternals.wait_a_while, not_ready)
@brewing.to(not_ready).upon(_BrewerInternals.wait_a_while)
def brewed(brewer: _BrewerInternals, core: BrewCore, mixture: Mixture) -> Mixture:
core.brew_light.on = False
return mixture
Expand Down

0 comments on commit 0018fd2

Please sign in to comment.