Skip to content

Commit

Permalink
fix: don't require &mut self for event appends
Browse files Browse the repository at this point in the history
wasm-bindgen doesn't deal well with &mut references in some
cases, and this was causing issues across various test suites
with the new ResumableTaskStore
  • Loading branch information
enmand committed Jun 19, 2024
1 parent 376738c commit f374bcb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
5 changes: 0 additions & 5 deletions crates/dwn-rs-stores/src/surrealdb/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use surrealdb::{
},
Surreal,
};
use ulid::Generator;

use crate::{surrealdb::auth::Auth, StoreError};

Expand All @@ -20,8 +19,6 @@ pub struct SurrealDB {
pub(super) db: Surreal<Any>,
constr: String,
invalid: bool,

pub(super) ulid_generator: Generator,
}

impl Default for SurrealDB {
Expand Down Expand Up @@ -57,8 +54,6 @@ impl SurrealDB {
db: surrealdb::Surreal::init(),
constr: String::new(),
invalid: false,

ulid_generator: Generator::new(),
}
}

Expand Down
15 changes: 7 additions & 8 deletions crates/dwn-rs-stores/src/surrealdb/event_log.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use async_trait::async_trait;
use dwn_rs_core::MapValue;
use dwn_rs_core::{MapValue, Value};
use surrealdb::sql::{Id, Table, Thing};
use ulid::Ulid;

use crate::{
Cursor, EventLog, EventLogError, Filters, MessageWatermark, Pagination, Query, QueryError,
Expand All @@ -22,18 +23,17 @@ impl EventLog for SurrealDB {
}

async fn append(
&mut self,
&self,
tenant: &str,
cid: String,
indexes: MapValue,
) -> Result<(), EventLogError> {
let id = Thing::from((EVENTS_TABLE, Id::String(cid.to_string())));
let watermark = self.ulid_generator.generate()?.to_string();
let watermark = Ulid::new();
let res = Thing::from((EVENTS_TABLE, cid.to_string().as_str()));

self.as_tenant(tenant, |db| async move {
db.create::<Option<CreateEvent>>(id.clone())
db.create::<Option<CreateEvent>>(res)
.content(CreateEvent {
id,
watermark,
cid,
indexes,
Expand Down Expand Up @@ -79,10 +79,9 @@ impl EventLog for SurrealDB {
.page(Some(page));

let (mut events, cursor) = qb.query().await?;
events.sort_by_key(|e| e.watermark.to_owned());

Ok(QueryReturn {
items: events.into_iter().map(|e| e.id.id.to_string()).collect(),
items: events.into_iter().map(|e| e.cid.to_string()).collect(),
cursor,
})
}
Expand Down
14 changes: 6 additions & 8 deletions crates/dwn-rs-wasm/src/surrealdb/event_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,20 @@ impl SurrealEventLog {
.open()
.await
.map_err(EventLogError::from)
.map_err(Into::into)
.map_err(Into::<JsError>::into)?;
Ok(())
}

#[wasm_bindgen]
pub async fn append(
&mut self,
tenant: &str,
cid: &str,
indexes: IndexMap,
) -> Result<(), JsError> {
pub async fn append(&self, tenant: &str, cid: &str, indexes: IndexMap) -> Result<(), JsError> {
let (indexes, _) = indexes.into();
self.store
.append(tenant, cid.to_string(), indexes)
.await
.map_err(EventLogError::from)
.map_err(Into::into)
.map_err(Into::<JsError>::into)?;

Ok(())
}

#[wasm_bindgen(js_name = getEvents)]
Expand Down

0 comments on commit f374bcb

Please sign in to comment.