// HandleCommand implements the HandleCommand method of the
// eventhorizon.CommandHandler interface.
764922
func (a *Aggregate) HandleCommand(ctx context.Context, cmd eh.Command) error {
	switch cmd.(type) {
	case *Create:
		// An aggregate can only be created once.
		if a.created {
			return errors.New("already created")
		}
	default:
		// All other events require the aggregate to be created.
		if !a.created {
			return errors.New("not created")
		}
	}

	switch cmd := cmd.(type) {
	case *Create:
		a.StoreEvent(Created, nil, TimeNow())
	case *Delete:
		a.StoreEvent(Deleted, nil, TimeNow())
	case *AddItem:
		a.StoreEvent(ItemAdded, &ItemAddedData{
			ItemID:      a.nextItemID,
			Description: cmd.Description,
		}, TimeNow())
	case *RemoveItem:
		found := false
		for _, item := range a.items {
			if item.ID == cmd.ItemID {
				found = true
				break
			}
		}
		if !found {
			return fmt.Errorf("item does not exist: %d", cmd.ItemID)
		}
		a.StoreEvent(ItemRemoved, &ItemRemovedData{
			ItemID: cmd.ItemID,
		}, TimeNow())
	case *RemoveCompletedItems:
		for _, item := range a.items {
			if item.Completed {
				a.StoreEvent(ItemRemoved, &ItemRemovedData{
					ItemID: item.ID,
				}, TimeNow())
			}
		}
	case *SetItemDescription:
		found := false
		for _, item := range a.items {
			if item.ID == cmd.ItemID {
				found = true
				if item.Description == cmd.Description {
					// Don't emit events when nothing has changed.
					return nil
				}
				break
			}
		}
		if !found {
			return fmt.Errorf("item does not exist: %d", cmd.ItemID)
		}
		a.StoreEvent(ItemDescriptionSet, &ItemDescriptionSetData{
			ItemID:      cmd.ItemID,
			Description: cmd.Description,
		}, TimeNow())
	case *CheckItem:
		found := false
		for _, item := range a.items {
			if item.ID == cmd.ItemID {
				found = true
				if item.Completed == cmd.Checked {
					// Don't emit events when nothing has changed.
					return nil
				}
				break
			}
		}
		if !found {
			return fmt.Errorf("item does not exist: %d", cmd.ItemID)
		}
		a.StoreEvent(ItemChecked, &ItemCheckedData{
			ItemID:  cmd.ItemID,
			Checked: cmd.Checked,
		}, TimeNow())
	case *CheckAllItems:
		for _, item := range a.items {
			if item.Completed != cmd.Checked {
				// Only emit events when there is a change.
				a.StoreEvent(ItemChecked, &ItemCheckedData{
					ItemID:  item.ID,
					Checked: cmd.Checked,
				}, TimeNow())
			}
		}
	default:
		return fmt.Errorf("could not handle command: %s", cmd.CommandType())
	}
	return nil
}
1186074
func (a *AppInstance) processCommand(url string, body string, method string) *CommandResponse {
	jsonMessage, err := json.Marshal(Command{URL: url, Method: method, Body: body})
	if err != nil {
		return &CommandResponse{}
	}

	a.commandChannel <- jsonMessage
	for {
		select {
		case r, r_ok := <-a.responseChannel:
			if r_ok {
				return r
			}
		case <-time.After(5 * time.Second):
			return &CommandResponse{}
		}
	}
}
662352
func (r Rule) HandleEvent(ctx context.Context, e *executor.Event) (err error) {
	if r == nil {
		return nil
	}
	_, _, err = r(ctx, e, nil, ChainIdentity)
	return
}
202423
func (s SerfEventHandler) HandleEvent(e serf.Event) {
	if e == nil {
		return
	}

	var reconcile bool
	switch e.EventType() {

	// If the event is a Join event, call NodeJoined and then reconcile event with
	// persistent storage.
	case serf.EventMemberJoin:
		reconcile = s.ReconcileOnJoin
		if s.NodeJoined != nil {
			s.NodeJoined.HandleMemberJoin(e.(serf.MemberEvent))
		}

	// If the event is a Leave event, call NodeLeft and then reconcile event with
	// persistent storage.
	case serf.EventMemberLeave:
		reconcile = s.ReconcileOnLeave
		if s.NodeLeft != nil {
			s.NodeLeft.HandleMemberLeave(e.(serf.MemberEvent))
		}

	// If the event is a Failed event, call NodeFailed and then reconcile event with
	// persistent storage.
	case serf.EventMemberFailed:
		reconcile = s.ReconcileOnFail
		if s.NodeFailed != nil {
			s.NodeFailed.HandleMemberFailure(e.(serf.MemberEvent))
		}

	// If the event is a Reap event, reconcile event with persistent storage.
	case serf.EventMemberReap:
		reconcile = s.ReconcileOnReap
		if s.NodeReaped != nil {
			s.NodeReaped.HandleMemberReap(e.(serf.MemberEvent))
		}

	// If the event is a user event, handle leader elections, user events and unknown events.
	case serf.EventUser:
		s.handleUserEvent(e.(serf.UserEvent))

	// If the event is an Update event, call NodeUpdated
	case serf.EventMemberUpdate:
		reconcile = s.ReconcileOnUpdate
		if s.NodeUpdated != nil {
			s.NodeUpdated.HandleMemberUpdate(e.(serf.MemberEvent))
		}

	// If the event is a query, call Query Handler
	case serf.EventQuery:
		if s.QueryHandler != nil {
			s.QueryHandler.HandleQueryEvent(*e.(*serf.Query))
		}
	default:
		s.Logger.Warn("unhandled Serf Event: %#v", e)
		return
	}

	// Reconcile event with external storage
	if reconcile && s.Reconciler != nil {
		s.reconcile(e.(serf.MemberEvent))
	}
}
51703
func (b *Bus) ListenCommand(et EventType, command Command, lis Listener) {
	b.lock.Lock()
	defer b.lock.Unlock()

	b.appendListener(et, command, lis)
}
893113
func (s *Server) Handle(method string, handlerFunc interface{}) {
	addHandler(s.handlers, method, handlerFunc)
}
972675
func (cli *HyperClient) Cmd(args ...string) error {
	if len(args) > 1 {
		method, exists := cli.getMethod(args[:2]...)
		if exists {
			return method(args[2:]...)
		}
	}
	if len(args) > 0 {
		method, exists := cli.getMethod(args[0])
		if !exists {
			fmt.Printf("%s: '%s' is not a %s command. See '%s --help'.\n", os.Args[0], args[0], os.Args[0], os.Args[0])
			os.Exit(1)
		}
		return method(args[1:]...)
	}
	return cli.HyperCmdHelp()
}
215755
func (h *Handler) HandleEvent(e *events.Event) {
	f := fmtPool.Get().(*formatter)
	f.buffer.Reset()
	f.emitter.Reset(&f.buffer)

	f.level = "INFO"
	f.time = e.Time
	f.message = e.Message
	f.data.args = e.Args
	f.info.Source = e.Source
	f.info.Program = h.Program
	f.info.Pid = h.Pid

	if e.Debug {
		f.level = "DEBUG"
	}

	for _, a := range e.Args {
		if err, ok := a.Value.(error); ok {
			f.level = "ERROR"
			f.info.Errors = append(f.info.Errors, makeEventError(err))
		}
	}

	(objconv.Encoder{Emitter: &f.emitter}).Encode(f.value)
	f.buffer.WriteByte('\n')

	h.mutex.Lock()
	h.Output.Write(f.buffer.b)
	h.mutex.Unlock()

	f.info.Source = ""
	f.info.Errors = f.info.Errors[:0]
	fmtPool.Put(f)
}
196136
func (hs Handlers) HandleEvent(ctx context.Context, e *executor.Event) (err error) {
	if h := hs[e.GetType()]; h != nil {
		return h.HandleEvent(ctx, e)
	}
	return nil
}
1286296
func (m *EventHandler) HandleEvent(ctx context.Context, event eh.Event) error {
	if m.Err != nil {
		return m.Err
	}
	m.Events = append(m.Events, event)
	m.Context = ctx
	m.Time = time.Now()
	m.Recv <- event
	return nil
}
15537
func (h HandlerFunc) Handle(data json.RawMessage, ctx *apex.Context) (interface{}, error) {
	event, record := new(Event), new(Record)

	if err := json.Unmarshal(data, record); err != nil {
		return nil, err
	}

	if err := decode(record, event); err != nil {
		return nil, err
	}

	if err := h(event, ctx); err != nil {
		return nil, err
	}

	return event, nil
}
457533
func CmdlineHandler() echo.HandlerFunc {
	return func(ctx echo.Context) error {
		Cmdline(ctx)
		return nil
	}
}
281467
func (w *Window) HandleEvent(event string, fn interface{}) (events.ThrustEventHandler, error) {
	if fn, ok := fn.(func(CommandResponse, *Window)); ok == true {
		return events.NewHandler(event, func(cr CommandResponse) {
			fn(cr, w)
		})
	}
	if fn, ok := fn.(func(EventResult, *Window)); ok == true {
		return events.NewHandler(event, func(er EventResult) {
			fn(er, w)
		})
	}
	return events.ThrustEventHandler{}, errors.New("Function Signature Invalid")
}
1369535
func (hf HandlerFunc) Handle(ctx context.Context, command Command) (result interface{}, err error) {
	return hf(ctx, command)
}
631715
func (s *Server) Command(name string) HandlerFactory {
	// Extensions can override builtin commands
	for _, ext := range s.extensions {
		if h := ext.Command(name); h != nil {
			return h
		}
	}

	return s.commands[name]
}
1256243
func (b *EventBus) handle(m eh.EventMatcher, h eh.EventHandler, ch <-chan evt) {
	b.wg.Add(1)
	defer b.wg.Done()

	for e := range ch {
		if !m(e.event) {
			continue
		}
		if err := h.HandleEvent(e.ctx, e.event); err != nil {
			select {
			case b.errCh <- eh.EventBusError{Err: fmt.Errorf("could not handle event (%s): %s", h.HandlerType(), err.Error()), Ctx: e.ctx, Event: e.event}:
			default:
			}
		}
	}
}
1321349
func (f *Flag) Handle() (Handle, error) {
	transport, err := process.NewClient(f.Command)
	if err != nil {
		return nil, fmt.Errorf("failed to open plugin %q: %v", f.Name, err)
	}

	handle, err := NewTransportHandle(f.Name, transport)
	if err != nil {
		return nil, multierr.Combine(
			fmt.Errorf("failed to open plugin %q: %v", f.Name, err),
			transport.Close(),
		)
	}

	return handle, nil
}