...

Source file src/github.com/redhat-developer/odo/pkg/apiserver-impl/sse/event.go

Documentation: github.com/redhat-developer/odo/pkg/apiserver-impl/sse

     1  package sse
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  )
     8  
     9  type EventType int
    10  
    11  const (
    12  	Heartbeat EventType = iota + 1
    13  	DevfileUpdated
    14  )
    15  
    16  type Event struct {
    17  	eventType EventType
    18  	data      interface{}
    19  }
    20  
    21  func (e Event) toSseString() (string, error) {
    22  	var eventName string
    23  	switch e.eventType {
    24  	case Heartbeat:
    25  		return ": heartbeat\n\n", nil
    26  	case DevfileUpdated:
    27  		eventName = "DevfileUpdated"
    28  	default:
    29  		return "", fmt.Errorf("unrecognized event type:%v", e.eventType)
    30  	}
    31  
    32  	if e.data == nil {
    33  		return fmt.Sprintf("event: %s\n\n", eventName), nil
    34  	}
    35  
    36  	jsonBytes, err := json.Marshal(e.data)
    37  	if err != nil {
    38  		return "", err
    39  	}
    40  	var dataStr bytes.Buffer
    41  	err = json.Compact(&dataStr, jsonBytes)
    42  	if err != nil {
    43  		return "", err
    44  	}
    45  	return fmt.Sprintf("event: %s\ndata: %s\n\n", eventName, dataStr.String()), nil
    46  }
    47  

View as plain text