@@ -10,13 +10,19 @@ use tracing::trace;
1010use tracing:: { debug, error} ;
1111use url:: Url ;
1212
13- pub type UpdateCallback = Box < dyn Fn ( Repository , Repository ) + Send > ;
13+ pub type UpdateCallback = Box < dyn Fn ( Repository , Repository , SyncType ) + Send > ;
1414
1515#[ derive( Debug , Clone ) ]
1616pub struct Synchronizer {
1717 inner : Arc < Inner > ,
1818}
1919
20+ #[ derive( Debug ) ]
21+ pub enum SyncType {
22+ Realtime ,
23+ Polling ,
24+ }
25+
2026struct Inner {
2127 toggles_url : Url ,
2228 refresh_interval : Duration ,
@@ -77,7 +83,9 @@ impl Synchronizer {
7783
7884 let is_timeout = Self :: init_timeout_fn ( start_wait, interval_duration, start) ;
7985 std:: thread:: spawn ( move || loop {
80- if let Some ( r) = Self :: should_send ( inner. sync_now ( ) , & is_timeout, is_send) {
86+ if let Some ( r) =
87+ Self :: should_send ( inner. sync_now ( SyncType :: Polling ) , & is_timeout, is_send)
88+ {
8189 is_send = true ;
8290 let _ = tx. try_send ( r) ;
8391 }
@@ -105,7 +113,7 @@ impl Synchronizer {
105113 tokio:: spawn ( async move {
106114 let mut interval = tokio:: time:: interval ( inner. refresh_interval ) ;
107115 loop {
108- let result = inner. sync_now ( ) . await ;
116+ let result = inner. sync_now ( SyncType :: Polling ) . await ;
109117
110118 if let Some ( r) = Self :: should_send ( result, & is_timeout, is_send) {
111119 is_send = true ;
@@ -135,8 +143,8 @@ impl Synchronizer {
135143 }
136144
137145 #[ cfg( test) ]
138- fn notify_update ( & self , old_repo : Repository , new_repo : Repository ) {
139- self . inner . notify_update ( old_repo, new_repo)
146+ fn notify_update ( & self , old_repo : Repository , new_repo : Repository , t : SyncType ) {
147+ self . inner . notify_update ( old_repo, new_repo, t )
140148 }
141149
142150 fn init_timeout_fn (
@@ -172,17 +180,17 @@ impl Synchronizer {
172180 }
173181
174182 #[ cfg( all( feature = "use_tokio" , feature = "realtime" ) ) ]
175- pub async fn sync_now ( & self ) -> Result < ( ) , FPError > {
176- self . inner . sync_now ( ) . await
183+ pub async fn sync_now ( & self , t : SyncType ) -> Result < ( ) , FPError > {
184+ self . inner . sync_now ( t ) . await
177185 }
178186}
179187
180188impl Inner {
181189 #[ cfg( feature = "use_tokio" ) ]
182- pub async fn sync_now ( & self ) -> Result < ( ) , FPError > {
190+ pub async fn sync_now ( & self , t : SyncType ) -> Result < ( ) , FPError > {
183191 use http:: header:: USER_AGENT ;
184192
185- trace ! ( "sync now {:?}" , self . auth) ;
193+ trace ! ( "sync_now {:?} {:?}" , self . auth, t ) ;
186194 let mut request = self
187195 . client
188196 . request ( Method :: GET , self . toggles_url . clone ( ) )
@@ -213,7 +221,7 @@ impl Inner {
213221 let old = ( * repo) . clone ( ) ;
214222 let new = r. clone ( ) ;
215223 * repo = r;
216- self . notify_update ( old, new) ;
224+ self . notify_update ( old, new, t ) ;
217225 }
218226 let mut is_init = self . is_init . write ( ) ;
219227 * is_init = true ;
@@ -225,8 +233,8 @@ impl Inner {
225233 }
226234
227235 #[ cfg( feature = "use_std" ) ]
228- pub fn sync_now ( & self ) -> Result < ( ) , FPError > {
229- trace ! ( "sync_now {:?}" , self . auth) ;
236+ pub fn sync_now ( & self , t : SyncType ) -> Result < ( ) , FPError > {
237+ trace ! ( "sync_now {:?}, {:?} " , self . auth, t ) ;
230238 //TODO: report failure
231239 let mut request = ureq:: get ( self . toggles_url . as_str ( ) )
232240 . set (
@@ -258,7 +266,7 @@ impl Inner {
258266 let old = ( * repo) . clone ( ) ;
259267 let new = r. clone ( ) ;
260268 * repo = r;
261- self . notify_update ( old, new) ;
269+ self . notify_update ( old, new, t ) ;
262270 }
263271 let mut is_init = self . is_init . write ( ) ;
264272 * is_init = true ;
@@ -270,10 +278,10 @@ impl Inner {
270278 }
271279 }
272280
273- fn notify_update ( & self , old_repo : Repository , new_repo : Repository ) {
281+ fn notify_update ( & self , old_repo : Repository , new_repo : Repository , t : SyncType ) {
274282 let lock = self . update_callback . lock ( ) ;
275283 if let Some ( cb) = & * lock {
276- cb ( old_repo, new_repo)
284+ cb ( old_repo, new_repo, t )
277285 }
278286 }
279287}
@@ -291,10 +299,10 @@ mod tests {
291299 let mut syncer = build_synchronizer ( 9000 ) ;
292300 let ( tx, rx) = channel ( ) ;
293301
294- syncer. set_update_callback ( Box :: new ( move |_old, _new| tx. send ( ( ) ) . unwrap ( ) ) ) ;
302+ syncer. set_update_callback ( Box :: new ( move |_old, _new, _ | tx. send ( ( ) ) . unwrap ( ) ) ) ;
295303 let old = Repository :: default ( ) ;
296304 let new = Repository :: default ( ) ;
297- syncer. notify_update ( old, new) ;
305+ syncer. notify_update ( old, new, SyncType :: Polling ) ;
298306
299307 assert ! ( rx. try_recv( ) . is_ok( ) )
300308 }
0 commit comments