Skip to content

Instantly share code, notes, and snippets.

@Neal
Last active March 9, 2016 17:14
Show Gist options
  • Save Neal/7688920 to your computer and use it in GitHub Desktop.
Save Neal/7688920 to your computer and use it in GitHub Desktop.
PebbleKit JS AppMessage queue.
var appMessageQueue = {
queue: [],
numTries: 0,
maxTries: 5,
working: false,
clear: function() {
this.queue = [];
this.working = false;
},
isEmpty: function() {
return this.queue.length === 0;
},
nextMessage: function() {
return this.isEmpty() ? {} : this.queue[0];
},
send: function(message) {
if (message) this.queue.push(message);
if (this.working) return;
if (this.queue.length > 0) {
this.working = true;
var ack = function() {
appMessageQueue.numTries = 0;
appMessageQueue.queue.shift();
appMessageQueue.working = false;
appMessageQueue.send();
};
var nack = function() {
appMessageQueue.numTries++;
appMessageQueue.working = false;
appMessageQueue.send();
};
if (this.numTries >= this.maxTries) {
console.log('Failed sending AppMessage: ' + JSON.stringify(this.nextMessage()));
ack();
}
console.log('Sending AppMessage: ' + JSON.stringify(this.nextMessage()));
Pebble.sendAppMessage(this.nextMessage(), ack, nack);
}
}
};
//
// Now use appMessageQueue.send() instead of Pebble.sendAppMessage() to add it to the end of the queue and send.
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment