Skip to content

Commit

Permalink
simplify poll and wait apis
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Jul 22, 2024
1 parent 8ef4b9c commit b06a0f0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
14 changes: 6 additions & 8 deletions src/ndb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ impl Ndb {
}
}

pub fn poll_for_notes(&self, sub: &Subscription, max_notes: u32) -> Vec<NoteKey> {
pub fn poll_for_notes(&self, sub_id: u64, max_notes: u32) -> Vec<NoteKey> {
let mut vec = vec![];
vec.reserve_exact(max_notes as usize);
let sub_id = sub.id;

unsafe {
let res = bindings::ndb_poll_for_notes(
Expand All @@ -146,9 +145,8 @@ impl Ndb {
vec.into_iter().map(NoteKey::new).collect()
}

pub async fn wait_for_notes(&self, sub: &Subscription, max_notes: u32) -> Result<Vec<NoteKey>> {
pub async fn wait_for_notes(&self, sub_id: u64, max_notes: u32) -> Result<Vec<NoteKey>> {
let ndb = self.clone();
let sub_id = sub.id;
let handle = task::spawn_blocking(move || {
let mut vec: Vec<u64> = vec![];
vec.reserve_exact(max_notes as usize);
Expand Down Expand Up @@ -356,7 +354,7 @@ mod tests {
let filters = vec![filter];

let sub = ndb.subscribe(filters.clone()).expect("sub_id");
let waiter = ndb.wait_for_notes(&sub, 1);
let waiter = ndb.wait_for_notes(sub.id, 1);
ndb.process_event(r#"["EVENT","b",{"id": "702555e52e82cc24ad517ba78c21879f6e47a7c0692b9b20df147916ae8731a3","pubkey": "32bf915904bfde2d136ba45dde32c88f4aca863783999faea2e847a8fafd2f15","created_at": 1702675561,"kind": 1,"tags": [],"content": "hello, world","sig": "2275c5f5417abfd644b7bc74f0388d70feb5d08b6f90fa18655dda5c95d013bfbc5258ea77c05b7e40e0ee51d8a2efa931dc7a0ec1db4c0a94519762c6625675"}]"#).expect("process ok");
let res = waiter.await.expect("await ok");
assert_eq!(res, vec![NoteKey::new(1)]);
Expand All @@ -381,7 +379,7 @@ mod tests {
let filter = Filter::new().kinds(vec![1]).build();

let sub = ndb.subscribe(vec![filter]).expect("sub_id");
let waiter = ndb.wait_for_notes(&sub, 1);
let waiter = ndb.wait_for_notes(sub.id, 1);
ndb.process_event(r#"["EVENT","b",{"id": "702555e52e82cc24ad517ba78c21879f6e47a7c0692b9b20df147916ae8731a3","pubkey": "32bf915904bfde2d136ba45dde32c88f4aca863783999faea2e847a8fafd2f15","created_at": 1702675561,"kind": 1,"tags": [],"content": "hello, world","sig": "2275c5f5417abfd644b7bc74f0388d70feb5d08b6f90fa18655dda5c95d013bfbc5258ea77c05b7e40e0ee51d8a2efa931dc7a0ec1db4c0a94519762c6625675"}]"#).expect("process ok");
let res = waiter.await.expect("await ok");
assert_eq!(res, vec![NoteKey::new(1)]);
Expand All @@ -401,12 +399,12 @@ mod tests {
let sub = ndb.subscribe(vec![filter]).expect("sub_id");
ndb.process_event(r#"["EVENT","b",{"id": "702555e52e82cc24ad517ba78c21879f6e47a7c0692b9b20df147916ae8731a3","pubkey": "32bf915904bfde2d136ba45dde32c88f4aca863783999faea2e847a8fafd2f15","created_at": 1702675561,"kind": 1,"tags": [],"content": "hello, world","sig": "2275c5f5417abfd644b7bc74f0388d70feb5d08b6f90fa18655dda5c95d013bfbc5258ea77c05b7e40e0ee51d8a2efa931dc7a0ec1db4c0a94519762c6625675"}]"#).expect("process ok");
// this is too fast, we should have nothing
let res = ndb.poll_for_notes(&sub, 1);
let res = ndb.poll_for_notes(sub.id, 1);
assert_eq!(res, vec![]);

std::thread::sleep(std::time::Duration::from_millis(100));
// now we should have something
let res = ndb.poll_for_notes(&sub, 1);
let res = ndb.poll_for_notes(sub.id, 1);
assert_eq!(res, vec![NoteKey::new(1)]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod tests {
]])
.build()])
.expect("sub");
let waiter = ndb.wait_for_notes(&sub, 1);
let waiter = ndb.wait_for_notes(sub.id, 1);
ndb.process_event(r#"["EVENT","s",{"id": "c5d98cbf4bcd811e2866770c3d380c0284ce1daf3ae9983d22565cb066cf2a19","pubkey": "083727b7a6051673f399102dc48c229c0ec08186ecd7e54ad0e9116d38429c4f","created_at": 1712517119,"kind": 1,"tags": [["e","b9e548b4aa30fa4ce9edf552adaf458385716704994fbaa9e0aa0042a5a5e01e"],["p","140ee9ff21da6e6671f750a0a747c5a3487ee8835159c7ca863e867a1c537b4f"],["hi","3"]],"content": "hi","sig": "1eed792e4db69c2bde2f5be33a383ef8b17c6afd1411598d0c4618fbdf4dbcb9689354276a74614511907a45eec234e0786733e8a6fbb312e6abf153f15fd437"}]"#).expect("process ok");
let res = waiter.await.expect("await ok");
assert_eq!(res.len(), 1);
Expand Down
10 changes: 5 additions & 5 deletions src/util/nip10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ mod test {
.try_into()
.unwrap();
let sub = ndb.subscribe(vec![filter.clone()]).expect("sub_id");
let waiter = ndb.wait_for_notes(&sub, 1);
let waiter = ndb.wait_for_notes(sub.id, 1);

ndb.process_event(r#"
[
Expand Down Expand Up @@ -333,7 +333,7 @@ mod test {
.try_into()
.unwrap();
let sub = ndb.subscribe(vec![filter.clone()]).expect("sub_id");
let waiter = ndb.wait_for_notes(&sub, 1);
let waiter = ndb.wait_for_notes(sub.id, 1);

ndb.process_event(r#"
[
Expand Down Expand Up @@ -391,7 +391,7 @@ mod test {
.try_into()
.unwrap();
let sub = ndb.subscribe(vec![filter.clone()]).expect("sub_id");
let waiter = ndb.wait_for_notes(&sub, 1);
let waiter = ndb.wait_for_notes(sub.id, 1);

ndb.process_event(r#"
[
Expand Down Expand Up @@ -454,7 +454,7 @@ mod test {
.try_into()
.unwrap();
let sub = ndb.subscribe(vec![filter.clone()]).expect("sub_id");
let waiter = ndb.wait_for_notes(&sub, 1);
let waiter = ndb.wait_for_notes(sub.id, 1);

ndb.process_event(r#"
[
Expand Down Expand Up @@ -536,7 +536,7 @@ mod test {
.try_into()
.unwrap();
let sub = ndb.subscribe(vec![filter.clone()]).expect("sub_id");
let waiter = ndb.wait_for_notes(&sub, 1);
let waiter = ndb.wait_for_notes(sub.id, 1);

ndb.process_event(r#"
[
Expand Down

0 comments on commit b06a0f0

Please sign in to comment.