1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-22 14:33:51 +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,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();
}
});
}
}

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,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();
});
});
});