commit b06a0f0fafc9e7c4b2a1abd73fd7549462d6066f
parent 8ef4b9c26145572ad7543d955778499e84723099
Author: William Casarin <jb55@jb55.com>
Date: Mon, 22 Jul 2024 15:14:28 -0700
simplify poll and wait apis
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
3 files changed, 12 insertions(+), 14 deletions(-)
diff --git a/src/ndb.rs b/src/ndb.rs
@@ -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(
@@ -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);
@@ -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)]);
@@ -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)]);
@@ -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)]);
}
}
diff --git a/src/tags.rs b/src/tags.rs
@@ -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);
diff --git a/src/util/nip10.rs b/src/util/nip10.rs
@@ -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#"
[
@@ -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#"
[
@@ -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#"
[
@@ -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#"
[
@@ -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#"
[