Skip to content

Commit

Permalink
ios: Queue up messages
Browse files Browse the repository at this point in the history
Queue up messages until event-sink has been created
Closes PhilipsHue#307, PhilipsHue#251, PhilipsHue#137
  • Loading branch information
jalpedersen committed Sep 30, 2021
1 parent 0d8afe6 commit fc331af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ final class PluginController {
}
}
}
var messageQueue: [CharacteristicValueInfo] = [];
var connectedDeviceSink: EventSink?
var characteristicValueUpdateSink: EventSink?

Expand Down Expand Up @@ -102,9 +103,6 @@ final class PluginController {
sink.add(.success(message))
},
onCharacteristicValueUpdate: papply(weak: self) { context, central, characteristic, value, error in
guard let sink = context.characteristicValueUpdateSink
else { assert(false); return }

let message = CharacteristicValueInfo.with {
$0.characteristic = CharacteristicAddress.with {
$0.characteristicUuid = Uuid.with { $0.data = characteristic.id.data }
Expand All @@ -121,7 +119,14 @@ final class PluginController {
}
}
}
sink.add(.success(message))
let sink = context.characteristicValueUpdateSink
if (sink != nil) {
sink!.add(.success(message))
} else {
// In case message arrives before sink is created
context.messageQueue.append(message);
}

}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ public class SwiftReactiveBlePlugin: NSObject, FlutterPlugin {
context: context,
onListen: { context, sink in
context.characteristicValueUpdateSink = sink
context.messageQueue.forEach { msg in
sink.add(.success(msg))
}
context.messageQueue.removeAll()
return nil
},
onCancel: { context in
context.messageQueue.removeAll()
context.characteristicValueUpdateSink = nil
return nil
}
Expand Down

0 comments on commit fc331af

Please sign in to comment.