diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-12 20:52:50 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-12 20:52:50 +0300 |
| commit | 233b22c7054841f7099bfdf1aea5a5e812ba5e42 (patch) | |
| tree | b95f9e649068dbe75790d239824e19f4a2b5ef6d /src/pkjs/index.js | |
| parent | d23ac3e5c82d53050dbd942d6a665ea66b68080c (diff) | |
Task 72: add CSV AppMessage backup
Diffstat (limited to 'src/pkjs/index.js')
| -rw-r--r-- | src/pkjs/index.js | 45 |
1 files changed, 45 insertions, 0 deletions
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); |
