From 878df9716fe5fed96ddd8bb86e624f7ac3bb6853 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Wed, 29 Nov 2023 00:51:51 +0100 Subject: [PATCH] fix: removed tonioware also moved select_buffer locally for testing, until it's stable --- Cargo.toml | 3 +- .../intellij/task/BufferEventAwaiterTask.java | 11 +----- src/main/rust/lib.rs | 34 +++++++++++++++++-- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6e4b471..12d9773 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,11 +4,12 @@ version = "0.1.0" edition = "2021" [dependencies] -codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag = "0.5.0", features = ["global", "sync"] } +codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag = "v0.5.0", features = ["global", "sync"] } jni = { version = "0.21.1", features = ["invocation"] } jni-sys = "0.3.0" log = "0.4.20" rifgen = { git = "https://github.com/Kofituo/rifgen.git", rev = "d27d9785b2febcf5527f1deb6a846be5d583f7d7"} +tokio = "1.34.0" [build-dependencies] flapigen = "0.6.0" diff --git a/src/main/java/com/codemp/intellij/task/BufferEventAwaiterTask.java b/src/main/java/com/codemp/intellij/task/BufferEventAwaiterTask.java index 0dc69a3..d0eb127 100644 --- a/src/main/java/com/codemp/intellij/task/BufferEventAwaiterTask.java +++ b/src/main/java/com/codemp/intellij/task/BufferEventAwaiterTask.java @@ -23,7 +23,6 @@ import java.util.concurrent.ConcurrentHashMap; public class BufferEventAwaiterTask extends Task.Backgroundable implements Disposable { private final Map bufferListeners = new ConcurrentHashMap<>(); - private final Set initialisedBuffers = Collections.newSetFromMap(new ConcurrentHashMap<>()); //also tonioware public BufferEventAwaiterTask(@NotNull Project project) { super(project, "Awaiting CodeMP buffer events", false); @@ -44,7 +43,6 @@ public class BufferEventAwaiterTask extends Task.Backgroundable implements Dispo public void unregisterListener(String name) { CodeMP.ACTIVE_BUFFERS_REVERSE.remove(CodeMP.ACTIVE_BUFFERS.remove(name)); - this.initialisedBuffers.remove(name); Disposable listener = this.bufferListeners.remove(name); if(listener != null) listener.dispose(); @@ -54,7 +52,7 @@ public class BufferEventAwaiterTask extends Task.Backgroundable implements Dispo public void dispose() {} @Override - @SuppressWarnings({"InfiniteLoopStatement", "UnstableApiUsage", "BusyWait"}) + @SuppressWarnings({"InfiniteLoopStatement", "UnstableApiUsage"}) public void run(@NotNull ProgressIndicator indicator) { try { while(true) { @@ -66,13 +64,6 @@ public class BufferEventAwaiterTask extends Task.Backgroundable implements Dispo continue; String buffer = bufferOptional.get(); - if(!this.initialisedBuffers.contains(buffer)) { //tonioware - try { - Thread.sleep(100); - } catch(InterruptedException ignored) {} - this.initialisedBuffers.add(buffer); - } - BufferHandler handler = CodeMPHandler.getBuffer(buffer); List changeList = new ArrayList<>(); diff --git a/src/main/rust/lib.rs b/src/main/rust/lib.rs index 59a9034..bfa3b46 100644 --- a/src/main/rust/lib.rs +++ b/src/main/rust/lib.rs @@ -1,7 +1,6 @@ use std::sync::Arc; use std::time::Duration; use codemp::prelude::*; -use codemp::tools; use rifgen::rifgen_attr::{generate_access_methods, generate_interface, generate_interface_doc}; pub mod glue { //rifgen generated code @@ -76,8 +75,37 @@ impl CodeMPHandler { Err(_) => continue } } - CODEMP_INSTANCE.rt().block_on( - tools::select_buffer_timeout(&buffers, Duration::from_millis(timeout as u64))) + + let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); + let mut tasks = Vec::new(); + for buffer in buffers { + let _tx = tx.clone(); + let _buffer = buffer.clone(); + tasks.push(CODEMP_INSTANCE.rt().spawn(async move { + match _buffer.poll().await { + Ok(()) => _tx.send(Ok(Some(_buffer.name.clone()))), + Err(_) => _tx.send(Err(CodempError::Channel { send: true })), + } + })) + } + let _tx = tx.clone(); + tasks.push(CODEMP_INSTANCE.rt().spawn(async move { + tokio::time::sleep(Duration::from_millis(timeout as u64)).await; + _tx.send(Ok(None)) + })); + loop { + match CODEMP_INSTANCE.rt().block_on(rx.recv()) { + None => return Err(CodempError::Channel { send: false }), + Some(Err(_)) => continue, + Some(Ok(None)) => return Ok(None), + Some(Ok(Some(x))) => { + for t in tasks { + t.abort(); + } + return Ok(Some(x.clone())); + }, + } + } } }