10000 forum users celebration - special event

Google it?

Nope nvm got a prob again.

It’s not on google >:(

SO thinks that people are question banned will never change, so they usually don’t do much.

It’s sad, because so many places on the web are like an aristocratic government - they give love to the popular and clean and disregard the rest.

So I asked a few bad questions, so what?

Most of the others just simply have 0 votes.

Oh well, that’s what I get for expecting the internet to be a helpful place :confused: (it is, but sometimes it isn’t)

what’s your question?

Ur the ā€œJS queenā€ā€¦ help me out here!

const { indexedDB } = globalThis;
const awaitEvent = (obj, handlerName, rejOnError = true, timeout = Infinity, callback = () => {}) => {
    if("object" !== typeof obj){
        throw new TypeError(`Argument provided for object to bind the listener to is not of type "object".`);
    }
    if("string" !== typeof handlerName){
        throw new TypeError(`Argument provided for object's event listener name is not of type "string".`);
    }
    if("function" !== typeof obj.addEventListener){
        throw new TypeError(`Object does not have an "addEventListener" function.`);
    }
    if("function" !== typeof callback){
        throw new TypeError(`Callback provided is not a function.`);
    }
    /*Status codes:
        0: ok
        1: timed out
    */
    return new Promise((res, rej) => {
       obj.addEventListener(handlerName, evt => {
           callback(evt);
            res({
                "ok": true,
                "status": 0,
                "statusText": ""
            });
       });
       if(timeout !== Infinity && timeout > 0){
           globalThis.setTimeout(timeout, () => {
               if(rejOnError){
                   rej({
                       "ok": false,
                       "status": 1,
                       "statusText": ""
                   });
               }
               else{
                   res({
                       "ok": false,
                       "status": 1,
                       "statusText": ""
                   });
               }
           });
       }
    });
};

const transactions = class {
    constructor() {
        this.db = null;
    }
    connect(name, version, upgradeNeededCallback = () => {}) {
        if(this.db !== null){
            throw new Error(`Database is already open. Close it using "disconnect()".`);
        }
        if("string" !== typeof name){
            throw new TypeError(`Argument provided for the database name is not of type "string".`);
        }
        if("number" !== typeof version){
            throw new TypeError(`Argument provided for the version is not of type "number".`);
        }
        if(version < 1){
            throw new TypeError(`Database version cannot be less than one. Expected a greater-than 0 value, got a less-than 0 value of ${version} instead.`);
        }
        if("function" !== typeof upgradeNeededCallback){
            throw new TypeError(`Argument provided for the upgrade needed callback is not a function.`);
        }
        return new Promise(async (res, rej) => {
            const dEntries = await indexedDB.databases();
            if(dEntries.filter(dEntry => dEntry.name === name).length < 1){
                rej(new Error(`Database is not initialized. Use "create()" instead.`));
                return;
            }
            const req = indexedDB.open(name, version);
            req.addEventListener("success", () => {
                this.db = req.result;
                res(this.db);
            });
            req.addEventListener("upgradeneeded", event => {
                this.db = req.result;
                upgradeNeededCallback(event, this.db);
                res(this.db);
            });
            req.addEventListener("error", err => rej(err));
        });
    }
    create(name, createdCallback = () => {}) {
        if(this.db !== null){
            throw new Error(`Database is already open. Close it using "disconnect()".`);
        }
        if("string" !== typeof name){
            throw new TypeError(`Argument provided for database name is not of type "string".`);
        }
        if("function" !== typeof createdCallback){
            throw new TypeError(`Argument provided for the initial creation callback is not a function.`);
        }
        return new Promise((res, rej) => {
            const req = indexedDB.open(name, 1);
            req.addEventListener("success", () => {
                rej(new Error(`Database is already created.`));
            });
            req.addEventListener("upgradeneeded", event => {
                if(event.oldVersion < 1){
                    this.db = req.result;
                    createdCallback(event, this.db);
                    res(this.db);
                }
                else{
                    rej(new Error(`Database is already created.`));
                }
            });
            req.addEventListener("error", err => rej(err));
        });
    }
    createObjectStore(name, options = {}){
        if(this.db === null){
            throw new Error(`Database is already open. Close it using "disconnect()".`);
        }
        if("string" !== typeof name){
            throw new TypeError(`Argument provided as the object store name is not of type "string".`);
        }
        if("object" !== typeof options){
            throw new TypeError(`Argument provided as the object store options is not of type "object".`);
        }
        return new Promise(async (res, rej) => {
            const dEntries = await indexedDB.databases();
            if(dEntries.filter(dEntry => dEntry.name === this.db.name).length < 1){
                rej(new Error(`Database is not initialized. Use "create()" instead.`));
                return;
            }
            //TODO & FIXME: Infinite update wait time (I'm probably upgrading it wrong)!!!
            const req = indexedDB.open(this.db.name, this.db.version + 1);
            req.addEventListener("upgradeneeded", event => {
                this.db = req.result;
                this.db.createObjectStore(name, options);
                console.log(this.db);
            });
            req.addEventListener("success", () => {
                this.db = req.result;
                this.db.createObjectStore(name, options);
                console.log(req);
                res(this.db);
            });
            console.log(req);
            req.addEventListener("error", err => rej(err));
        });
    }
    disconnect() {
        this.db = null;
    }
};

export default transactions;

Under the "TODO & FIXME", I get an infinite waiting time.

Keeping the same version gives:

transactions.js:141 Uncaught DOMException: Failed to execute ā€˜createObjectStore’ on ā€˜IDBDatabase’: The database is not running a version change transaction.
at IDBOpenDBRequest. (http://localhost:8888/transactions.js:141:25)

And then doing the current, I have an infinite pending time >:(

node.js?

Uhhhh, no.

IndexedDB is for client.

Pure vanilla JS (which is why I don’t answer questions about redux and such).

I typically hate libraries/frameworks because I want to know how things work, not have people do them for me.

oh i meant is it on browser side or naw?
because i cannot provide solution without trying it :slight_smile:

Yeah.

I think it is blocked (the version upgrade), but I have to see for myself.

I found this but let me try to debug.

Yeah I know.

Oops, my code will give err.

Hang on, let me fix (was trying something and I changed it back now).

There, check the edit.

Ok I found it, I forgot to close the DB XD!

That’s why i hate programming at this point d7u9nw1-be41509b-739a-47e3-aca7-140022c7ce9c

Yeah, XD.

I’m working it out, I think my method might not be possible (thank goodness I’m question banned - MAN I thought I’d never say that)

I’m too lazy to read the code again, is this a library for in-browser database?

Yeah. Got it!

Code:

const { indexedDB } = globalThis;
const awaitEvent = (obj, handlerName, rejOnError = true, timeout = Infinity, callback = () => {}) => {
    if("object" !== typeof obj){
        throw new TypeError(`Argument provided for object to bind the listener to is not of type "object".`);
    }
    if("string" !== typeof handlerName){
        throw new TypeError(`Argument provided for object's event listener name is not of type "string".`);
    }
    if("function" !== typeof obj.addEventListener){
        throw new TypeError(`Object does not have an "addEventListener" function.`);
    }
    if("function" !== typeof callback){
        throw new TypeError(`Callback provided is not a function.`);
    }
    /*Status codes:
        0: ok
        1: timed out
    */
    return new Promise((res, rej) => {
       obj.addEventListener(handlerName, evt => {
           callback(evt);
            res({
                "ok": true,
                "status": 0,
                "statusText": ""
            });
       });
       if(timeout !== Infinity && timeout > 0){
           globalThis.setTimeout(timeout, () => {
               if(rejOnError){
                   rej({
                       "ok": false,
                       "status": 1,
                       "statusText": ""
                   });
               }
               else{
                   res({
                       "ok": false,
                       "status": 1,
                       "statusText": ""
                   });
               }
           });
       }
    });
};

const transactions = class {
    constructor() {
        this.db = null;
    }
    connect(name, version, upgradeNeededCallback = () => {}) {
        if(this.db !== null){
            throw new Error(`Database is already open. Close it using "disconnect()".`);
        }
        if("string" !== typeof name){
            throw new TypeError(`Argument provided for the database name is not of type "string".`);
        }
        if("number" !== typeof version){
            throw new TypeError(`Argument provided for the version is not of type "number".`);
        }
        if(version < 1){
            throw new TypeError(`Database version cannot be less than one. Expected a greater-than 0 value, got a less-than 0 value of ${version} instead.`);
        }
        if("function" !== typeof upgradeNeededCallback){
            throw new TypeError(`Argument provided for the upgrade needed callback is not a function.`);
        }
        return new Promise(async (res, rej) => {
            const dEntries = await indexedDB.databases();
            if(dEntries.filter(dEntry => dEntry.name === name).length < 1){
                rej(new Error(`Database is not initialized. Use "create()" instead.`));
                return;
            }
            const req = indexedDB.open(name, version);
            req.addEventListener("success", () => {
                this.db = req.result;
                res(this.db);
            });
            req.addEventListener("upgradeneeded", event => {
                this.db = req.result;
                upgradeNeededCallback(event, this.db);
                res(this.db);
            });
            req.addEventListener("error", err => rej(err));
        });
    }
    create(name, createdCallback = () => {}) {
        if(this.db !== null){
            throw new Error(`Database is already open. Close it using "disconnect()".`);
        }
        if("string" !== typeof name){
            throw new TypeError(`Argument provided for database name is not of type "string".`);
        }
        if("function" !== typeof createdCallback){
            throw new TypeError(`Argument provided for the initial creation callback is not a function.`);
        }
        return new Promise((res, rej) => {
            const req = indexedDB.open(name, 1);
            req.addEventListener("success", () => {
                rej(new Error(`Database is already created.`));
            });
            req.addEventListener("upgradeneeded", event => {
                if(event.oldVersion < 1){
                    this.db = req.result;
                    createdCallback(event, this.db);
                    res(this.db);
                }
                else{
                    rej(new Error(`Database is already created.`));
                }
            });
            req.addEventListener("error", err => rej(err));
        });
    }
    createObjectStore(name, options = {}){
        if(this.db === null){
            throw new Error(`Database is already open. Close it using "disconnect()".`);
        }
        if("string" !== typeof name){
            throw new TypeError(`Argument provided as the object store name is not of type "string".`);
        }
        if("object" !== typeof options){
            throw new TypeError(`Argument provided as the object store options is not of type "object".`);
        }
        return new Promise(async (res, rej) => {
            this.db.close();
            const dEntries = await indexedDB.databases();
            if(dEntries.filter(dEntry => dEntry.name === this.db.name).length < 1){
                rej(new Error(`Database is not initialized. Use "create()" instead.`));
                return;
            }
            //TODO & FIXME: Infinite update wait time (I'm probably upgrading it wrong)!!!
            const req = indexedDB.open(this.db.name, this.db.version + 1);
            req.addEventListener("upgradeneeded", event => {
                this.db = req.result;
                this.db.createObjectStore(name, options);
                console.log(this.db);
            });
            req.addEventListener("success", () => {
                this.db = req.result;
                this.db.addEventListener("versionchange", () => {
                    this.db.createObjectStore(name, options);
                    console.log(req);
                    res(this.db);
                });
            });
            console.log(req);
            req.addEventListener("error", err => rej(err));
            req.addEventListener("blocked", err => rej(err));
        });
    }
    disconnect() {
        this.db = null;
    }
};

export default transactions;

I wish we had executable code blocks like SO does :confused:

I’m making my own transaction handler because it’s hard to do indexedDB so I’ll use this for handling DB stuff (I’m using functions for their real purpose: to reduce code redundancies).

I found out the prob from here:

Reading does help! Who knew…

1 Like

OMG I found the best song, but it’s not out yet (I have Monstercat gold so I can listen early). I’ll show u when you can view it, it’s amazing.

Rich boy >:(

Its legit $60 a year (now $80 but whatever).

I’m actually poor.

Ok, I have $700 saved for a computer, but like it’s a dead effort at this point since parents are saying no.

Oh, and btw…

Are you celebrating Easter?

We are (kinda).

I… legit… slept… through… church!

And not ā€œsleep inā€, I actually was asleep.

They were talking about (according to my Mom) how God can bring everything back to life.

Duh, he’s God!!!

But I think the more amazing thing is that he chooses to bring things back to life :slight_smile:

As a muslim, not really.

1 Like

Ah…

At least we believe in the same God (with separate qualities, but whatever).

I was going to ask if u were Muslim (since a lot of people in your area probably are, but I don’t want to discriminate or be biased or stereotypical)…

Muslim/Jewish/Christians believe in the same God, practically - more evidence that he exists :slight_smile:

1 Like