1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2025-02-16 17:30:27 +00:00

createNewPersistentStore() calls callback whether or not it converted

This commit is contained in:
Steven Hugg 2019-05-03 18:58:35 -04:00
parent 02ec5c7d04
commit 95feff49da
3 changed files with 75 additions and 72 deletions

View File

@ -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;
}

View File

@ -1376,7 +1376,7 @@ function loadSharedGist(gistkey : string) {
var filename;
var newid;
console.log("Fetched " + gistkey, val);
store = createNewPersistentStore(platform_id, null);
store = createNewPersistentStore(platform_id, (store) => {
for (filename in val.files) {
store.setItem('shared/'+filename, val.files[filename].content);
if (!newid) newid = 'shared/'+filename;
@ -1384,6 +1384,7 @@ function loadSharedGist(gistkey : string) {
// TODO: wait for set?
delete qs['gistkey'];
reloadPresetNamed(newid);
});
}).fail(function(err) {
alert("Error loading share file: " + err.message);
});
@ -1471,7 +1472,7 @@ export function startUI(loadplatform : boolean) {
// otherwise, open IDE
else {
// create store for platform
store = createNewPersistentStore(platform_id, null);
store = createNewPersistentStore(platform_id, (store) => {
// is this an importURL?
if (qs['importURL']) {
loadImportedURL(qs['importURL']);
@ -1483,6 +1484,7 @@ export function startUI(loadplatform : boolean) {
} else {
startPlatform();
}
});
}
}

View File

@ -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,7 +105,7 @@ describe('Store', function() {
]
}
];
var store = mstore.createNewPersistentStore(test_platform_id);
var store = mstore.createNewPersistentStore(test_platform_id, (store) => {
var worker = {
postMessage: function(m) { msgs.push(m); },
};
@ -124,6 +124,7 @@ describe('Store', function() {
done();
});
});
});
// lines: [ { line: 3, offset: 61440, insns: 'a9 00', iscode: true } ] }
@ -131,7 +132,7 @@ describe('Store', function() {
localStorage.clear();
localItems['__migrated__TEST'] = 'true';
var msgs = [];
var store = mstore.createNewPersistentStore(test_platform_id);
var store = mstore.createNewPersistentStore(test_platform_id, (store) => {
var worker = {
};
var platform = {
@ -156,5 +157,6 @@ describe('Store', function() {
assert.equal(1, lst.sourcefile.lineCount());
done();
});
});
});