From 95feff49da01e652bf3a0dd0b3ffdcf65a394ab2 Mon Sep 17 00:00:00 2001 From: Steven Hugg Date: Fri, 3 May 2019 18:58:35 -0400 Subject: [PATCH] createNewPersistentStore() calls callback whether or not it converted --- src/store.ts | 19 +++++----- src/ui.ts | 42 +++++++++++---------- test/cli/teststore.js | 86 ++++++++++++++++++++++--------------------- 3 files changed, 75 insertions(+), 72 deletions(-) diff --git a/src/store.ts b/src/store.ts index b4557834..0af8a882 100644 --- a/src/store.ts +++ b/src/store.ts @@ -4,7 +4,7 @@ //import * as localforage from "localforage"; declare var localforage; -var OldFileStore = function(storage, prefix:string) { +var Ver2xFileStore = function(storage, prefix:string) { var self = this; this.saveFile = function(name, text) { storage.setItem(prefix + name, text); @@ -31,12 +31,14 @@ var OldFileStore = function(storage, prefix:string) { } // copy localStorage to new driver -function copyFromOldStorageFormat(platformid:string, newstore, conversioncallback:()=>void) { +function copyFromVer2xStorageFormat(platformid:string, newstore, callback:(store)=>void) { var alreadyMigratedKey = "__migrated_" + platformid; //localStorage.removeItem(alreadyMigratedKey); - if (localStorage.getItem(alreadyMigratedKey)) + if (localStorage.getItem(alreadyMigratedKey)) { + callback(newstore); return; - var oldstore = new OldFileStore(localStorage, platformid + '/'); + } + var oldstore = new Ver2xFileStore(localStorage, platformid + '/'); var keys = oldstore.getFiles(''); // no files to convert? if (keys.length == 0) { @@ -60,10 +62,7 @@ function copyFromOldStorageFormat(platformid:string, newstore, conversioncallbac console.log("Migrated " + len + " local files to new data store"); if (len) { localStorage.setItem(alreadyMigratedKey, 'true'); - if (conversioncallback) - conversioncallback(); - else - window.location.reload(); + callback(newstore); } }); } @@ -73,11 +72,11 @@ function copyFromOldStorageFormat(platformid:string, newstore, conversioncallbac migrateNext(); // start the conversion } -export function createNewPersistentStore(platformid:string, callback:()=>void) { +export function createNewPersistentStore(platformid:string, callback:(store)=>void) { var store = localforage.createInstance({ name: "__" + platformid, version: 2.0 }); - copyFromOldStorageFormat(platformid, store, callback); + copyFromVer2xStorageFormat(platformid, store, callback); return store; } diff --git a/src/ui.ts b/src/ui.ts index 9a369772..b2609763 100644 --- a/src/ui.ts +++ b/src/ui.ts @@ -1376,14 +1376,15 @@ function loadSharedGist(gistkey : string) { var filename; var newid; console.log("Fetched " + gistkey, val); - store = createNewPersistentStore(platform_id, null); - for (filename in val.files) { - store.setItem('shared/'+filename, val.files[filename].content); - if (!newid) newid = 'shared/'+filename; - } - // TODO: wait for set? - delete qs['gistkey']; - reloadPresetNamed(newid); + store = createNewPersistentStore(platform_id, (store) => { + for (filename in val.files) { + store.setItem('shared/'+filename, val.files[filename].content); + if (!newid) newid = 'shared/'+filename; + } + // TODO: wait for set? + delete qs['gistkey']; + reloadPresetNamed(newid); + }); }).fail(function(err) { alert("Error loading share file: " + err.message); }); @@ -1471,18 +1472,19 @@ export function startUI(loadplatform : boolean) { // otherwise, open IDE else { // create store for platform - store = createNewPersistentStore(platform_id, null); - // is this an importURL? - if (qs['importURL']) { - loadImportedURL(qs['importURL']); - return; - } - // load and start platform object - if (loadplatform) { - loadAndStartPlatform(); - } else { - startPlatform(); - } + store = createNewPersistentStore(platform_id, (store) => { + // is this an importURL? + if (qs['importURL']) { + loadImportedURL(qs['importURL']); + return; + } + // load and start platform object + if (loadplatform) { + loadAndStartPlatform(); + } else { + startPlatform(); + } + }); } } diff --git a/test/cli/teststore.js b/test/cli/teststore.js index d3252845..7b1b25a0 100644 --- a/test/cli/teststore.js +++ b/test/cli/teststore.js @@ -53,7 +53,7 @@ describe('Store', function() { localStorage.setItem('_TEST/test', 'a'); localStorage.setItem('_TEST/local/test', 'b'); assert.equal(2, localMods); - var store = mstore.createNewPersistentStore(test_platform_id, function() { + var store = mstore.createNewPersistentStore(test_platform_id, function(store) { assert.equal('true', localItems['__migrated__TEST']); store.getItem('test', function(err, result) { if (err) done(err); @@ -71,7 +71,7 @@ describe('Store', function() { it('Should load local project', function(done) { localStorage.clear(); localStorage.setItem('_TEST/local/test', 'a'); - var store = mstore.createNewPersistentStore(test_platform_id, function() { + var store = mstore.createNewPersistentStore(test_platform_id, function(store) { var worker = {}; var platform = {}; var project = new prj.CodeProject(worker, test_platform_id, platform, store); @@ -105,23 +105,24 @@ describe('Store', function() { ] } ]; - var store = mstore.createNewPersistentStore(test_platform_id); - var worker = { - postMessage: function(m) { msgs.push(m); }, - }; - var platform = { - getToolForFilename: function(fn) { return 'dasm'; }, - }; - var project = new prj.CodeProject(worker, test_platform_id, platform, store); - project.callbackBuildStatus = function(b) { msgs.push(b) }; - project.updateFile('test.a', ' lda #0'); - project.setMainFile('test.a'); - project.updateFile('test.a', ' lda #1'); // don't send twice (yet) - assert.deepEqual(msgs, expectmsgs); - store.getItem('test.a', function(err, result) { - assert.equal(null, err); - assert.equal(' lda #1', result); - done(); + var store = mstore.createNewPersistentStore(test_platform_id, (store) => { + var worker = { + postMessage: function(m) { msgs.push(m); }, + }; + var platform = { + getToolForFilename: function(fn) { return 'dasm'; }, + }; + var project = new prj.CodeProject(worker, test_platform_id, platform, store); + project.callbackBuildStatus = function(b) { msgs.push(b) }; + project.updateFile('test.a', ' lda #0'); + project.setMainFile('test.a'); + project.updateFile('test.a', ' lda #1'); // don't send twice (yet) + assert.deepEqual(msgs, expectmsgs); + store.getItem('test.a', function(err, result) { + assert.equal(null, err); + assert.equal(' lda #1', result); + done(); + }); }); }); @@ -131,30 +132,31 @@ describe('Store', function() { localStorage.clear(); localItems['__migrated__TEST'] = 'true'; var msgs = []; - var store = mstore.createNewPersistentStore(test_platform_id); - var worker = { - }; - var platform = { - }; - var project = new prj.CodeProject(worker, test_platform_id, platform, store); - project.callbackBuildStatus = function(b) { msgs.push(b) }; - var buildresult = { - listings: { - test: { - lines: [ { line: 3, offset: 61440, insns: 'a9 00', iscode: true } ] + var store = mstore.createNewPersistentStore(test_platform_id, (store) => { + var worker = { + }; + var platform = { + }; + var project = new prj.CodeProject(worker, test_platform_id, platform, store); + project.callbackBuildStatus = function(b) { msgs.push(b) }; + var buildresult = { + listings: { + test: { + lines: [ { line: 3, offset: 61440, insns: 'a9 00', iscode: true } ] + } } - } - }; - worker.onmessage({data:buildresult}); - assert.deepEqual([false], msgs); - var lst = buildresult.listings.test; - console.log(lst); - assert.equal(3, lst.sourcefile.findLineForOffset(61440+15, 15)); - assert.equal(null, lst.sourcefile.findLineForOffset(61440+16, 15)); - assert.equal(null, lst.sourcefile.findLineForOffset(61440+1, 0)); - assert.equal(null, lst.sourcefile.findLineForOffset(61440-1, 16)); - assert.equal(1, lst.sourcefile.lineCount()); - done(); + }; + worker.onmessage({data:buildresult}); + assert.deepEqual([false], msgs); + var lst = buildresult.listings.test; + console.log(lst); + assert.equal(3, lst.sourcefile.findLineForOffset(61440+15, 15)); + assert.equal(null, lst.sourcefile.findLineForOffset(61440+16, 15)); + assert.equal(null, lst.sourcefile.findLineForOffset(61440+1, 0)); + assert.equal(null, lst.sourcefile.findLineForOffset(61440-1, 16)); + assert.equal(1, lst.sourcefile.lineCount()); + done(); + }); }); });