Skip to content

Commit 8dcfebf

Browse files
committed
0.1.7 Fix flickering with MysqlSubscription.change() method
* Also update node-mysql to 2.6.1
1 parent d8be84e commit 8dcfebf

File tree

5 files changed

+72
-50
lines changed

5 files changed

+72
-50
lines changed

.versions

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ id-map@1.0.3
1616
insecure@1.0.3
1717
jquery@1.11.3_2
1818
json@1.0.3
19-
local-test:numtel:mysql@0.1.6
19+
local-test:numtel:mysql@0.1.7
2020
logging@1.0.7
2121
meteor@1.1.6
2222
minifiers@1.1.5
2323
minimongo@1.0.8
2424
mongo@1.1.0
25-
numtel:mysql@0.1.6
25+
numtel:mysql@0.1.7
2626
observe-sequence@1.0.6
2727
ordered-dict@1.0.3
2828
random@1.0.3

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The prototype inherits from `Array` and is extended with the following methods:
4040

4141
Name | Description
4242
-----|--------------------------
43-
`change([args...])` | Change the subscription's arguments. Publication name and connection are preserved. *UI flickering may occur, please stand-by until I update this package with an improved diffing algorithm that will fix this.*
43+
`change([args...])` | Change the subscription's arguments. Publication name and connection are preserved.
4444
`addEventListener(eventName, listener)` | Bind a listener function to this subscription
4545
`removeEventListener(eventName)` | Remove listener functions from an event queue
4646
`dispatchEvent(eventName, [args...])` | Call the listeners for a given event, returns boolean

lib/LiveMysql.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,31 @@ LiveMysql.LiveMysqlSelect.prototype._publishCursor = function(sub) {
2323
fields: { reset: true }
2424
});
2525

26+
// Send aggregation of differences
27+
self.on('diff', function(diff){
28+
sub._session.send({
29+
msg: 'added',
30+
collection: sub._name,
31+
id: sub._subscriptionId,
32+
fields: { diff: diff }
33+
});
34+
});
35+
36+
// Mark subscription as ready if no data
2637
self.on('update', function(rows){
2738
if(sub._ready === false){
2839
initLength = rows.length;
2940
if(initLength === 0) fut['return']();
3041
}
3142
});
3243

33-
function selectHandler(eventName, fieldArgument, indexArgument, customAfter){
34-
// Events from mysql-live-select are the same names as the DDP msg types
35-
self.on(eventName, function(/* row, [newRow,] index */){
36-
sub._session.send({
37-
msg: eventName,
38-
collection: sub._name,
39-
id: sub._subscriptionId + ':' + arguments[indexArgument],
40-
fields: fieldArgument !== null ? arguments[fieldArgument] : undefined
41-
});
42-
if(customAfter) customAfter();
43-
});
44-
}
45-
46-
selectHandler('added', 0, 1, function(){
44+
// Mark subscription as ready if data is loaded
45+
self.on('added', function(row, index){
4746
if(sub._ready === false &&
4847
self.data.length === initLength - 1){
4948
fut['return']();
5049
}
5150
});
52-
selectHandler('changed', 1, 2);
53-
selectHandler('removed', null, 1);
5451

5552
return fut.wait()
5653
}

lib/MysqlSubscription.js

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ MysqlSubscription = function(connection, name /* arguments */){
4444
connection: connection,
4545
name: name,
4646
subscriptionId: self.subscriptionId,
47-
instance: self
47+
instance: self,
48+
resetOnDiff: false
4849
});
4950

