Example #1
0
// Unlocked represents the unlocked state receiving coins.
func (m *lockMachine) Unlocked(ctx cells.Cell, event cells.Event) (behaviors.FSMState, error) {
	switch event.Topic() {
	case "cents?":
		return m.Unlocked, event.Respond(m.cents)
	case "info?":
		info := fmt.Sprintf("state 'unlocked' with %d cents", m.cents)
		return m.Unlocked, event.Respond(info)
	case "coin!":
		cents := payloadCents(event)
		ctx.EmitNewContext("return", cents, event.Context())
		return m.Unlocked, nil
	case "button-press!":
		ctx.Environment().EmitNewContext("restorer", "drop!", m.cents, event.Context())
		m.cents = 0
		return m.Locked, nil
	}
	return m.Unlocked, fmt.Errorf("illegal topic in state 'unlocked': %s", event.Topic())
}