Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create message channel again if closed #398

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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