Giter VIP home page Giter VIP logo

Comments (12)

icorbrey avatar icorbrey commented on May 30, 2024 3

Quick update! I've been trying to debug this issue assuming that we're using the native IndexedDB interface, while lamenting that we're not using the idb package internally. However, I dug deeper and discovered that yes, we are in fact using the idb package! Once I figured that out I was able to properly implement this in about 15 minutes lol.

TL;DR: Don't use oncomplete when you're supposed to be using an async/await interface.

from fakeindexeddb.

icorbrey avatar icorbrey commented on May 30, 2024 1

from fakeindexeddb.

dumbmatter avatar dumbmatter commented on May 30, 2024

Can you provide a test case showing the problem?

from fakeindexeddb.

BrinsterAman avatar BrinsterAman commented on May 30, 2024

Yes sure,

tx.objectStore("test-key").index("contextLabel").openCursor().onsuccess = function (event) { var cursor = event.target.result; if (cursor) { console.log("From cursor:", cursor.value); cursor.continue(); } };
this is what I am using in my test case, as suggested by your npmjs page. But it is still not covering the lines.

from fakeindexeddb.

dumbmatter avatar dumbmatter commented on May 30, 2024

Thanks... but how about a complete script I can actually run? I ask because I am skeptical such a core feature is just completely broken. There are a ton of tests and a lot of people using fake-indexeddb. So it's likely some subtle issue specific to precisely how you're using it. Same for #59

from fakeindexeddb.

icorbrey avatar icorbrey commented on May 30, 2024

I'm getting a similar error using the default React testing library (i.e. react-scripts test on a CRA app). When we initialize a cursor using FDBFactory it does not seem to run cursors at all. See following:

// In tests before execution
indexedDB = new FDBFactory();

// In implementation
async function Migration(transaction) {

    const albumCursor = await transaction
        .objectStore('album')
        .index('albumId')
        .openCursor();

    console.log(albumCursor); // FDBCursorWithValue { ... }

    if (albumCursor) {
        albumCursor.onsuccess = function (event) {
            const cursor = event.target.result;
            if (cursor) {
                console.log('Got a value!'); // Never prints
            }
            else {
                console.log('Finishing up!'); // Never prints
            }
        }
    }
}

I would expect some sort of error to occur if we had set this up wrong but nothing happens.

from fakeindexeddb.

dumbmatter avatar dumbmatter commented on May 30, 2024

@icorbrey can you provide a self-contained example that I can run to see the problem? Without that it'd be a lot of guesswork for me to try to figure out what you're doing that is triggering a bug. There's already a lot of automated tests covering the basic functionality of cursors, so if there is a bug, I imagine it is some subtle interaction related to the specific way you're using it.

from fakeindexeddb.

icorbrey avatar icorbrey commented on May 30, 2024

Sure. Please note that idb() is simply getting a reference to our IndexedDB instance, you'll need to graft an existing IDB instance on. Additionally, album and photo are both tables in our schema.

describe('The fake-indexeddb cursor', () => {
    beforeEach(() => {
        indexedDB = new FDBFactory();
    });

    it('runs', async () => {
        const db = await idb();
        const cursor = db
            .transaction(['album', 'photo'], 'readwrite')
            .objectStore('album')
            .index('albumId')
            .openCursor();

        let didRun = false;
        cursor.onsuccess = function () {
            didRun = true;
            console.log('Ran!');
        };

        expect(didRun).toBeTruthy();
    });
});

from fakeindexeddb.

dumbmatter avatar dumbmatter commented on May 30, 2024

That test will always fail because onsuccess is executed asynchronously.

I added a unit test containing a working version of this code in commit c15b096

You can run it with yarn run build && yarn run test-mocha. It does indeed work. Closing this issue for now, I don't think there is a bug here, but feel free to provide more information if anyone is still having trouble.

from fakeindexeddb.

icorbrey avatar icorbrey commented on May 30, 2024

Of course it runs asynchronously, but the issue is that it doesn't run. In our tests, on success is never actually called

from fakeindexeddb.

dumbmatter avatar dumbmatter commented on May 30, 2024

Okay, but like I said above, I tried filling in the blanks in your code in commit c15b096 and it works fine. Maybe there is something else going on that you did not include in your partial code? Not much more I can say without a complete example.

from fakeindexeddb.

Charith47 avatar Charith47 commented on May 30, 2024

Quick update! I've been trying to debug this issue assuming that we're using the native IndexedDB interface, while lamenting that we're not using the idb package internally. However, I dug deeper and discovered that yes, we are in fact using the idb package! Once I figured that out I was able to properly implement this in about 15 minutes lol.

TL;DR: Don't use oncomplete when you're supposed to be using an async/await interface.

Same thing happened to me when using idb package. I used below method instead of onsuccess event and it worked.

		const db = await dbPromise(); // opens db
		const tx = db.transaction(storeName, 'readonly');
		const store = tx.objectStore(storeName);
		let cursor = await store.openCursor();
		
		while (cursor) {
                        // logic
			cursor = await cursor.continue();
		}

from fakeindexeddb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.