codefrau / SqueakJS

A Squeak Smalltalk VM in Javascript
https://squeak.js.org
MIT License
365 stars 75 forks source link

Templates do not work when IndexedDB is unavailable #48

Closed fniephaus closed 4 years ago

fniephaus commented 8 years ago

To reproduce, add delete window.indexedDB; after this line and then run Etoys.

You should see something like this in the Console: image

fniephaus commented 8 years ago

After trying to understand what is happening here, I think I have identified the problem. If indexedDB is unavailable, a fake DB is used and it does not call onerror nor onsuccess: https://github.com/bertfreudenberg/SqueakJS/blob/a4d76c0dfecee23f2e499f957a0dd8e97bb88bff/vm.js#L474-L489

This naive fix seems to get it working:

this.dbTransaction("readonly", "get " + filepath, function(fileStore) {
    var callFetchTemplateFile = function() {
        Squeak.fetchTemplateFile(path.fullname,
            function gotTemplate(template) {thenDo(template)},
            function noTemplate() {
                // if no indexedDB then we have checked fake db already
                if (typeof indexedDB == "undefined") return errorDo("file not found: " + path.fullname);
                // fall back on fake db, may be file is there
                var fakeReq = Squeak.dbFake().get(path.fullname);
                fakeReq.onerror = function(e) { errorDo("file not found: " + path.fullname) };
                fakeReq.onsuccess = function(e) { thenDo(this.result); }
            });
    };
    if (typeof indexedDB == "undefined") {
        callFetchTemplateFile();
        return;
    }
    var getReq = fileStore.get(path.fullname);
    getReq.onerror = function(e) { errorDo(e.target.error.name) };
    getReq.onsuccess = function(e) {
        if (this.result !== undefined) return thenDo(this.result);
        // might be a template
        callFetchTemplateFile();
    };
});