Skip to content

Commit

Permalink
Create message channel again if closed
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed Jul 9, 2024
1 parent 4ad8ef4 commit dd56f27
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions flutter_ffi_plugin/bin/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -298,26 +298,19 @@ impl ${normalizePascal(messageName)} {
let (sender, receiver) = unbounded_channel();
guard.replace((sender, Some(receiver)));
}
#[cfg(debug_assertions)]
{
// After Dart's hot restart,
// a sender from the previous run already exists
// which is now closed.
let pair = guard
.as_ref()
.ok_or(RinfError::NoMessageChannel)?;
if pair.0.is_closed() {
let (sender, receiver) = unbounded_channel();
guard.replace((sender, Some(receiver)));
}
}
let pair = guard
let (mut sender, mut receiver_option) = guard
.take()
.ok_or(RinfError::NoMessageChannel)?;
guard.replace((pair.0, None));
let receiver = pair
.1
.ok_or(RinfError::MessageReceiverTaken)?;
// After Dart's hot restart or app reopen on mobile devices,
// a sender from the previous run already exists
// which is now closed.
if sender.is_closed() {
let receiver;
(sender, receiver) = unbounded_channel();
receiver_option = Some(receiver);
}
let receiver = receiver_option.ok_or(RinfError::MessageReceiverTaken)?;
guard.replace((sender, None));
Ok(receiver)
}
}
Expand Down Expand Up @@ -474,22 +467,19 @@ new_hash_map.insert(
let (sender, receiver) = unbounded_channel();
guard.replace((sender, Some(receiver)));
}
#[cfg(debug_assertions)]
{
// After Dart's hot restart,
// a sender from the previous run already exists
// which is now closed.
let pair = guard
let mut pair = guard
.as_ref()
.ok_or(RinfError::NoMessageChannel)?;
// After Dart's hot restart or app reopen on mobile devices,
// a sender from the previous run already exists
// which is now closed.
if pair.0.is_closed() {
let (sender, receiver) = unbounded_channel();
guard.replace((sender, Some(receiver)));
pair = guard
.as_ref()
.ok_or(RinfError::NoMessageChannel)?;
if pair.0.is_closed() {
let (sender, receiver) = unbounded_channel();
guard.replace((sender, Some(receiver)));
}
}
let pair = guard
.as_ref()
.ok_or(RinfError::NoMessageChannel)?;
let sender = &pair.0;
let _ = sender.send(dart_signal);
Ok(())
Expand Down

0 comments on commit dd56f27

Please sign in to comment.