Skip to content

Commit c72aefb

Browse files
committed
0.0.11: Update event on empty initial result set
Related to numtel/meteor-pg#4
1 parent bd29d99 commit c72aefb

File tree

4 files changed

+100
-6
lines changed

4 files changed

+100
-6
lines changed

lib/LivePG.js

+63-4
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ var LivePG = (function (_EventEmitter) {
284284
triggers: triggers,
285285
data: [],
286286
handlers: [handle],
287-
notifications: []
287+
notifications: [],
288+
initialized: false
288289
};
289290
context$2$0.next = 11;
290291
return common.getClient(this.connStr);
@@ -411,7 +412,7 @@ var LivePG = (function (_EventEmitter) {
411412
}, {
412413
key: '_updateQuery',
413414
value: function _updateQuery(queryHash) {
414-
var pgHandle, queryBuffer, update, _iteratorNormalCompletion4, _didIteratorError4, _iteratorError4, _iterator4, _step4, updateHandler;
415+
var pgHandle, queryBuffer, update, _iteratorNormalCompletion4, _didIteratorError4, _iteratorError4, _iterator4, _step4, updateHandler, _iteratorNormalCompletion5, _didIteratorError5, _iteratorError5, _iterator5, _step5;
415416

416417
return _regeneratorRuntime.async(function _updateQuery$(context$2$0) {
417418
while (1) switch (context$2$0.prev = context$2$0.next) {
@@ -431,7 +432,7 @@ var LivePG = (function (_EventEmitter) {
431432
pgHandle.done();
432433

433434
if (!(update !== null)) {
434-
context$2$0.next = 29;
435+
context$2$0.next = 31;
435436
break;
436437
}
437438

@@ -480,10 +481,68 @@ var LivePG = (function (_EventEmitter) {
480481
return context$2$0.finish(21);
481482

482483
case 29:
484+
context$2$0.next = 51;
485+
break;
486+
487+
case 31:
488+
if (!(queryBuffer.initialized === false)) {
489+
context$2$0.next = 51;
490+
break;
491+
}
492+
493+
_iteratorNormalCompletion5 = true;
494+
_didIteratorError5 = false;
495+
_iteratorError5 = undefined;
496+
context$2$0.prev = 35;
497+
498+
// Initial update with empty data
499+
for (_iterator5 = _getIterator(queryBuffer.handlers); !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
500+
updateHandler = _step5.value;
501+
502+
updateHandler.emit('update', { removed: null, moved: null, copied: null, added: [] }, []);
503+
}
504+
context$2$0.next = 43;
505+
break;
506+
507+
case 39:
508+
context$2$0.prev = 39;
509+
context$2$0.t4 = context$2$0['catch'](35);
510+
_didIteratorError5 = true;
511+
_iteratorError5 = context$2$0.t4;
512+
513+
case 43:
514+
context$2$0.prev = 43;
515+
context$2$0.prev = 44;
516+
517+
if (!_iteratorNormalCompletion5 && _iterator5['return']) {
518+
_iterator5['return']();
519+
}
520+
521+
case 46:
522+
context$2$0.prev = 46;
523+
524+
if (!_didIteratorError5) {
525+
context$2$0.next = 49;
526+
break;
527+
}
528+
529+
throw _iteratorError5;
530+
531+
case 49:
532+
return context$2$0.finish(46);
533+
534+
case 50:
535+
return context$2$0.finish(43);
536+
537+
case 51:
538+
539+
queryBuffer.initialized = true;
540+
541+
case 52:
483542
case 'end':
484543
return context$2$0.stop();
485544
}
486-
}, null, this, [[13, 17, 21, 29], [22,, 24, 28]]);
545+
}, null, this, [[13, 17, 21, 29], [22,, 24, 28], [35, 39, 43, 51], [44,, 46, 50]]);
487546
}
488547
}, {
489548
key: '_processNotification',

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name" : "pg-live-select",
3-
"version" : "0.0.10",
3+
"version" : "0.0.11",
44
"description" : "Live updating PostgreSQL SELECT statements",
55
"main" : "lib/LivePG.js",
66
"license" : "MIT",

src/LivePG.es6

+12-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ class LivePG extends EventEmitter {
151151
triggers,
152152
data : [],
153153
handlers : [ handle ],
154-
notifications : []
154+
notifications : [],
155+
initialized : false
155156
}
156157

157158
let pgHandle = await common.getClient(this.connStr)
@@ -210,6 +211,16 @@ class LivePG extends EventEmitter {
210211
filterHashProperties(update.diff), filterHashProperties(update.data))
211212
}
212213
}
214+
else if(queryBuffer.initialized === false) {
215+
// Initial update with empty data
216+
for(let updateHandler of queryBuffer.handlers) {
217+
updateHandler.emit('update',
218+
{ removed: null, moved: null, copied: null, added: [] },
219+
[])
220+
}
221+
}
222+
223+
queryBuffer.initialized = true
213224
}
214225

215226
_processNotification(payload) {

test/fixtures/variousQueries.es6

+24
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,30 @@ exports.cases.allExpression = {
508508
]
509509
}
510510

511+
// If initial result set is empty, an update event with an empty added array
512+
// is emitted.
513+
exports.cases.initialEmpty = {
514+
query: `SELECT score FROM scores WHERE score > 100 ORDER BY score DESC`,
515+
events: [
516+
{ diff: {
517+
removed: null,
518+
moved: null,
519+
copied: null,
520+
added: []
521+
} },
522+
{ data: [] },
523+
{ perform: [
524+
`UPDATE scores SET score = 200 WHERE id = 3`
525+
] },
526+
{ diff: {
527+
removed: null,
528+
moved: null,
529+
copied: null,
530+
added: [ { score: 200, _index: 1 } ]
531+
} }
532+
]
533+
}
534+
511535
exports.cases.sortMoved = {
512536
query: `SELECT score, assignment_id AS assign FROM scores ORDER BY score DESC`,
513537
events: [

0 commit comments

Comments
 (0)