diff --git a/rust/src/api/plugin/plugin.rs b/rust/src/api/plugin/plugin.rs index 451d94c6..38fd33e3 100644 --- a/rust/src/api/plugin/plugin.rs +++ b/rust/src/api/plugin/plugin.rs @@ -125,7 +125,7 @@ pub struct SpotubePlugin { pub track: PluginTrackSender, pub user: PluginUserSender, event_tx: Sender, - event_rx: Option>, + pub event_rx: Option>, } impl SpotubePlugin { diff --git a/rust/src/main.rs b/rust/src/main.rs index 058f9fe7..7197fef5 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -2,93 +2,15 @@ mod api; mod frb_generated; mod internal; -use rquickjs::function::{Async, Func}; -use rquickjs::{async_with, AsyncContext, AsyncRuntime, Function, Object, Promise}; -use tokio::time::Instant; - +use crate::api::plugin::models::auth::{AuthEventObject, AuthEventType}; use crate::api::plugin::models::core::{PluginAbility, PluginConfiguration}; use crate::api::plugin::plugin::SpotubePlugin; +use crate::frb_generated::StreamSink; +use tokio::io::sink; +use tokio::time::Instant; -async fn set_timeout(func: Function<'_>, timeout: u64) { - tokio::time::sleep(std::time::Duration::from_millis(timeout)).await; - func.call::<_, ()>(()).unwrap(); -} - -fn print(msg: String) { - println!("{}", msg); -} - -async fn non_plugin() -> anyhow::Result<()> { - let start = Instant::now(); - let rt = AsyncRuntime::new()?; - let ctx = AsyncContext::full(&rt).await?; - - async_with!(ctx => |ctx| { - let global = ctx.globals(); - let console = Object::new(ctx.clone()).unwrap(); - console.set("log", Func::from(print)).unwrap(); - global.set("console", console).unwrap(); - - global.set("setTimeout", - Function::new(ctx.clone(), Async(set_timeout)).unwrap().with_name("setTimeout") - ).unwrap(); - - let check_update_fn: Function = ctx.eval(r#" - function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); - } - (async function checkUpdate() { - console.log('Core checkUpdate'); - await sleep(1000); - console.log('No update available'); - }) - "#)?; - - let (r1, r2) = tokio::join!( - check_update_fn.call::<_, Promise>(()).unwrap().into_future::<()>(), - check_update_fn.call::<_, Promise>(()).unwrap().into_future::<()>() - ); - r1?; - r2?; - Ok::<(), rquickjs::Error>(()) - }) - .await - .map_err(|e| anyhow::anyhow!(e))?; - - let duration = start.elapsed(); - println!("[NON-PLUGIN] Execution time: {:?}", duration); - Ok(()) -} - -const PLUGIN_JS: &str = "\ -function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); -} - -class Core { - async checkUpdate() { - console.log('Core checkUpdate'); - await sleep(1000); - console.log('No update available'); - } - support() { - return 'Metadata'; - } -} - -class Auth {} - -class TestingPlugin { - constructor() { - this.core = new Core(); - this.auth = new Auth(); - } -} -"; - -async fn plugin() -> anyhow::Result<()> { - let start = Instant::now(); - let plugin = SpotubePlugin::new(); +async fn plugin(plugin_js: String) -> anyhow::Result<()> { + let mut plugin = SpotubePlugin::new(); let config = PluginConfiguration { entry_point: "TestingPlugin".to_string(), abilities: vec![PluginAbility::Metadata], @@ -100,23 +22,43 @@ async fn plugin() -> anyhow::Result<()> { repository: None, version: "0.1.0".to_string(), }; - let sender = plugin.create_context(PLUGIN_JS.to_string(), config.clone(), "".to_string(), "".to_string(), "".into()).await?; - let (r1, r2) = tokio::join!( - plugin.core.check_update(&sender, config.clone()), - plugin.core.check_update(&sender, config.clone()) + let sender = plugin + .create_context( + plugin_js, + config.clone(), + "https://localhost:3000".to_string(), + "1234567890_secret".to_string(), + "/home/krtirtho/.local/share/spotube".into(), + ) + .await?; + + tokio::spawn(async move { + while let Some(event) = plugin.event_rx.take().unwrap().recv().await { + println!("Auth event: {:?}", event); + if event.event_type != AuthEventType::Logout { + break; + } + } + }); + println!( + "Is Authenticated: {}", + plugin.auth.is_authenticated(&sender).await? ); - r1?; - r2?; - let duration = start.elapsed(); - println!("[PLUGIN] Execution time: {:?}", duration); + tokio::time::sleep(tokio::time::Duration::from_secs(10)).await; Ok(()) } #[tokio::main] async fn main() -> anyhow::Result<()> { - non_plugin().await?; - plugin().await?; + let js_code_path = std::env::args().nth(1); + if js_code_path.is_none() { + panic!("Please provide the path to the plugin JS code as a command line argument."); + } + let js_code_path = js_code_path.unwrap(); + let plugin_js = tokio::fs::read_to_string(js_code_path).await?; + + plugin(plugin_js).await?; Ok(()) }