-
Notifications
You must be signed in to change notification settings - Fork 282
feat: EventChannel #178
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
feat: EventChannel #178
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package plugin | ||
|
||
import ( | ||
"fmt" | ||
"runtime/debug" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// EventChannel provides way for flutter applications and hosts to communicate | ||
// using event streams. | ||
// It must be used with a codec, for example the StandardMethodCodec. | ||
type EventChannel struct { | ||
messenger BinaryMessenger | ||
channelName string | ||
methodCodec MethodCodec | ||
|
||
handler StreamHandler | ||
activeSink *EventSink | ||
} | ||
|
||
// NewEventChannel creates a new event channel | ||
func NewEventChannel(messenger BinaryMessenger, channelName string, methodCodec MethodCodec) (channel *EventChannel) { | ||
ec := &EventChannel{ | ||
messenger: messenger, | ||
channelName: channelName, | ||
methodCodec: methodCodec, | ||
} | ||
messenger.SetChannelHandler(channelName, ec.handleChannelMessage) | ||
return ec | ||
} | ||
|
||
// Handle registers a StreamHandler for a event channel. | ||
// | ||
// Consecutive calls override any existing handler registration. | ||
// When given nil as handler, the previously registered | ||
// handler for a method is unregistrered. | ||
// | ||
// When no handler is registered for a method, it will be handled silently by | ||
// sending a nil reply which triggers the dart MissingPluginException exception. | ||
func (e *EventChannel) Handle(handler StreamHandler) { | ||
e.handler = handler | ||
} | ||
|
||
// handleChannelMessage decodes incoming binary message to a method call, calls the | ||
// handler, and encodes the outgoing reply. | ||
func (e *EventChannel) handleChannelMessage(binaryMessage []byte, responseSender ResponseSender) (err error) { | ||
methodCall, err := e.methodCodec.DecodeMethodCall(binaryMessage) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to decode incomming message") | ||
} | ||
|
||
if e.handler == nil { | ||
fmt.Printf("go-flutter: no method handler registered for event channel '%s'\n", e.channelName) | ||
responseSender.Send(nil) | ||
return nil | ||
} | ||
|
||
defer func() { | ||
p := recover() | ||
if p != nil { | ||
fmt.Printf("go-flutter: recovered from panic while handling message for event channel '%s': %v\n", e.channelName, p) | ||
debug.PrintStack() | ||
} | ||
}() | ||
|
||
switch methodCall.Method { | ||
case "listen": | ||
|
||
binaryReply, err := e.methodCodec.EncodeSuccessEnvelope(nil) | ||
if err != nil { | ||
fmt.Printf("go-flutter: failed to encode listen envelope for event channel '%s', error: %v\n", e.channelName, err) | ||
} | ||
responseSender.Send(binaryReply) | ||
|
||
if e.activeSink != nil { | ||
// Repeated calls to onListen may happen during hot restart. | ||
// We separate them with a call to onCancel. | ||
e.handler.OnCancel(nil) | ||
} | ||
|
||
e.activeSink = &EventSink{eventChannel: e} | ||
go e.handler.OnListen(methodCall.Arguments, e.activeSink) | ||
|
||
case "cancel": | ||
if e.activeSink != nil { | ||
e.activeSink = nil | ||
go e.handler.OnCancel(methodCall.Arguments) | ||
|
||
binaryReply, _ := e.methodCodec.EncodeSuccessEnvelope(nil) | ||
responseSender.Send(binaryReply) | ||
} else { | ||
fmt.Printf("go-flutter: No active strean to cancel onEventChannel '%s'\n", e.channelName) | ||
binaryReply, _ := e.methodCodec.EncodeErrorEnvelope("error", "No active stream to cancel", nil) | ||
responseSender.Send(binaryReply) | ||
} | ||
|
||
default: | ||
fmt.Printf("go-flutter: no StreamHandler handler registered for method '%s' on EventChannel '%s'\n", methodCall.Method, e.channelName) | ||
responseSender.Send(nil) // MissingPluginException | ||
} | ||
|
||
return nil | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package plugin | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
// StreamHandler defines the interface for a stream handler setup and tear-down | ||
// requests. | ||
type StreamHandler interface { | ||
// OnListen handles a request to set up an event stream. | ||
OnListen(arguments interface{}, sink *EventSink) | ||
// OnCancel handles a request to tear down the most recently created event | ||
// stream. | ||
OnCancel(arguments interface{}) | ||
} | ||
|
||
// EventSink defines the interface for producers of events to send message to | ||
// Flutter. StreamHandler act as a clients of EventSink for sending events. | ||
type EventSink struct { | ||
eventChannel *EventChannel | ||
|
||
hasEnded bool | ||
sync.Mutex | ||
} | ||
|
||
// Success consumes a successful event. | ||
func (es *EventSink) Success(event interface{}) { | ||
|
||
pchampio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
es.Lock() | ||
defer es.Unlock() | ||
if es.hasEnded || es != es.eventChannel.activeSink { | ||
return | ||
} | ||
|
||
binaryMsg, err := es.eventChannel.methodCodec.EncodeSuccessEnvelope(event) | ||
if err != nil { | ||
fmt.Printf("go-flutter: failed to encode success envelope for event channel '%s', error: %v\n", es.eventChannel.channelName, err) | ||
} | ||
es.eventChannel.messenger.Send(es.eventChannel.channelName, binaryMsg) | ||
} | ||
|
||
// Error consumes an error event. | ||
func (es *EventSink) Error(errorCode string, errorMessage string, errorDetails interface{}) { | ||
|
||
es.Lock() | ||
defer es.Unlock() | ||
if es.hasEnded || es != es.eventChannel.activeSink { | ||
return | ||
} | ||
|
||
binaryMsg, err := es.eventChannel.methodCodec.EncodeErrorEnvelope(errorCode, errorMessage, errorDetails) | ||
if err != nil { | ||
fmt.Printf("go-flutter: failed to encode success envelope for event channel '%s', error: %v\n", es.eventChannel.channelName, err) | ||
} | ||
es.eventChannel.messenger.Send(es.eventChannel.channelName, binaryMsg) | ||
} | ||
|
||
// EndOfStream consumes end of stream. | ||
func (es *EventSink) EndOfStream() { | ||
|
||
es.Lock() | ||
defer es.Unlock() | ||
if es.hasEnded || es != es.eventChannel.activeSink { | ||
return | ||
} | ||
es.hasEnded = true | ||
|
||
es.eventChannel.messenger.Send(es.eventChannel.channelName, nil) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.