Skip to content

Commit f0297b0

Browse files
committed
Added stop and active methods to LiveMsqlSelect prototype
1 parent 26b1837 commit f0297b0

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Method Name | Arguments | Description
111111
------------|-----------|-----------------------
112112
`on`, `addListener` | `event`, `handler` | Add an event handler to the result set. See the following section for a list of the available event names.
113113
`update` | `callback` | Update the result set. Callback function accepts `error, rows` arguments. Events will be emitted.
114+
`stop` | *None* | Stop receiving updates
115+
`active` | *None* | Return `true` if ready to recieve updates, `false` if `stop()` method has been called.
114116

115117
As well as all of the other methods available on [`EventEmitter`](http://nodejs.org/api/events.html)...
116118

lib/LiveMysql.js

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ LiveMysql.prototype.select = function(query, triggers){
6868
triggers.length === 0)
6969
throw new Error('triggers array required');
7070

71+
// Update schema included in ZongJi events
7172
var includeSchema = self.zongjiSettings.includeSchema;
7273
for(var i = 0; i < triggers.length; i++){
7374
var triggerDatabase = triggers[i].database || self.settings.database;

lib/LiveMysqlSelect.js

+17
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,21 @@ LiveMysqlSelect.prototype.update = function(callback){
134134
}
135135
};
136136

137+
LiveMysqlSelect.prototype.stop = function(){
138+
var self = this;
139+
140+
var index = self.base._select.indexOf(self);
141+
if(index !== -1){
142+
self.base._select.splice(index, 1);
143+
return true;
144+
}else{
145+
return false;
146+
}
147+
};
148+
149+
LiveMysqlSelect.prototype.active = function(){
150+
var self = this;
151+
return self.base._select.indexOf(self) !== -1;
152+
};
153+
137154
module.exports = LiveMysqlSelect;

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.9",
3+
"version": "0.0.10",
44
"description": "Live updating MySQL SELECT statements",
55
"main": "lib/LiveMysql.js",
66
"repository": "https://github.com/numtel/mysql-live-select",

test/index.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,15 @@ module.exports = {
113113
}else if(rows.length === 0){
114114
// Give time, just in case the `removed` event comes in
115115
setTimeout(function(){
116+
conn.settings.skipDiff = false;
116117
test.done();
117118
}, 100);
118119
}
119120
})
120121
.on('added', error)
121122
.on('changed', error)
122-
.on('removed', error);
123+
.on('removed', error)
124+
.on('diff', error);
123125

124126
querySequence(conn.db, [
125127
'UPDATE ' + escId(table) +
@@ -130,6 +132,43 @@ module.exports = {
130132
});
131133
});
132134
},
135+
stopAndActive: function(test){
136+
var table = 'stop_active';
137+
server.on('ready', function(conn, esc, escId, queries){
138+
querySequence(conn.db, [
139+
'DROP TABLE IF EXISTS ' + escId(table),
140+
'CREATE TABLE ' + escId(table) + ' (col INT UNSIGNED)',
141+
'INSERT INTO ' + escId(table) + ' (col) VALUES (10)',
142+
], function(results){
143+
conn.select('SELECT * FROM ' + escId(table), [ {
144+
table: table,
145+
database: server.database
146+
} ]).on('update', function(rows){
147+
if(rows.length > 0 && rows[0].col === 10){
148+
test.ok(true);
149+
}else if(rows.length > 0 && rows[0].col === 15){
150+
test.ok(this.active());
151+
this.stop();
152+
test.ok(!this.active());
153+
conn.db.query('DELETE FROM ' + escId(table));
154+
setTimeout(function(){
155+
test.done();
156+
}, 100);
157+
}
158+
}).on('removed', function(row, index){
159+
throw new Error('should not be called');
160+
});
161+
162+
querySequence(conn.db, [
163+
'UPDATE ' + escId(table) +
164+
' SET `col` = 15'
165+
], function(results){
166+
// ...
167+
});
168+
});
169+
});
170+
},
171+
133172
error_no_db_selected: function(test){
134173
server.on('ready', function(conn, esc, escId, queries){
135174

0 commit comments

Comments
 (0)