@chris-lewis/fitbit-utils
Version:
Common utility modules for common tasks across projects.
43 lines (40 loc) • 1.4 kB
JavaScript
import { outbox } from 'file-transfer';
import * as cbor from 'cbor';
import * as messaging from 'messaging';
/**
* Send a message through the peerSocket.
* Appears to be less reliable then file-transfer.
*
* @param {Object} data - JSON object data to send.
*/
export var sendMessage = function sendMessage(data) {
return messaging.peerSocket.send(data);
};
/**
* Send a JSON object as a file. Appears to be almost always reliable,
* allowing a couple of seconds typical to transfer.
*
* @param {Object} data - JSON object data to send.
* @param {string} [fileName] - Optional explicit file name.
* @returns {Promise}
*/
export var sendFile = function sendFile(data) {
var fileName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'data.json';
return outbox.enqueue(fileName, cbor.encode(data)).then(function (res) {
return console.log("File '".concat(fileName, "' enqueued"));
});
};
/**
* Setup handlers for messaging and file-transfer events.
*
* @param {Object} handlers - Handlers for socket open, socket message,
* errors, and file received events.
*/
export var setup = function setup(handlers) {
var open = handlers.open,
message = handlers.message,
error = handlers.error;
messaging.peerSocket.onopen = open;
messaging.peerSocket.onmessage = message;
messaging.peerSocket.onerror = error;
};