Esempio n. 1
0
func primeEventStream(eventChannel chan models.Event, eventType string, primer func(), cleanup func()) {
	primer()

PRIMING:
	for {
		select {
		case <-eventChannel:
			break PRIMING
		case <-time.After(50 * time.Millisecond):
			cleanup()
			primer()
		}
	}

	cleanup()

	var event models.Event
	for {
		Eventually(eventChannel).Should(Receive(&event))
		if event.EventType() == eventType {
			break
		}
	}

	for {
		select {
		case <-eventChannel:
		case <-time.After(50 * time.Millisecond):
			return
		}
	}
}
Esempio n. 2
0
func NewEventFromModelEvent(eventID int, event models.Event) (sse.Event, error) {
	payload, err := proto.Marshal(event)
	if err != nil {
		return sse.Event{}, err
	}

	encodedPayload := base64.StdEncoding.EncodeToString(payload)
	return sse.Event{
		ID:   strconv.Itoa(eventID),
		Name: string(event.EventType()),
		Data: []byte(encodedPayload),
	}, nil
}