thinky icon indicating copy to clipboard operation
thinky copied to clipboard

on('ready',function(){

Open doomedramen opened this issue 10 years ago • 8 comments

I have tests running on travis-ci but I have to put the timeout on them to 5000> because the first test that runs causes the tables to be created, is there a way to call the auto table creation with a callback so i only run the tests once its ready?

Thanks

doomedramen avatar Oct 19 '15 10:10 doomedramen

The ready event is available for Model, not globally at the moment. It shouldn't be too hard to add though.

neumino avatar Oct 19 '15 15:10 neumino

Would it be possible to call the initialisation of the database/tables manually and then get the callback rather wait for the the first time the db is called?

something lilke:

thinky.ensureReady(function(){ console.log('it is ready'); })

so if its already ...ready it will just call the cb and if its not ready it will do it then call the cb.

doomedramen avatar Oct 19 '15 15:10 doomedramen

Models are available in thinky.models, and you can call ready that will return a promise which will be resolved when the table and its indexes are available.

neumino avatar Oct 19 '15 15:10 neumino

For now I have done this:

var thinky = require('./lib/thinky');
var modelTest = require('./models/project');

var modelCount = 0;
var readyCount = 0;

var calledCallback = false;

var cb = function () {
  console.log('all models are ready');
};

for (var model in thinky.models) {
  if (thinky.models.hasOwnProperty(model)) {
    modelCount++;
    var self = thinky.models[model];
    self.on('ready', function () {
      readyCount++;
      if (readyCount == readyCount && !calledCallback) {
        calledCallback = true;
        cb();
      }
    });
  }
}

when all models are 'ready' it calls the callback.

I will put this in the before function of my tests

doomedramen avatar Oct 19 '15 16:10 doomedramen

Another way to do it is also:

var promises = [];
for(var name in thinky.models) {
  promises.push(thinky.models[name].ready());
}
Promise.all(promises).then(function() {
  // thinky is ready
})

neumino avatar Oct 20 '15 03:10 neumino

That's much tidier, cheers :)

On Tue, 20 Oct 2015, 04:33 Michel [email protected] wrote:

Another way to do it is also:

var promises = []; for(var name in thinky.models) { promises.push(thinky.models[name].ready()); } Promise.all(promises).then(function() { // thinky is ready })

— Reply to this email directly or view it on GitHub https://github.com/neumino/thinky/issues/368#issuecomment-149419233.

doomedramen avatar Oct 21 '15 09:10 doomedramen

hmmm... any idea why 'ready()' does not exist? code

...
before(function (done) {
    var promises = [];
    for (var name in thinky.models) {
      console.log(name);
      console.log(thinky.models[name]);
      promises.push(thinky.models[name].ready());
    }
    Promise.all(promises).then(function () {
      done();
    })
  });
...

output:

Project
{ [Function: model]
  domain: [Function],
  _events: [Function],
  _maxListeners: [Function],
  setMaxListeners: [Function],
  emit: [Function],
  addListener: [Function],
  on: [Function],
  once: [Function],
  removeListener: [Function],
  removeAllListeners: [Function],
  listeners: [Function] }
TypeError: undefined is not a function
    at Context.<anonymous> (test/server.js:23:41)

doomedramen avatar Nov 02 '15 11:11 doomedramen

ignore me!, i was a few versions behind.

doomedramen avatar Nov 02 '15 12:11 doomedramen