5051
// If first store for this subscription name, register it!
@@ -58,37 +59,59 @@ MysqlSubscription = function(connection, name /* arguments */){
5859

5960
var registerStore = function(connection, name){
6061
connection.registerStore(name, {
61-
beginUpdate: function(batchSize, reset){},
62+
beginUpdate: function(){},
6263
update: function(msg){
63-
var idSplit = msg.id.split(':');
64-
var sub = _.filter(buffer, function(sub){
65-
return sub.subscriptionId === idSplit[0];
66-
})[0].instance;
67-
if(idSplit.length === 1 && msg.msg === 'added' &&
64+
var subBuffer = _.filter(buffer, function(sub){
65+
return sub.subscriptionId === msg.id;
66+
})[0];
67+
var sub = subBuffer.instance;
68+
69+
if(msg.msg === 'added' &&
6870
msg.fields && msg.fields.reset === true){
6971
// This message indicates a reset of a result set
70-
sub.dispatchEvent('reset', msg);
71-
sub.splice(0, sub.length);
72-
}else{
73-
var index = parseInt(idSplit[1], 10);
74-
var oldRow;
75-
sub.dispatchEvent('update', index, msg);
76-
switch(msg.msg){
77-
case 'added':
78-
sub.splice(index, 0, msg.fields);
79-
sub.dispatchEvent(msg.msg, index, msg.fields);
80-
break;
81-
case 'changed':
82-
oldRow = _.clone(sub[index]);
83-
sub[index] = _.extend(sub[index], msg.fields);
84-
sub.dispatchEvent(msg.msg, index, oldRow, sub[index]);
85-
break;
86-
case 'removed':
87-
oldRow = _.clone(sub[index]);
88-
sub.splice(index, 1);
89-
sub.dispatchEvent(msg.msg, index, oldRow);
90-
break;
72+
if(subBuffer.resetOnDiff === false){
73+
sub.dispatchEvent('reset', msg);
74+
sub.splice(0, sub.length);
75+
}
76+
}else if(msg.msg === 'added' &&
77+
msg.fields && 'diff' in msg.fields){
78+
// Aggregation of changes has arrived
79+
80+
if(subBuffer.resetOnDiff === true){
81+
sub.splice(0, sub.length);
82+
subBuffer.resetOnDiff = false;
9183
}
84+
85+
msg.fields.diff.forEach(function(event){
86+
var index = event.pop();
87+
var oldRow;
88+
89+
// Provide generic update event
90+
sub.dispatchEvent('update', index, {
91+
msg: event[0],
92+
collection: msg.collection,
93+
id: msg.id,
94+
fields: event[0] === 'update' ? event[2] : event[1]
95+
});
96+
97+
// Provide specific change event and perform change
98+
switch(event[0]){
99+
case 'added':
100+
sub.splice(index, 0, event[1]);
101+
sub.dispatchEvent(event[0], index, event[1]);
102+
break;
103+
case 'changed':
104+
oldRow = _.clone(sub[index]);
105+
sub[index] = _.extend(sub[index], event[2]);
106+
sub.dispatchEvent(event[0], index, oldRow, sub[index]);
107+
break;
108+
case 'removed':
109+
oldRow = _.clone(sub[index]);
110+
sub.splice(index, 1);
111+
sub.dispatchEvent(event[0], index, oldRow);
112+
break;
113+
}
114+
});
92115
}
93116
sub.changed();
94117
},
@@ -123,6 +146,8 @@ MysqlSubscription.prototype.change = function(/* arguments */){
123146
var subsNew = _.difference(_.keys(connection._subscriptions), subsBefore);
124147
if(subsNew.length !== 1) throw new Error('Subscription failed!');
125148
self.subscriptionId = selfBuffer.subscriptionId = subsNew[0];
149+
150+
selfBuffer.resetOnDiff = true;
126151
};
127152

128153
MysqlSubscription.prototype._eventRoot = function(eventName){

package.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Package.describe({
22
name: 'numtel:mysql',
33
summary: 'MySQL support with Reactive Select Subscriptions',
4-
version: '0.1.6',
4+
version: '0.1.7',
55
git: 'https://github.com/numtel/meteor-mysql.git'
66
});
77

88
Npm.depends({
9-
'mysql': '2.5.5',
10-
'mysql-live-select': '0.0.17'
9+
'mysql': '2.6.1',
10+
'mysql-live-select': '0.0.18'
1111
});
1212

1313
Package.onUse(function(api) {

0 commit comments

Comments
 (0)