File tree Expand file tree Collapse file tree 3 files changed +17
-8
lines changed Expand file tree Collapse file tree 3 files changed +17
-8
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ type Client struct {
17
17
waiter sync.WaitGroup
18
18
}
19
19
20
- // Creates a client wrapping a response writer.
20
+ // NewClient creates a client wrapping a response writer.
21
21
// The response writer must support http.Flusher and http.CloseNotifier
22
22
// interfaces.
23
23
// Returns nil on error.
Original file line number Diff line number Diff line change @@ -2,12 +2,15 @@ package eventsource
2
2
3
3
import (
4
4
"bytes"
5
- "io/ioutil"
6
5
"strconv"
7
6
"strings"
8
7
)
9
8
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.
11
14
type Event struct {
12
15
id string
13
16
data []string
@@ -58,6 +61,14 @@ func (e *Event) Read(p []byte) (int, error) {
58
61
return e .buf .Read (p )
59
62
}
60
63
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
+
61
72
// Wipe out any existing data
62
73
e .buf .Reset ()
63
74
@@ -93,8 +104,6 @@ func (e *Event) Read(p []byte) (int, error) {
93
104
94
105
e .buf .WriteByte ('\n' )
95
106
e .bufSet = true
96
-
97
- return e .buf .Read (p )
98
107
}
99
108
100
109
// Write to the event. Buffer will be converted to one or more
@@ -133,8 +142,8 @@ func (e *Event) WriteRaw(p []byte) (int, error) {
133
142
134
143
// String returns the Event in wire format as a string
135
144
func (e * Event ) String () string {
136
- fullEvent , _ := ioutil . ReadAll ( e )
137
- return string (fullEvent )
145
+ e . prepare ( )
146
+ return string (e . buf . Bytes () )
138
147
}
139
148
140
149
// Clone returns a deep copy of the event
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ type EventFactory interface {
10
10
New () * Event
11
11
}
12
12
13
- // EventIdIncrementer is an event factory that creates events with
13
+ // EventIdFactory is an event factory that creates events with
14
14
// sequential ID fields.
15
15
// If NewFunc is set, the factory uses it to create events before setting
16
16
// their IDs
You can’t perform that action at this time.
0 commit comments