From a2514915d3e7555918744d68515bfb8430208b7a Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 11 Jan 2017 10:42:24 +0000 Subject: [PATCH 1/2] Move Chrome syncing back to app.js --- src/app.js | 52 ++++++++++++++++++++++++++++++++++++++++------ src/app/storage.js | 43 +------------------------------------- 2 files changed, 47 insertions(+), 48 deletions(-) diff --git a/src/app.js b/src/app.js index 2f7dd3ed8f..259022c96c 100644 --- a/src/app.js +++ b/src/app.js @@ -1,4 +1,4 @@ -/* global alert, confirm, prompt, Option, Worker */ +/* global alert, confirm, prompt, Option, Worker, chrome */ 'use strict' var $ = require('jquery') @@ -39,7 +39,7 @@ window.addEventListener('message', function (ev) { var run = function () { var self = this this.event = new EventManager() - var storage = new Storage(updateFiles) + var storage = new Storage() function loadFiles (files) { for (var f in files) { @@ -84,10 +84,50 @@ var run = function () { }) }) - // ----------------- storage sync -------------------- + // ----------------- Chrome cloud storage sync -------------------- - window.syncStorage = storage.sync - storage.sync() + function chromeCloudSync () { + if (typeof chrome === 'undefined' || !chrome || !chrome.storage || !chrome.storage.sync) { + return + } + + var obj = {} + var done = false + var count = 0 + + function check (key) { + chrome.storage.sync.get(key, function (resp) { + console.log('comparing to cloud', key, resp) + if (typeof resp[key] !== 'undefined' && obj[key] !== resp[key] && confirm('Overwrite "' + utils.fileNameFromKey(key) + '"? Click Ok to overwrite local file with file from cloud. Cancel will push your local file to the cloud.')) { + console.log('Overwriting', key) + storage.set(key, resp[key]) + updateFiles() + } else { + console.log('add to obj', obj, key) + obj[key] = storage.get(key) + } + done++ + if (done >= count) { + chrome.storage.sync.set(obj, function () { + console.log('updated cloud files with: ', obj, this, arguments) + }) + } + }) + } + + for (var y in storage.keys()) { + console.log('checking', y) + obj[y] = storage.get(y) + if (!utils.isCachedFile(y)) { + continue + } + count++ + check(y) + } + } + + window.syncStorage = chromeCloudSync + chromeCloudSync() // ----------------- editor ---------------------- @@ -657,7 +697,7 @@ var run = function () { loadVersion('builtin') }) - storage.sync() + chromeCloudSync() } module.exports = { diff --git a/src/app/storage.js b/src/app/storage.js index 16d8f99e18..e457087d2a 100644 --- a/src/app/storage.js +++ b/src/app/storage.js @@ -1,9 +1,8 @@ -/* global chrome, confirm */ 'use strict' var utils = require('./utils') -function Storage (updateFiles) { +function Storage () { this.rename = function (originalName, newName) { var content = this.get(originalName) this.set(newName, content) @@ -43,46 +42,6 @@ function Storage (updateFiles) { } this.set(filename, content) } - - this.sync = function () { - if (typeof chrome === 'undefined' || !chrome || !chrome.storage || !chrome.storage.sync) { - return - } - - var obj = {} - var done = false - var count = 0 - - function check (key) { - chrome.storage.sync.get(key, function (resp) { - console.log('comparing to cloud', key, resp) - if (typeof resp[key] !== 'undefined' && obj[key] !== resp[key] && confirm('Overwrite "' + utils.fileNameFromKey(key) + '"? Click Ok to overwrite local file with file from cloud. Cancel will push your local file to the cloud.')) { - console.log('Overwriting', key) - window.localStorage.setItem(key, resp[key]) - updateFiles() - } else { - console.log('add to obj', obj, key) - obj[key] = window.localStorage[key] - } - done++ - if (done >= count) { - chrome.storage.sync.set(obj, function () { - console.log('updated cloud files with: ', obj, this, arguments) - }) - } - }) - } - - for (var y in window.localStorage) { - console.log('checking', y) - obj[y] = window.localStorage.getItem(y) - if (!utils.isCachedFile(y)) { - continue - } - count++ - check(y) - } - } } module.exports = Storage From b2cfd0245f771070af888e204fd87460beabf0ab Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 11 Jan 2017 10:44:17 +0000 Subject: [PATCH 2/2] Don't call chromeCloudSync twice --- src/app.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/app.js b/src/app.js index 259022c96c..909f2d3619 100644 --- a/src/app.js +++ b/src/app.js @@ -696,8 +696,6 @@ var run = function () { loadVersion('builtin') }) - - chromeCloudSync() } module.exports = {