This commit is contained in:
sxyazi 2024-04-25 18:45:29 +08:00
parent 903850935f
commit 6066f1e382
No known key found for this signature in database
3 changed files with 20 additions and 14 deletions

View file

@ -98,8 +98,7 @@ impl Manager {
} }
if !succeeded.is_empty() { if !succeeded.is_empty() {
// Pubsub::pub_from_bulk(tab, &changes); Pubsub::pub_from_bulk(tab, succeeded.iter().map(|(u, f)| (u, &f.url)).collect());
FilesOp::Upserting(cwd, succeeded).emit(); FilesOp::Upserting(cwd, succeeded).emit();
} }
drop(permit); drop(permit);

View file

@ -9,20 +9,27 @@ use super::Body;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct BodyBulk<'a> { pub struct BodyBulk<'a> {
pub tab: usize, pub tab: usize,
pub changes: Cow<'a, HashMap<Url, Url>>, pub changes: HashMap<Cow<'a, Url>, Cow<'a, Url>>,
} }
impl<'a> BodyBulk<'a> { impl<'a> BodyBulk<'a> {
#[inline] #[inline]
pub fn borrowed(tab: usize, changes: &'a HashMap<Url, Url>) -> Body<'a> { pub fn borrowed(tab: usize, changes: &HashMap<&'a Url, &'a Url>) -> Body<'a> {
Self { tab, changes: Cow::Borrowed(changes) }.into() let iter = changes.iter().map(|(&from, &to)| (Cow::Borrowed(from), Cow::Borrowed(to)));
Self { tab, changes: iter.collect() }.into()
} }
} }
impl BodyBulk<'static> { impl BodyBulk<'static> {
#[inline] #[inline]
pub fn owned(tab: usize, changes: &HashMap<Url, Url>) -> Body<'static> { pub fn owned(tab: usize, changes: &HashMap<&Url, &Url>) -> Body<'static> {
Self { tab, changes: Cow::Owned(changes.clone()) }.into() let changes = changes
.iter()
.map(|(&from, &to)| (Cow::Owned(from.clone()), Cow::Owned(to.clone())))
.collect();
Self { tab, changes }.into()
} }
} }
@ -32,14 +39,14 @@ impl<'a> From<BodyBulk<'a>> for Body<'a> {
impl IntoLua<'_> for BodyBulk<'static> { impl IntoLua<'_> for BodyBulk<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> { fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
BodyBulkIter { tab: self.tab, inner: self.changes.into_owned().into_iter() }.into_lua(lua) BodyBulkIter { tab: self.tab, inner: self.changes.into_iter() }.into_lua(lua)
} }
} }
// --- Iterator // --- Iterator
pub struct BodyBulkIter { pub struct BodyBulkIter {
pub tab: usize, pub tab: usize,
pub inner: hash_map::IntoIter<Url, Url>, pub inner: hash_map::IntoIter<Cow<'static, Url>, Cow<'static, Url>>,
} }
impl UserData for BodyBulkIter { impl UserData for BodyBulkIter {
@ -52,7 +59,7 @@ impl UserData for BodyBulkIter {
methods.add_meta_function(MetaMethod::Pairs, |lua, me: AnyUserData| { methods.add_meta_function(MetaMethod::Pairs, |lua, me: AnyUserData| {
let iter = lua.create_function(|lua, mut me: UserDataRefMut<Self>| { let iter = lua.create_function(|lua, mut me: UserDataRefMut<Self>| {
if let Some((from, to)) = me.inner.next() { if let Some((Cow::Owned(from), Cow::Owned(to))) = me.inner.next() {
(lua.create_any_userdata(from)?, lua.create_any_userdata(to)?).into_lua_multi(lua) (lua.create_any_userdata(from)?, lua.create_any_userdata(to)?).into_lua_multi(lua)
} else { } else {
().into_lua_multi(lua) ().into_lua_multi(lua)

View file

@ -130,15 +130,15 @@ impl Pubsub {
} }
} }
pub fn pub_from_bulk(tab: usize, changes: &HashMap<Url, Url>) { pub fn pub_from_bulk(tab: usize, changes: HashMap<&Url, &Url>) {
if LOCAL.read().contains_key("bulk") { if LOCAL.read().contains_key("bulk") {
Self::pub_(BodyBulk::owned(tab, changes)); Self::pub_(BodyBulk::owned(tab, &changes));
} }
if PEERS.read().values().any(|p| p.able("bulk")) { if PEERS.read().values().any(|p| p.able("bulk")) {
Client::push(BodyBulk::borrowed(tab, changes)); Client::push(BodyBulk::borrowed(tab, &changes));
} }
if BOOT.local_events.contains("bulk") { if BOOT.local_events.contains("bulk") {
BodyBulk::borrowed(tab, changes).with_receiver(*ID).flush(); BodyBulk::borrowed(tab, &changes).with_receiver(*ID).flush();
} }
} }