Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-cmds
Commits
cc3ad79f
Commit
cc3ad79f
authored
Jan 28, 2017
by
Jan Winkelmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add TeeEmitters
parent
dbec99e4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
113 additions
and
1 deletion
+113
-1
http/responseemitter.go
http/responseemitter.go
+50
-0
responseemitter.go
responseemitter.go
+63
-1
No files found.
http/responseemitter.go
View file @
cc3ad79f
...
...
@@ -196,3 +196,53 @@ func guessMimeType(enc cmds.EncodingType) string {
return
mimeTypes
[
cmds
.
JSON
]
}
// NewTeeEmitter returns a ResponseEmitter that can Flush. It will forward that flush to re1 and, if it can flush, re2.
func
NewTeeEmitter
(
re1
*
ResponseEmitter
,
re2
cmds
.
ResponseEmitter
)
*
ResponseEmitter
{
return
*
teeEmitter
{
re1
,
re2
,
}
}
type
teeEmitter
struct
{
*
ResponseEmitter
re
cmds
.
ResponseEmitter
}
func
(
re
*
teeEmitter
)
Close
()
error
{
err1
:=
re
.
ResponseEmitter
.
Close
()
err2
:=
re
.
re
.
Close
()
if
err1
!=
nil
{
return
err1
}
// XXX we drop the second error if both fail
return
err2
}
func
(
re
*
teeEmitter
)
Emit
(
v
interface
{})
error
{
err1
:=
re
.
ResponseEmitter
.
Emit
()
err2
:=
re
.
re
.
Emit
()
if
err1
!=
nil
{
return
err1
}
// XXX we drop the second error if both fail
return
err2
}
func
(
re
*
teeEmitter
)
SetError
(
err
interface
{},
code
cmds
.
ErrorType
)
{
re
.
ResponseEmitter
.
SetError
(
err
,
code
)
re
.
re
.
SetError
(
err
,
code
)
}
func
(
re
*
teeEmitter
)
Flush
()
{
re
.
ResponseEmitter
.
Flush
()
if
hre
,
ok
:=
re
.
re
.
(
*
ResponseEmitter
);
ok
{
hre
.
Flush
()
}
}
responseemitter.go
View file @
cc3ad79f
...
...
@@ -64,7 +64,7 @@ type responseEmitter struct {
w
io
.
WriteCloser
enc
Encoder
encType
EncodingType
err
e
rror
err
*
E
rror
}
func
(
re
*
responseEmitter
)
Close
()
error
{
...
...
@@ -110,3 +110,65 @@ func (re *responseEmitter) Emit(value interface{}) error {
return
err
}
func
NewChanResponseEmitter
(
ch
chan
<-
interface
{})
ResponseEmitter
{
return
&
chanResponseEmitter
{
responseEmitter
:
responseEmitter
{},
ch
:
ch
,
}
}
type
chanResponseEmitter
struct
{
responseEmitter
ch
chan
<-
interface
{}
}
func
(
re
*
chanResponseEmitter
)
Emit
(
v
interface
{})
error
{
re
.
ch
<-
v
return
nil
}
func
(
re
*
chanResponseEmitter
)
Close
()
error
{
close
(
re
.
ch
)
return
nil
}
func
NewTeeEmitter
(
re1
,
re2
ResponseEmitter
)
ResponseEmitter
{
return
&
teeEmitter
{
re1
,
re2
}
}
type
teeEmitter
struct
{
ResponseEmitter
re
ResponseEmitter
}
func
(
re
*
teeEmitter
)
Close
()
error
{
err1
:=
re
.
ResponseEmitter
.
Close
()
err2
:=
re
.
re
.
Close
()
if
err1
!=
nil
{
return
err1
}
// XXX we drop the second error if both fail
return
err2
}
func
(
re
*
teeEmitter
)
Emit
(
v
interface
{})
error
{
err1
:=
re
.
ResponseEmitter
.
Emit
(
v
)
err2
:=
re
.
re
.
Emit
(
v
)
if
err1
!=
nil
{
return
err1
}
// XXX we drop the second error if both fail
return
err2
}
func
(
re
*
teeEmitter
)
SetError
(
err
interface
{},
code
ErrorType
)
{
re
.
ResponseEmitter
.
SetError
(
err
,
code
)
re
.
re
.
SetError
(
err
,
code
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment