|
| 1 | +package plugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "runtime/debug" |
| 6 | + |
| 7 | + "github.com/pkg/errors" |
| 8 | +) |
| 9 | + |
| 10 | +// EventChannel provides way for flutter applications and hosts to communicate |
| 11 | +// using event streams. |
| 12 | +// It must be used with a codec, for example the StandardMethodCodec. |
| 13 | +type EventChannel struct { |
| 14 | + messenger BinaryMessenger |
| 15 | + channelName string |
| 16 | + methodCodec MethodCodec |
| 17 | + |
| 18 | + handler StreamHandler |
| 19 | + activeSink *EventSink |
| 20 | +} |
| 21 | + |
| 22 | +// NewEventChannel creates a new event channel |
| 23 | +func NewEventChannel(messenger BinaryMessenger, channelName string, methodCodec MethodCodec) (channel *EventChannel) { |
| 24 | + ec := &EventChannel{ |
| 25 | + messenger: messenger, |
| 26 | + channelName: channelName, |
| 27 | + methodCodec: methodCodec, |
| 28 | + } |
| 29 | + messenger.SetChannelHandler(channelName, ec.handleChannelMessage) |
| 30 | + return ec |
| 31 | +} |
| 32 | + |
| 33 | +// Handle registers a StreamHandler for a event channel. |
| 34 | +// |
| 35 | +// Consecutive calls override any existing handler registration. |
| 36 | +// When given nil as handler, the previously registered |
| 37 | +// handler for a method is unregistrered. |
| 38 | +// |
| 39 | +// When no handler is registered for a method, it will be handled silently by |
| 40 | +// sending a nil reply which triggers the dart MissingPluginException exception. |
| 41 | +func (e *EventChannel) Handle(handler StreamHandler) { |
| 42 | + e.handler = handler |
| 43 | +} |
| 44 | + |
| 45 | +// handleChannelMessage decodes incoming binary message to a method call, calls the |
| 46 | +// handler, and encodes the outgoing reply. |
| 47 | +func (e *EventChannel) handleChannelMessage(binaryMessage []byte, responseSender ResponseSender) (err error) { |
| 48 | + methodCall, err := e.methodCodec.DecodeMethodCall(binaryMessage) |
| 49 | + if err != nil { |
| 50 | + return errors.Wrap(err, "failed to decode incomming message") |
| 51 | + } |
| 52 | + |
| 53 | + if e.handler == nil { |
| 54 | + fmt.Printf("go-flutter: no method handler registered for event channel '%s'\n", e.channelName) |
| 55 | + responseSender.Send(nil) |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + defer func() { |
| 60 | + p := recover() |
| 61 | + if p != nil { |
| 62 | + fmt.Printf("go-flutter: recovered from panic while handling message for event channel '%s': %v\n", e.channelName, p) |
| 63 | + debug.PrintStack() |
| 64 | + } |
| 65 | + }() |
| 66 | + |
| 67 | + switch methodCall.Method { |
| 68 | + case "listen": |
| 69 | + |
| 70 | + binaryReply, err := e.methodCodec.EncodeSuccessEnvelope(nil) |
| 71 | + if err != nil { |
| 72 | + fmt.Printf("go-flutter: failed to encode listen envelope for event channel '%s', error: %v\n", e.channelName, err) |
| 73 | + } |
| 74 | + responseSender.Send(binaryReply) |
| 75 | + |
| 76 | + if e.activeSink != nil { |
| 77 | + // Repeated calls to onListen may happen during hot restart. |
| 78 | + // We separate them with a call to onCancel. |
| 79 | + e.handler.OnCancel(nil) |
| 80 | + } |
| 81 | + |
| 82 | + e.activeSink = &EventSink{eventChannel: e} |
| 83 | + go e.handler.OnListen(methodCall.Arguments, e.activeSink) |
| 84 | + |
| 85 | + case "cancel": |
| 86 | + if e.activeSink != nil { |
| 87 | + e.activeSink = nil |
| 88 | + go e.handler.OnCancel(methodCall.Arguments) |
| 89 | + |
| 90 | + binaryReply, _ := e.methodCodec.EncodeSuccessEnvelope(nil) |
| 91 | + responseSender.Send(binaryReply) |
| 92 | + } else { |
| 93 | + fmt.Printf("go-flutter: No active stream to cancel onEventChannel '%s'\n", e.channelName) |
| 94 | + binaryReply, _ := e.methodCodec.EncodeErrorEnvelope("error", "No active stream to cancel", nil) |
| 95 | + responseSender.Send(binaryReply) |
| 96 | + } |
| 97 | + |
| 98 | + default: |
| 99 | + fmt.Printf("go-flutter: no StreamHandler handler registered for method '%s' on EventChannel '%s'\n", methodCall.Method, e.channelName) |
| 100 | + responseSender.Send(nil) // MissingPluginException |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
0 commit comments