Skip to content

Commit

Permalink
Adding functionality to subscribe command that exits after receving s…
Browse files Browse the repository at this point in the history
…o many events or after receiving an event with matching data. Tests are needed.
  • Loading branch information
JamesHagerman committed Apr 8, 2020
1 parent 4222580 commit 9baa6cd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/cli/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ module.exports = ({ commandProcessor, root }) => {
},
'device': {
describe: 'Listen to events from this device only'
},
'until': {
describe: 'Listen until we see an event exactly matching this data'
},
'count': {
number: true,
describe: 'Listen until we see this many events'
}
},
handler: (args) => {
Expand All @@ -18,7 +25,9 @@ module.exports = ({ commandProcessor, root }) => {
'$0 $command': 'Subscribe to all event published by my devices',
'$0 $command update': 'Subscribe to events starting with update from my devices',
'$0 $command --device x': 'Subscribe to all events published by device x',
'$0 $command --all': 'Subscribe to public events and all events published by my devices'
'$0 $command --all': 'Subscribe to public events and all events published by my devices',
'$0 $command --until x': 'Subscribe to all events and exit when an event has data matching x',
'$0 $command --count x': 'Subscribe to all events and exit after seeing x events'
}
});
};
Expand Down
25 changes: 24 additions & 1 deletion src/cmd/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const ApiClient = require('../lib/api-client');


module.exports = class SubscribeCommand {
startListening(event, { device, all }) {
startListening(event, { device, all, until, max }) {
const api = new ApiClient();
api.ensureToken();

Expand Down Expand Up @@ -35,6 +35,16 @@ module.exports = class SubscribeCommand {
console.log('Subscribing to ' + eventLabel + ' from ' + deviceId + "'s stream");
}

if (until) {
console.log(`This command will exit after receiving event data matching: '${until}'`);
}

let eventCount = 0;
if (max) {
max = Math.abs(max);
console.log(`This command will exit after receiving ${max} event(s)...`);
}

let chunks = [];
function appendToQueue(arr) {
for (let i = 0; i < arr.length; i++) {
Expand Down Expand Up @@ -64,6 +74,19 @@ module.exports = class SubscribeCommand {
}

console.log(JSON.stringify(obj));

if (until && until === obj.data) {
console.log('Matching event received. Exiting...');
process.exit(1); // One matching event
}

if (max) {
eventCount = eventCount + 1;
if (eventCount === max) {
console.log(`${eventCount} event(s) received. Exiting...`);
process.exit(eventCount); // Number of events we received
}
}
}

return api.getEventStream(eventName, deviceId, (event) => {
Expand Down

0 comments on commit 9baa6cd

Please sign in to comment.