当前位置: 首页>>代码示例>>Golang>>正文


Golang Event.Respond方法代码示例

本文整理汇总了Golang中github.com/tideland/gocells/cells.Event.Respond方法的典型用法代码示例。如果您正苦于以下问题:Golang Event.Respond方法的具体用法?Golang Event.Respond怎么用?Golang Event.Respond使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/tideland/gocells/cells.Event的用法示例。


在下文中一共展示了Event.Respond方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Locked

// Locked represents the locked state receiving coins.
func (m *lockMachine) Locked(ctx cells.Cell, event cells.Event) (behaviors.FSMState, error) {
	switch event.Topic() {
	case "cents?":
		return m.Locked, event.Respond(m.cents)
	case "info?":
		info := fmt.Sprintf("state 'locked' with %d cents", m.cents)
		return m.Locked, event.Respond(info)
	case "coin!":
		cents := payloadCents(event)
		if cents < 1 {
			return nil, fmt.Errorf("do not insert buttons")
		}
		m.cents += cents
		if m.cents > 100 {
			m.cents -= 100
			return m.Unlocked, nil
		}
		return m.Locked, nil
	case "button-press!":
		if m.cents > 0 {
			ctx.Environment().EmitNewContext("restorer", "drop!", m.cents, event.Context())
			m.cents = 0
		}
		return m.Locked, nil
	case "screwdriver!":
		// Allow a screwdriver to bring the lock into an undefined state.
		return nil, nil
	}
	return m.Locked, fmt.Errorf("illegal topic in state 'locked': %s", event.Topic())
}
开发者ID:tideland,项目名称:gocells,代码行数:31,代码来源:fsm_test.go

示例2: ProcessEvent

// ProcessEvent executes the state function and stores
// the returned new state.
func (b *fsmBehavior) ProcessEvent(event cells.Event) error {
	switch event.Topic() {
	case cells.StatusTopic:
		status := FSMStatus{
			Done:  b.done,
			Error: b.err,
		}
		if err := event.Respond(status); err != nil {
			return err
		}
	default:
		if b.done {
			return nil
		}
		state, err := b.state(b.cell, event)
		if err != nil {
			b.done = true
			b.err = err
		} else if state == nil {
			b.done = true
		}
		b.state = state
	}
	return nil
}
开发者ID:tideland,项目名称:gocells,代码行数:27,代码来源:fsm.go

示例3: ProcessEvent

// ProcessEvent counts the event for the return value of the counter func
// and emits this value.
func (b *counterBehavior) ProcessEvent(event cells.Event) error {
	switch event.Topic() {
	case cells.CountersTopic:
		response := b.copyCounters()
		if err := event.Respond(response); err != nil {
			return err
		}
	case cells.ResetTopic:
		b.counters = make(map[string]int64)
	default:
		cids := b.counterFunc(b.cell.ID(), event)
		if cids != nil {
			for _, cid := range cids {
				v, ok := b.counters[cid]
				if ok {
					b.counters[cid] = v + 1
				} else {
					b.counters[cid] = 1
				}
				topic := "counter:" + cid
				b.cell.EmitNewContext(topic, b.counters[cid], event.Context())
			}
		}
	}
	return nil
}
开发者ID:tideland,项目名称:gocells,代码行数:28,代码来源:counter.go

示例4: ProcessEvent

func (b *restorerBehavior) ProcessEvent(event cells.Event) error {
	switch event.Topic() {
	case "grab!":
		cents := b.cents
		b.cents = 0
		return event.Respond(cents)
	case "drop!":
		b.cents += payloadCents(event)
	}
	return nil
}
开发者ID:tideland,项目名称:gocells,代码行数:11,代码来源:fsm_test.go

示例5: Unlocked

// 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())
}
开发者ID:tideland,项目名称:gocells,代码行数:19,代码来源:fsm_test.go

示例6: ProcessEvent

// ProcessEvent collects and re-emits events.
func (b *collectorBehavior) ProcessEvent(event cells.Event) error {
	switch event.Topic() {
	case cells.CollectedTopic:
		response := make([]EventData, len(b.collected))
		copy(response, b.collected)
		if err := event.Respond(response); err != nil {
			return err
		}
	case cells.ResetTopic:
		b.collected = []EventData{}
	default:
		b.collected = append(b.collected, newEventData(event))
		if len(b.collected) > b.max {
			b.collected = b.collected[1:]
		}
		b.cell.Emit(event)
	}
	return nil
}
开发者ID:tideland,项目名称:gocells,代码行数:20,代码来源:collector.go

示例7: ProcessEvent

func (b *collectBehavior) ProcessEvent(event cells.Event) error {
	switch event.Topic() {
	case cells.ProcessedTopic:
		processed := make([]string, len(b.processed))
		copy(processed, b.processed)
		err := event.Respond(processed)
		if err != nil {
			return err
		}
	case cells.ResetTopic:
		b.processed = []string{}
	case cells.PingTopic:
		err := event.Respond(cells.PongResponse)
		if err != nil {
			return err
		}
	case iterateTopic:
		err := b.c.SubscribersDo(func(s cells.Subscriber) error {
			return s.ProcessNewEvent("love", b.c.ID()+" loves "+s.ID(), event.Context())
		})
		if err != nil {
			return err
		}
	case panicTopic:
		panic("Ouch!")
	case subscribersTopic:
		var ids []string
		b.c.SubscribersDo(func(s cells.Subscriber) error {
			ids = append(ids, s.ID())
			return nil
		})
		err := event.Respond(ids)
		if err != nil {
			return err
		}
	default:
		b.processed = append(b.processed, fmt.Sprintf("%v", event))
		return b.c.Emit(event)
	}
	return nil
}
开发者ID:tideland,项目名称:gocells,代码行数:41,代码来源:behaviors_test.go


注:本文中的github.com/tideland/gocells/cells.Event.Respond方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。