From 233b22c7054841f7099bfdf1aea5a5e812ba5e42 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 12 Apr 2026 20:52:50 +0300 Subject: Task 72: add CSV AppMessage backup --- src/pkjs/index.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/pkjs/index.js (limited to 'src/pkjs/index.js') diff --git a/src/pkjs/index.js b/src/pkjs/index.js new file mode 100644 index 0000000..bc8777e --- /dev/null +++ b/src/pkjs/index.js @@ -0,0 +1,45 @@ +var Keys = require('message_keys'); + +var CSV_STORAGE_KEY = 'fastforge.backup.csv'; +var CSV_UPDATED_AT_KEY = 'fastforge.backup.csv.updated_at'; + +function ensureArraySize(rows, total) { + if (!rows || rows.length !== total) { + return new Array(total); + } + return rows; +} + +function storeCsv(rows) { + var csv = rows.join('\n'); + localStorage.setItem(CSV_STORAGE_KEY, csv); + localStorage.setItem(CSV_UPDATED_AT_KEY, new Date().toISOString()); + console.log('FastForge backup stored, bytes=' + csv.length); +} + +function onAppMessage(event) { + var payload = event.payload || {}; + var sequence = payload[Keys.EXPORT_SEQUENCE]; + var total = payload[Keys.EXPORT_TOTAL]; + var row = payload[Keys.EXPORT_ROW]; + + if (typeof sequence !== 'number' || typeof total !== 'number' || typeof row !== 'string') { + console.log('FastForge backup chunk missing fields: ' + JSON.stringify(payload)); + return; + } + + var rows = ensureArraySize(onAppMessage.rows, total); + rows[sequence] = row; + onAppMessage.rows = rows; + + if (sequence + 1 === total) { + storeCsv(rows); + onAppMessage.rows = null; + } +} + +Pebble.addEventListener('ready', function() { + console.log('FastForge companion ready'); +}); + +Pebble.addEventListener('appmessage', onAppMessage); -- cgit v1.2.3