Skip to content

Commit e71953b

Browse files
committed
1.0.3: Introduce checkConditionWhenQueued setting
#16
1 parent 346a98d commit e71953b

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Setting | Type | Description
5151
--------|------|------------------------------
5252
`serverId` | `integer` | [Unique number (1 - 2<sup>32</sup>)](http://dev.mysql.com/doc/refman/5.0/en/replication-options.html#option_mysqld_server-id) to identify this replication slave instance. Must be specified if running more than one instance.<br>**Default:** `1`
5353
`minInterval` | `integer` | Pass a number of milliseconds to use as the minimum between result set updates. Omit to refresh results on every update. May be changed at runtime.
54+
`checkConditionWhenQueued` | `boolean` | Set to `true` to call the condition function of a query on every binlog row change event. By default (when undefined or `false`), the condition function will not be called again when a query is already queued to be refreshed. Enabling this can be useful if external caching of row changes.
5455

5556
```javascript
5657
// Example:

lib/LiveMysql.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ function LiveMysql(settings, callback){
3737

3838
Object.keys(self._queryCache).forEach(function(query) {
3939
var curCache = self._queryCache[query];
40-
if(curCache.updateTimeout === null && curCache.matchRowEvent(event)){
40+
if((self.settings.checkConditionWhenQueued
41+
|| curCache.updateTimeout === null)
42+
&& curCache.matchRowEvent(event)){
4143
curCache.invalidate();
4244
}
4345
});

package.json

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

test/index.js

+60
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,66 @@ module.exports = {
217217
});
218218
});
219219
},
220+
checkConditionWhenQueued: function(test) {
221+
var table = 'check_condition_when_queued';
222+
server.on('ready', function(conn, esc, escId, queries) {
223+
// The following line should make no change but it is here for
224+
// explicitness
225+
conn.settings.checkConditionWhenQueued = false;
226+
227+
querySequence(conn.db, [
228+
'DROP TABLE IF EXISTS ' + escId(table),
229+
'CREATE TABLE ' + escId(table) + ' (col INT UNSIGNED)',
230+
'INSERT INTO ' + escId(table) + ' (col) VALUES (10)',
231+
], function(results) {
232+
var conditionCountUnder1000 = 0;
233+
var conditionCountOver1000 = 0;
234+
conn.select('SELECT * FROM ' + escId(table), [ {
235+
table: table,
236+
database: server.database,
237+
condition: function(row, newRow, rowDeleted) {
238+
if(newRow.col < 1000) {
239+
// Under 1000, checkConditionWhenQueued is false
240+
// Will not bother rechecking the condition when query is
241+
// queued to be refreshed already
242+
conditionCountUnder1000++;
243+
} else {
244+
// Over 1000, checkConditionWhenQueued is true
245+
// Condition will be checked with every row that changes
246+
conditionCountOver1000++;
247+
}
248+
return true;
249+
}
250+
} ]).on('update', function(diff, rows){
251+
if(rows.length > 0 && rows[0].col === 2000){
252+
conn.settings.checkConditionWhenQueued = false;
253+
test.equal(conditionCountUnder1000, 1);
254+
test.equal(conditionCountOver1000, 4);
255+
test.done();
256+
}
257+
});
258+
259+
querySequence(conn.db, [
260+
'UPDATE ' + escId(table) + ' SET `col` = `col` + 5',
261+
'UPDATE ' + escId(table) + ' SET `col` = `col` + 5',
262+
'UPDATE ' + escId(table) + ' SET `col` = `col` + 5',
263+
'UPDATE ' + escId(table) + ' SET `col` = 1000',
264+
], function(results){
265+
// Should have only had one condition function call at this point
266+
conn.settings.checkConditionWhenQueued = true;
267+
268+
querySequence(conn.db, [
269+
'UPDATE ' + escId(table) + ' SET `col` = `col` + 5',
270+
'UPDATE ' + escId(table) + ' SET `col` = `col` + 5',
271+
'UPDATE ' + escId(table) + ' SET `col` = `col` + 5',
272+
'UPDATE ' + escId(table) + ' SET `col` = 2000',
273+
], function(results){
274+
// Should have seen all of these updates in condition function
275+
});
276+
});
277+
});
278+
});
279+
},
220280
pauseAndResume: function(test) {
221281
var waitTime = 500;
222282
var table = 'pause_resume';

0 commit comments

Comments
 (0)