Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dekkku committed Sep 12, 2024
1 parent b5993bb commit 7a1b85b
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions projects/dekkku/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,44 @@ impl Query {
let mut posts: Vec<Post> = http_client.request(ALL_POSTS).await?;
let store = ctx.data_unchecked::<Arc<Store>>();

// TODO: clean this up.
let are_posts_same = {
let is_cache_empty = store.posts.read().unwrap().is_empty();
if is_cache_empty {
let mut rng = rand::thread_rng();
let selected_posts: Vec<&Post> = posts.choose_multiple(&mut rng, 2).collect();
if selected_posts.len() == 2 {
let mut posts_writer = store.posts.write().unwrap();
for post in selected_posts {
posts_writer.insert(post.id, post.clone());
}
}
false
} else {
let cached_posts: Vec<Post> =
store.posts.read().unwrap().values().cloned().collect();
let mut posts_writer = store.posts.write().unwrap();
cached_posts.iter().all(|post| {
if let Some(new_post) = posts.iter().find(|p| p.id == post.id) {
if post != new_post {
posts_writer.insert(new_post.id, new_post.clone());
false
} else {
true
}
} else {
false
}
})
}
};

let should_query_user = ctx
.field()
.selection_set()
.any(|field| field.name() == "user");

if should_query_user {
// TODO: clean this up.
let are_posts_same = {
let is_cache_empty = store.posts.read().unwrap().is_empty();
if is_cache_empty {
let mut rng = rand::thread_rng();
let selected_posts: Vec<&Post> = posts.choose_multiple(&mut rng, 2).collect();
if selected_posts.len() == 2 {
let mut posts_writer = store.posts.write().unwrap();
for post in selected_posts {
posts_writer.insert(post.id, post.clone());
}
}
false
} else {
let cached_posts: Vec<Post> =
store.posts.read().unwrap().values().cloned().collect();
let mut posts_writer = store.posts.write().unwrap();
cached_posts.iter().all(|post| {
if let Some(new_post) = posts.iter().find(|p| p.id == post.id) {
if post != new_post {
posts_writer.insert(new_post.id, new_post.clone());
false
} else {
true
}
} else {
false
}
})
}
};

if are_posts_same {
// Posts are the same, load the data from store and update the posts' user property.
let store_users = store.users.read().unwrap();
Expand Down

0 comments on commit 7a1b85b

Please sign in to comment.