Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a blank Future in Dart + how do I return a future currently in progress?

I'm trying to create a server-side Dart class that performs various data-related tasks. All of these tasks rely on the database having been first initialized. The problem is that the init of the database happens asynchronously (returns a Future). I first tried to put the init code into the constructor, but have given up on this approach as it seems to not be viable.

I am now attempting to figure out how to force the DB initialization as a first step in any method call that accesses data. So in other words, when attemptLogin() is called below, I'd like to first check if the DB has been initialized and initialize it if necessary.

However, there are two obstacles. If the database hasn't been initialized, the code is straightforward - initialize the db, then use the then() method of the returned future to do the rest of the function. If the db is not yet initialized, what do I attach my then() method to?

Second related question is what happens when a database is currently being initialized but this process is not yet complete? How can I pull in and return this "in-progress" Future?

This is the basic gist of the code I'm trying to wrangle:

class DataManager {
  bool DbIsReady = false;
  bool InitializingDb = false;
  Db _db;

  Future InitMongoDB() {
    print("Initializing MongoDB");
    InitializingDb = true;
    _db = new Db("mongodb://127.0.0.1/test");
    return _db.open().then((_) {
      DbIsReady = true;
      InitializingDb = false;
    });
  }  

  Future<List> attemptLogin(String username, String password) {
    Future firstStep;
    if ((!DbIsReady) && (!InitializingDb) {
       Future firstStep = InitMongoDB()
    }
    else if (InitializingDb) {
       // Need to return the InitMongoDB() Future that's currently running, but how?
    }
    else {
       // How do I create a blank firstStep here? 
    }

    return firstStep.then((_) {
      users = _db.collection("users");
      return // ... rest of code cut out for clarity
    });
  }
}

Thanks in advance for your help,

Greg

like image 749
Greg Sherman Avatar asked Jul 10 '14 14:07

Greg Sherman


2 Answers

Just return

return new Future<bool>.value(true); 
// or any other value instead of `true` you want to return.
// or none
// return new Future.value();
like image 196
Günter Zöchbauer Avatar answered Nov 02 '22 05:11

Günter Zöchbauer


Just keep the future alive:

class DataManager {
  Future _initializedDb;

  Future initMongoDb() { ... }

  Future<List> attemptLogin(String username, String password) {
    if (_initializedDb == null) {
       _initializedDb = initMongoDB();
    }
    return _initializedDb.then((db) {
      users = db.collection("users");
      return // ... rest of code cut out for clarity
    });
  }
}

You might need to pay attention for the error-case. It's up to you if you want to deal with errors in the initMongoDB or after it.

like image 20
Florian Loitsch Avatar answered Nov 02 '22 06:11

Florian Loitsch