Skip to content

Commit ff50a83

Browse files
committed
0.0.22: LiveMysql Constructor callback now waits for Zongji ready
Fixes #10
1 parent 40829c4 commit ff50a83

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

lib/LiveMysql.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
var ZongJi = require('zongji');
44
var mysql = require('mysql');
55

6+
// Maximum duration to wait for Zongji to initialize before timeout error (ms)
7+
var ZONGJI_INIT_TIMEOUT = 1500;
8+
69
var LiveMysqlSelect = require('./LiveMysqlSelect');
710

811
function LiveMysql(settings, callback){
@@ -63,8 +66,24 @@ function LiveMysql(settings, callback){
6366

6467
});
6568

69+
// Wait for Zongji to be ready before executing callback
70+
var zongjiInitTime = Date.now();
71+
var zongjiReady = function() {
72+
if(zongji.ready === true) {
73+
// Call the callback if it exists and do not keep waiting
74+
callback && callback();
75+
} else {
76+
// Wait for Zongji to be ready
77+
if(Date.now() - zongjiInitTime > ZONGJI_INIT_TIMEOUT) {
78+
// Zongji initialization has exceeded timeout, callback error
79+
callback && callback(new Error('ZONGJI_INIT_TIMEOUT_OCCURED'));
80+
} else {
81+
setTimeout(zongjiReady, 40);
82+
}
83+
}
84+
};
6685
zongji.start(self.zongjiSettings);
67-
if(callback) return callback();
86+
zongjiReady();
6887
});
6988
}
7089

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mysql-live-select",
3-
"version": "0.0.21",
3+
"version": "0.0.22",
44
"description": "Live updating MySQL SELECT statements",
55
"main": "lib/LiveMysql.js",
66
"repository": "https://github.com/numtel/mysql-live-select",

test/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ module.exports = {
242242
});
243243
});
244244
},
245+
immediate_disconnection: function(test){
246+
var myTest = new LiveMysql(settings, function(error){
247+
myTest.end();
248+
test.ok(typeof error === 'undefined');
249+
test.done();
250+
});
251+
},
245252
error_invalid_connection: function(test){
246253
var myTest = new LiveMysql({
247254
host: '127.0.0.1',

0 commit comments

Comments
 (0)