/// Setups local and remote database and returns them as one Connection struct.
/// Also, binds on change events of the database to the given closures.
Setups local and remote database and returns them as one Connection struct.
Also, binds on change events of the database to the given closures.
Setups local and remote database and returns them as one Connection struct . Also binds on change events of the database to the given closures .
fn build_connection(
        &self, name: String,
        change: &Closure<dyn FnMut(JsValue)>, change_error: &Closure<dyn FnMut(JsValue)>,
        sync: &Closure<dyn FnMut(JsValue)>, sync_error: &Closure<dyn FnMut(JsValue)>
    ) -> Connection {
        // Setup local database
        let settings = Settings { adapter: "idb".to_string() };
        let local_db_name = &format!("{}-{}", self.user.borrow().as_ref().unwrap(), &name);
        let local_db = PouchDB::new(local_db_name, &JsValue::from_serde(&settings).unwrap());
        // Setup changes feed for database
        let changes_feed = local_db.changes();
        // On change functions need to be bound to the changes feed
        changes_feed.on_change(change);
        changes_feed.on_error(change_error);
        // Create variable that contains the remote database or none
        let mut remote_db = None;
        // When online, setup remote database and sync handler
        let sync_handler = if self.service_status.borrow().as_str() == "online" {
            // Init remote databases
            let remote_db_here = PouchDB::new_with_name(&format!("{}/-{}",
                 &self.database_url.borrow().as_ref().unwrap(), &name));
            // Get Sync Handler
            let sync_handler: SyncHandler = local_db.sync(&remote_db_here);
            // Bind on change functions
            sync_handler.on_change(sync);
            sync_handler.on_error(sync_error);
            // Save new remote database
            remote_db = Some(remote_db_here);
            // Return sync handler
            Some(sync_handler)
        } else {
            None
        };
        // Create struct that holds connection information and return it
        Connection {
            local_db,
            remote_db,
            changes_feed,
            sync_handler
        }
    }
fn build_connection ( & self , name : String , change : & Closure < dyn FnMut ( JsValue ) > , change_error : & Closure < dyn FnMut ( JsValue ) > , sync : & Closure < dyn FnMut ( JsValue ) > , sync_error : & Closure < dyn FnMut ( JsValue ) > ) -> Connection { // Setup local database let settings = Settings { adapter : " " . to_string ( ) } ; let local_db_name = & format ! ( " " self user borrow ( ) as_ref ( ) unwrap ( ) name ) ; let local_db = PouchDB :: new ( local_db_name , & JsValue :: from_serde ( & settings ) . unwrap ( ) ) ; // Setup changes feed for database let changes_feed = local_db . changes ( ) ; // On change functions need to be bound to the changes feed changes_feed . on_change ( change ) ; changes_feed . on_error ( change_error ) ; // Create variable that contains the remote database or none let mut remote_db = None ; // When online, setup remote database and sync handler let sync_handler = if self . service_status . borrow ( ) . as_str ( ) == " " { // Init remote databases let remote_db_here = PouchDB :: new_with_name ( & format ! ( " " self database_url borrow ( ) as_ref ( ) unwrap ( ) name ) ) ; // Get Sync Handler let sync_handler : SyncHandler = local_db . sync ( & remote_db_here ) ; // Bind on change functions sync_handler . on_change ( sync ) ; sync_handler . on_error ( sync_error ) ; // Save new remote database remote_db = Some ( remote_db_here ) ; // Return sync handler Some ( sync_handler ) } else { None } ; // Create struct that holds connection information and return it Connection { local_db , remote_db , changes_feed , sync_handler } }
Setups local and remote database and returns them as one Connection struct.
Setups local and remote database and returns them as one Connection struct .
&self
name
String
change
&Closure<dyn FnMut(JsValue)>
change_error
&Closure<dyn FnMut(JsValue)>
sync
&Closure<dyn FnMut(JsValue)>
sync_error
&Closure<dyn FnMut(JsValue)>