Skip to content

Commit

Permalink
Move TaskFuture to composer module
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Nov 24, 2024
1 parent 0c0eaa6 commit de49ba5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
22 changes: 20 additions & 2 deletions src/composer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{prelude::*, ScopeData, TaskWaker};
use crate::{prelude::*, ScopeData};
use compose::AnyCompose;
use slotmap::{DefaultKey, SlotMap};
use std::{
Expand All @@ -7,7 +7,7 @@ use std::{
pin::Pin,
rc::Rc,
sync::{mpsc, Arc},
task::{Context, Waker},
task::{Context, Wake, Waker},
};
use tokio::sync::{RwLock, RwLockWriteGuard};

Expand Down Expand Up @@ -111,6 +111,24 @@ impl<U: Updater> Updater for UpdateWrapper<U> {
}
}

struct TaskWaker {
key: DefaultKey,
updater: Arc<dyn Updater>,
tx: mpsc::Sender<DefaultKey>,
}

impl Wake for TaskWaker {
fn wake(self: Arc<Self>) {
let key = self.key;
let pending = self.tx.clone();
self.updater.update(Update {
f: Box::new(move || {
pending.send(key).unwrap();
}),
});
}
}

/// Composer for composable content.
pub struct Composer {
compose: Box<dyn AnyCompose>,
Expand Down
24 changes: 2 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@
#![deny(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]

use composer::{Runtime, Update, Updater};
use slotmap::DefaultKey;
use composer::Runtime;
use std::{
any::{Any, TypeId},
cell::{Cell, RefCell, UnsafeCell},
Expand All @@ -115,8 +114,7 @@ use std::{
pin::Pin,
ptr::NonNull,
rc::Rc,
sync::{mpsc, Arc},
task::Wake,
sync::Arc,
};
use thiserror::Error;

Expand Down Expand Up @@ -943,24 +941,6 @@ pub fn use_drop<'a>(cx: ScopeState<'a>, f: impl FnOnce() + 'a) {
}
}

struct TaskWaker {
key: DefaultKey,
updater: Arc<dyn Updater>,
tx: mpsc::Sender<DefaultKey>,
}

impl Wake for TaskWaker {
fn wake(self: Arc<Self>) {
let key = self.key;
let pending = self.tx.clone();
self.updater.update(Update {
f: Box::new(move || {
pending.send(key).unwrap();
}),
});
}
}

/// Use a local task that runs on the current thread.
///
/// This will run on the window event loop, polling the task until it completes.
Expand Down

0 comments on commit de49ba5

Please sign in to comment.