mirror of
https://github.com/sehugg/8bitworkshop.git
synced 2024-06-01 05:41:31 +00:00
convert loadFiles() to Promise
This commit is contained in:
parent
03d85db4d0
commit
9aba7ac296
|
@ -6,7 +6,6 @@ import { Platform } from "./baseplatform";
|
||||||
|
|
||||||
type BuildResultCallback = (result:WorkerResult) => void;
|
type BuildResultCallback = (result:WorkerResult) => void;
|
||||||
type BuildStatusCallback = (busy:boolean) => void;
|
type BuildStatusCallback = (busy:boolean) => void;
|
||||||
type LoadFilesCallback = (err:string, result?:Dependency[]) => void;
|
|
||||||
type IterateFilesCallback = (path:string, data:FileData) => void;
|
type IterateFilesCallback = (path:string, data:FileData) => void;
|
||||||
type GetRemoteCallback = (path:string, callback:(data:FileData) => void, datatype:'text'|'arraybuffer') => any;
|
type GetRemoteCallback = (path:string, callback:(data:FileData) => void, datatype:'text'|'arraybuffer') => any;
|
||||||
|
|
||||||
|
@ -122,18 +121,18 @@ export class CodeProject {
|
||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
loadFileDependencies(text:string, callback:LoadFilesCallback) {
|
loadFileDependencies(text:string) : Promise<Dependency[]> {
|
||||||
let includes = this.parseIncludeDependencies(text);
|
let includes = this.parseIncludeDependencies(text);
|
||||||
let linkfiles = this.parseLinkDependencies(text);
|
let linkfiles = this.parseLinkDependencies(text);
|
||||||
let allfiles = includes.concat(linkfiles);
|
let allfiles = includes.concat(linkfiles);
|
||||||
this.loadFiles(allfiles, (err:string, result?:Dependency[]) => {
|
return this.loadFiles(allfiles).then((result) => {
|
||||||
// set 'link' property on files that are link dependencies (must match filename)
|
// set 'link' property on files that are link dependencies (must match filename)
|
||||||
if (result) {
|
if (result) {
|
||||||
for (let dep of result) {
|
for (let dep of result) {
|
||||||
dep.link = linkfiles.indexOf(dep.path) >= 0;
|
dep.link = linkfiles.indexOf(dep.path) >= 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
callback(err, result);
|
return result;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,7 +174,8 @@ export class CodeProject {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: get local file as well as presets?
|
// TODO: get local file as well as presets?
|
||||||
loadFiles(paths:string[], callback:LoadFilesCallback) {
|
loadFiles(paths:string[]) : Promise<Dependency[]> {
|
||||||
|
return new Promise( (yes,no) => {
|
||||||
var result : Dependency[] = [];
|
var result : Dependency[] = [];
|
||||||
var addResult = (path, data) => {
|
var addResult = (path, data) => {
|
||||||
result.push({
|
result.push({
|
||||||
|
@ -189,7 +189,7 @@ export class CodeProject {
|
||||||
var path = paths.shift();
|
var path = paths.shift();
|
||||||
if (!path) {
|
if (!path) {
|
||||||
// finished loading all files; return result
|
// finished loading all files; return result
|
||||||
callback(null, result);
|
yes(result);
|
||||||
} else {
|
} else {
|
||||||
// look in cache
|
// look in cache
|
||||||
if (path in this.filedata) { // found in cache?
|
if (path in this.filedata) { // found in cache?
|
||||||
|
@ -201,7 +201,7 @@ export class CodeProject {
|
||||||
// look in store
|
// look in store
|
||||||
this.store.getItem(path, (err, value) => {
|
this.store.getItem(path, (err, value) => {
|
||||||
if (err) { // err fetching from store
|
if (err) { // err fetching from store
|
||||||
callback(err, result);
|
no(err);
|
||||||
} else if (value) { // found in store?
|
} else if (value) { // found in store?
|
||||||
this.filedata[path] = value; // do not update store, just cache
|
this.filedata[path] = value; // do not update store, just cache
|
||||||
addResult(path, value);
|
addResult(path, value);
|
||||||
|
@ -231,6 +231,7 @@ export class CodeProject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
loadNext(); // load first file
|
loadNext(); // load first file
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getFile(path:string):FileData {
|
getFile(path:string):FileData {
|
||||||
|
@ -262,10 +263,7 @@ export class CodeProject {
|
||||||
// otherwise, make it a string
|
// otherwise, make it a string
|
||||||
var text = typeof maindata === "string" ? maindata : '';
|
var text = typeof maindata === "string" ? maindata : '';
|
||||||
// TODO: load dependencies of non-main files
|
// TODO: load dependencies of non-main files
|
||||||
this.loadFileDependencies(text, (err, depends) => {
|
return this.loadFileDependencies(text).then( (depends) => {
|
||||||
if (err) {
|
|
||||||
console.log(err); // TODO?
|
|
||||||
}
|
|
||||||
if (!depends) depends = [];
|
if (!depends) depends = [];
|
||||||
var workermsg = this.buildWorkerMessage(depends);
|
var workermsg = this.buildWorkerMessage(depends);
|
||||||
this.worker.postMessage(workermsg);
|
this.worker.postMessage(workermsg);
|
||||||
|
|
|
@ -260,10 +260,8 @@ function loadProject(preset_id:string) {
|
||||||
current_project.mainPath = preset_id;
|
current_project.mainPath = preset_id;
|
||||||
setLastPreset(preset_id);
|
setLastPreset(preset_id);
|
||||||
// load files from storage or web URLs
|
// load files from storage or web URLs
|
||||||
current_project.loadFiles([preset_id], function(err, result) {
|
current_project.loadFiles([preset_id]).then((result) => {
|
||||||
if (err) {
|
if (result && result.length) {
|
||||||
alertError(err);
|
|
||||||
} else if (result && result.length) {
|
|
||||||
// file found; continue
|
// file found; continue
|
||||||
loadMainWindow(preset_id);
|
loadMainWindow(preset_id);
|
||||||
} else {
|
} else {
|
||||||
|
@ -1376,7 +1374,7 @@ function addFileToProject(type, ext, linefn) {
|
||||||
if (!checkEnteredFilename(filename)) return;
|
if (!checkEnteredFilename(filename)) return;
|
||||||
var path = filename;
|
var path = filename;
|
||||||
var newline = "\n" + linefn(filename) + "\n";
|
var newline = "\n" + linefn(filename) + "\n";
|
||||||
current_project.loadFiles([path], (err, result) => {
|
current_project.loadFiles([path]).then((result) => {
|
||||||
if (result && result.length) {
|
if (result && result.length) {
|
||||||
alertError(filename + " already exists; including anyway");
|
alertError(filename + " already exists; including anyway");
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -45,8 +45,7 @@ describe('Store', function() {
|
||||||
remote.push(path);
|
remote.push(path);
|
||||||
success();
|
success();
|
||||||
};
|
};
|
||||||
project.loadFiles(['local/test','test'], function(err, result) {
|
project.loadFiles(['local/test','test']).then((result) => {
|
||||||
assert.equal(null, err);
|
|
||||||
assert.deepEqual(["presets/_TEST/test"], remote);
|
assert.deepEqual(["presets/_TEST/test"], remote);
|
||||||
assert.deepEqual([ { path: 'local/test', filename: 'local/test', data: 'a', link:true } ], result);
|
assert.deepEqual([ { path: 'local/test', filename: 'local/test', data: 'a', link:true } ], result);
|
||||||
done();
|
done();
|
||||||
|
@ -81,13 +80,15 @@ describe('Store', function() {
|
||||||
project.callbackBuildStatus = function(b) { msgs.push(b) };
|
project.callbackBuildStatus = function(b) { msgs.push(b) };
|
||||||
project.updateFile('test.a', ' lda #0');
|
project.updateFile('test.a', ' lda #0');
|
||||||
project.setMainFile('test.a');
|
project.setMainFile('test.a');
|
||||||
project.updateFile('test.a', ' lda #1'); // don't send twice (yet)
|
setTimeout(() => {
|
||||||
assert.deepEqual(msgs, expectmsgs);
|
project.updateFile('test.a', ' lda #1'); // don't send twice (yet)
|
||||||
store.getItem('test.a', function(err, result) {
|
assert.deepEqual(msgs, expectmsgs);
|
||||||
assert.equal(null, err);
|
store.getItem('test.a', function(err, result) {
|
||||||
assert.equal(' lda #1', result);
|
assert.equal(null, err);
|
||||||
done();
|
assert.equal(' lda #1', result);
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
|
}, 1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user