Skip to content

Commit 30422ae

Browse files
committed
Bytes logic update
1 parent 4ab45b9 commit 30422ae

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Client struct {
1717
waiter sync.WaitGroup
1818
}
1919

20-
// Creates a client wrapping a response writer.
20+
// NewClient creates a client wrapping a response writer.
2121
// The response writer must support http.Flusher and http.CloseNotifier
2222
// interfaces.
2323
// Returns nil on error.

event.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package eventsource
22

33
import (
44
"bytes"
5-
"io/ioutil"
65
"strconv"
76
"strings"
87
)
98

10-
// Holds the data for an event
9+
// Event holds the structured data for an event.
10+
// The event object contains working memory as well as a bytes
11+
// buffer. The buffer is filled from the working area at the first
12+
// call to either Read or String. Mutating the event resets the buffer
13+
// but sequential calls to Read do not.
1114
type Event struct {
1215
id string
1316
data []string
@@ -58,6 +61,14 @@ func (e *Event) Read(p []byte) (int, error) {
5861
return e.buf.Read(p)
5962
}
6063

64+
e.prepare()
65+
66+
return e.buf.Read(p)
67+
}
68+
69+
// Prepares the data buf for reading
70+
func (e *Event) prepare() {
71+
6172
// Wipe out any existing data
6273
e.buf.Reset()
6374

@@ -93,8 +104,6 @@ func (e *Event) Read(p []byte) (int, error) {
93104

94105
e.buf.WriteByte('\n')
95106
e.bufSet = true
96-
97-
return e.buf.Read(p)
98107
}
99108

100109
// Write to the event. Buffer will be converted to one or more
@@ -133,8 +142,8 @@ func (e *Event) WriteRaw(p []byte) (int, error) {
133142

134143
// String returns the Event in wire format as a string
135144
func (e *Event) String() string {
136-
fullEvent, _ := ioutil.ReadAll(e)
137-
return string(fullEvent)
145+
e.prepare()
146+
return string(e.buf.Bytes())
138147
}
139148

140149
// Clone returns a deep copy of the event

event_factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type EventFactory interface {
1010
New() *Event
1111
}
1212

13-
// EventIdIncrementer is an event factory that creates events with
13+
// EventIdFactory is an event factory that creates events with
1414
// sequential ID fields.
1515
// If NewFunc is set, the factory uses it to create events before setting
1616
// their IDs

0 commit comments

Comments
 (0)