Skip to content

Commit

Permalink
Merge pull request #337 from cunarist/better-channel
Browse files Browse the repository at this point in the history
Generate cleaner message file code
  • Loading branch information
temeddix authored May 30, 2024
2 parents a50e831 + ba706f1 commit 3c40065
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions flutter_ffi_plugin/bin/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ use rinf::SharedCell;
use std::cell::RefCell;
use std::sync::Mutex;
use std::sync::OnceLock;
use tokio::sync::mpsc::channel;
use tokio::sync::mpsc::Receiver;
use tokio::sync::mpsc::Sender;
use tokio::sync::mpsc::unbounded_channel;
use tokio::sync::mpsc::UnboundedReceiver;
use tokio::sync::mpsc::UnboundedSender;
''',
atFront: true,
);
Expand All @@ -273,17 +273,17 @@ use tokio::sync::mpsc::Sender;
rustPath,
'''
type ${messageName}Cell = SharedCell<(
Option<Sender<DartSignal<${normalizePascal(messageName)}>>>,
Option<Receiver<DartSignal<${normalizePascal(messageName)}>>>,
Option<UnboundedSender<DartSignal<${normalizePascal(messageName)}>>>,
Option<UnboundedReceiver<DartSignal<${normalizePascal(messageName)}>>>,
)>;
pub static ${snakeName.toUpperCase()}_CHANNEL: ${messageName}Cell =
OnceLock::new();
impl ${normalizePascal(messageName)} {
pub fn get_dart_signal_receiver() -> Receiver<DartSignal<Self>> {
pub fn get_dart_signal_receiver() -> UnboundedReceiver<DartSignal<Self>> {
let cell = ${snakeName.toUpperCase()}_CHANNEL
.get_or_init(|| {
let (sender, receiver) = channel(1024);
let (sender, receiver) = unbounded_channel();
Mutex::new(RefCell::new(Some((Some(sender), Some(receiver)))))
})
.lock()
Expand All @@ -298,7 +298,7 @@ impl ${normalizePascal(messageName)} {
let is_closed = pair.0.as_ref().unwrap().is_closed();
drop(borrowed);
if is_closed {
let (sender, receiver) = channel(1024);
let (sender, receiver) = unbounded_channel();
cell.replace(Some((Some(sender), Some(receiver))));
}
}
Expand All @@ -313,39 +313,41 @@ impl ${normalizePascal(messageName)} {
await insertTextToFile(
dartPath,
'''
void sendSignalToRust() {
sendDartSignal(
${markedMessage.id},
this.writeToBuffer(),
Uint8List(0),
);
extension ${messageName}Extension on $messageName{
void sendSignalToRust() {
sendDartSignal(
${markedMessage.id},
this.writeToBuffer(),
Uint8List(0),
);
}
}
''',
after: "class $messageName extends \$pb.GeneratedMessage {",
);
}
}
if (markType == MarkType.dartSignalBinary) {
await insertTextToFile(
dartPath,
'''
void sendSignalToRust(Uint8List binary) {
sendDartSignal(
${markedMessage.id},
this.writeToBuffer(),
binary,
);
extension {$messageName}Extension on $messageName{
void sendSignalToRust(Uint8List binary) {
sendDartSignal(
${markedMessage.id},
this.writeToBuffer(),
binary,
);
}
}
''',
after: "class $messageName extends \$pb.GeneratedMessage {",
);
}
if (markType == MarkType.rustSignal ||
markType == MarkType.rustSignalBinary) {
await insertTextToFile(
dartPath,
'''
static Stream<RustSignal<$messageName>> rustSignalStream =
static final rustSignalStream =
${camelName}Controller.stream.asBroadcastStream();
''',
after: "class $messageName extends \$pb.GeneratedMessage {",
Expand Down Expand Up @@ -407,7 +409,7 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::OnceLock;
use tokio::sync::mpsc::channel;
use tokio::sync::mpsc::unbounded_channel;
type SignalHandlers =
OnceLock<Mutex<HashMap<i32, Box<dyn Fn(Vec<u8>, Vec<u8>) + Send>>>>;
Expand Down Expand Up @@ -452,7 +454,7 @@ hash_map.insert(
};
let cell = ${snakeName.toUpperCase()}_CHANNEL
.get_or_init(|| {
let (sender, receiver) = channel(1024);
let (sender, receiver) = unbounded_channel();
Mutex::new(RefCell::new(Some((Some(sender), Some(receiver)))))
})
.lock()
Expand All @@ -467,14 +469,14 @@ hash_map.insert(
let is_closed = pair.0.as_ref().unwrap().is_closed();
drop(borrowed);
if is_closed {
let (sender, receiver) = channel(1024);
let (sender, receiver) = unbounded_channel();
cell.replace(Some((Some(sender), Some(receiver))));
}
}
let borrowed = cell.borrow();
let pair = borrowed.as_ref().unwrap();
let sender = pair.0.as_ref().unwrap();
let _ = sender.try_send(dart_signal);
let _ = sender.send(dart_signal);
}),
);
''';
Expand Down

0 comments on commit 3c40065

Please sign in to comment.