EventSink.swift 946 B

1234567891011121314151617181920212223242526272829303132
  1. import enum SwiftProtobuf.BinaryEncodingError
  2. struct EventSink {
  3. private let name: String
  4. private let sink: FlutterEventSink
  5. init(name: String, _ sink: @escaping FlutterEventSink) {
  6. self.name = name
  7. self.sink = sink
  8. }
  9. func add(_ event: PlatformMethodResult) {
  10. switch event {
  11. case .success(let message):
  12. if let message = message {
  13. do {
  14. sink(FlutterStandardTypedData(bytes: try message.serializedData()))
  15. } catch let error as BinaryEncodingError {
  16. sink(PluginError.messageSerializationFailure(type: type(of: message), underlyingError: error).asFlutterError)
  17. } catch {
  18. sink(PluginError.unknown(error).asFlutterError)
  19. }
  20. } else {
  21. sink(nil)
  22. }
  23. case .failure(let error):
  24. sink(error)
  25. }
  26. }
  27. }