Skip to content

Commit

Permalink
Merge pull request #34 from itskihaga/dev
Browse files Browse the repository at this point in the history
fix: on_error
  • Loading branch information
eatski authored Nov 27, 2021
2 parents e652e90 + 40e72f9 commit ae426a6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
8 changes: 4 additions & 4 deletions libs/exprocess/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Record<Core: ExprocessCore> {
pub id: String
}
pub trait Repository<Core: ExprocessCore,Err> {
fn push(&mut self,record: Record<Core>) -> Result<(),Err>;
fn push(&mut self,record: Record<Core>,on_error: Box<dyn FnOnce(Err)>);
fn sync(&mut self,listener: Box<dyn FnMut(Vec<RecordSync<Core>>)>,on_error: Box<dyn FnMut(Err)>);
fn unsync(&mut self);
}
Expand Down Expand Up @@ -74,9 +74,9 @@ impl <Core: ExprocessCore + 'static,Err : 'static> Runner<Core,Err> where Core::
command,
}
};
if let Err(err) = self.repository.push(record) {
self.on_error.borrow_mut()(err)
}
let on_error = self.on_error.clone();
self.repository.push(record,Box::new(move |err| on_error.borrow_mut()(err)));

}
pub fn unsync(&mut self){
self.repository.unsync();
Expand Down
4 changes: 2 additions & 2 deletions libs/exprocess/directly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ type SharedUsedId = Shared<HashSet<String>>;

impl <Core: ExprocessCore + 'static,Err,Inner: Repository<Core,Err>> Repository<Core,Err> for DirectlyDispatch<Core,Err,Inner> {

fn push(&mut self,record: Record<Core>) -> Result<(), Err> {
fn push(&mut self,record: Record<Core>,on_error: Box<dyn FnOnce(Err)>) {
let records =
vec![RecordSync {id: record.id.as_str(),command: &record.command, result: &record.result}];
(self.listener.borrow_mut())(records);
self.used_id.borrow_mut().insert(record.id.clone());
self.inner.push(record)
self.inner.push(record,on_error);
}

fn sync(&mut self,listener: Box<dyn FnMut(Vec<RecordSync<Core>>)>,on_error: Box<dyn FnMut(Err)>) {
Expand Down
29 changes: 17 additions & 12 deletions src/domain/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,24 @@ pub struct RecordDesirailizeIO {

impl Repository<AppCore,RepositoryError> for AppRepository {

fn push(&mut self,record: Record<AppCore>) -> Result<(),RepositoryError>{
push_record(
self.room_id.as_str(),
RecordPushIO {
id: record.id.as_str(),
result: serde_json::to_string(&record.result)?.as_str(),
command: serde_json::to_string(&record.command)?.as_str()
fn push(&mut self,record: Record<AppCore>,on_error: Box<dyn FnOnce(RepositoryError)>) {

match (serde_json::to_string(&record.result),serde_json::to_string(&record.command)) {
(Ok(result_json), Ok(command_json)) => {
let io = RecordPushIO {
id: record.id.as_str(),
result: result_json.as_str(),
command: command_json.as_str()
};
push_record(
self.room_id.as_str(),
io,
Box::new(|| on_error(RepositoryError::UnExpected("".into())))
);
},
Box::new(|| {
todo!()
})
);
Ok(())
(_, Err(err)) => on_error(err.into()),
(Err(err), _) => on_error(err.into()),
}
}

fn sync(&mut self,mut listener: Box<dyn FnMut(Vec<RecordSync<AppCore>>)>,on_error: Box<dyn FnMut(RepositoryError)>) {
Expand Down
4 changes: 2 additions & 2 deletions src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ pub struct RecordPushIO<'a> {
pub result: &'a str
}

pub fn push_record(room_id: &str,record: RecordPushIO,on_error: Box<dyn FnMut()>) {
pub fn push_record(room_id: &str,record: RecordPushIO,on_error: Box<dyn FnOnce()>) {
js_bridge::push_record(
room_id,
record.id,
record.command,
record.result,
Closure::wrap (on_error).into_js_value()
Closure::once_into_js(on_error)
)
}

Expand Down

0 comments on commit ae426a6

Please sign in to comment.