File tree 2 files changed +30
-7
lines changed
2 files changed +30
-7
lines changed Original file line number Diff line number Diff line change 1
1
use super :: EventStore ;
2
2
use super :: { Aggregate , Direction , Event , Split } ;
3
- use crate :: util;
3
+ use crate :: util:: { self , AsyncTaskBoxed } ;
4
+ use log:: info;
4
5
use std:: sync:: mpsc;
5
6
6
7
/// Type that allows to communicate with an [event store](EventStore).
7
8
pub struct EventStoreClient {
8
9
sender : mpsc:: Sender < Message > ,
9
- handler : Option < Box < dyn FnOnce ( ) -> Result < ( ) , ( ) > > > ,
10
+ handler : Option < AsyncTaskBoxed > ,
10
11
}
11
12
12
13
impl EventStoreClient {
@@ -154,7 +155,7 @@ impl Drop for EventStoreClient {
154
155
let handler = self . handler . take ( ) ;
155
156
156
157
if let Some ( handler) = handler {
157
- handler ( ) . expect ( "The event store thread should stop." ) ;
158
+ handler. join ( ) . expect ( "The event store thread should stop." ) ;
158
159
}
159
160
}
160
161
}
Original file line number Diff line number Diff line change 1
1
#![ allow( missing_docs) ]
2
2
3
+ pub trait AsyncTask {
4
+ fn join ( self : Box < Self > ) -> Result < ( ) , ( ) > ;
5
+ }
6
+
7
+ pub type AsyncTaskBoxed = Box < dyn AsyncTask > ;
8
+
9
+ struct Thread {
10
+ join : Box < dyn FnOnce ( ) -> Result < ( ) , ( ) > > ,
11
+ }
12
+
13
+ impl AsyncTask for Thread {
14
+ fn join ( self : Box < Self > ) -> Result < ( ) , ( ) > {
15
+ ( self . join ) ( )
16
+ }
17
+ }
18
+
19
+ impl Thread {
20
+ fn new ( join : Box < dyn FnOnce ( ) -> Result < ( ) , ( ) > > ) -> Self {
21
+ Thread { join }
22
+ }
23
+ }
24
+
3
25
#[ cfg( not( feature = "browser" ) ) ]
4
- pub fn spawn < F > ( f : F ) -> Box < dyn FnOnce ( ) -> Result < ( ) , ( ) > >
26
+ pub fn spawn < F > ( f : F ) -> AsyncTaskBoxed
5
27
where
6
28
F : FnOnce ( ) ,
7
29
F : Send + ' static ,
8
30
{
9
31
let handle = std:: thread:: spawn ( f) ;
10
- Box :: new ( move || handle. join ( ) . map_err ( |_| ( ) ) )
32
+ Box :: new ( Thread :: new ( Box :: new ( move || handle. join ( ) . map_err ( |_| ( ) ) ) ) )
11
33
}
12
34
13
35
#[ cfg( feature = "browser" ) ]
14
- pub fn spawn < F > ( f : F ) -> Box < dyn FnOnce ( ) -> Result < ( ) , ( ) > >
36
+ pub fn spawn < F > ( f : F ) -> AsyncTaskBoxed
15
37
where
16
38
F : FnOnce ( ) ,
17
39
F : Send + ' static ,
18
40
{
19
41
rayon:: spawn ( f) ;
20
- Box :: new ( || Ok ( ( ) ) )
42
+ Box :: new ( Thread :: new ( Box :: new ( || Ok ( ( ) ) ) ) )
21
43
}
22
44
23
45
#[ cfg( feature = "browser" ) ]
You can’t perform that action at this time.
0 commit comments