Giter Site home page Giter Site logo

Comments (10)

samwillis avatar samwillis commented on June 18, 2024 2

Hey @Neo-Ciber94

I've found the problem, it's due to React strict mode. Essentially PGlite is initiated twice for the same database, the first time it creates the indexeddb database to store the files, the second time it sees the database is there and so thinks it doesn't have to initiate it. However the first run hasn't completed and so the database doesn't exist.

I commented out the strict mode on this fork: https://stackblitz.com/edit/react-pglite-bug-rcynzw-cwupa2?file=src%2FApp.tsx

It's one of the occasions where you almost certainly don't want strict mode to init the postgres twice as it's such a cpu heavy operation, even for development. I would recommend explicitly only having a single instance of PGlite, example here (and below): https://stackblitz.com/edit/react-pglite-bug-rcynzw-bzsd5x?file=src%2FApp.tsx,src%2Fmain.tsx,src%2Findex.css

We should look at having a lock so that only one instance of the database can be opened at once with indexeddb and throw an error if it can't be acquired. I'm going to use this issue to track that.

import { PGlite } from '@electric-sql/pglite';
import { useState, useEffect } from 'react';

const DATA_DIR = 'idb://somedatabase2';

let client: PGlite;

export default function App() {
  const [data, setData] = useState<unknown>('PENDING');

  useEffect(() => {
    (async () => {
      client ??= new PGlite(DATA_DIR, { debug: 1 });
      const result = await client.exec('SELECT NOW()');
      setData(result);
    })();
  }, []);

  return (
    <div>
      <h1>PGLite</h1>
      <pre>{JSON.stringify(data, null, 2)}</pre>
    </div>
  );
}

from pglite.

thruflo avatar thruflo commented on June 18, 2024 2

Perhaps we could provide a PGlite.createOnce() function or similar, which would return the same instance no matter how many times it’s called.

from pglite.

samwillis avatar samwillis commented on June 18, 2024 1

Could you check if there is an indexeddb via the Chrome dev tools, if there is one delete it and refresh the app. It may be that the state of the database is broken, I'm suspicious that the initdb phase didn't complete when you first ran the code. Alternatively change the name of the database so that it creates a new one.

from pglite.

samwillis avatar samwillis commented on June 18, 2024

Thanks for the report, could you let me know what React framework and build tooling you are using?

the "use client" is making me think you may be using Next.js and RSC?

Does this happen in all web browsers or only a specific one?

from pglite.

Neo-Ciber94 avatar Neo-Ciber94 commented on June 18, 2024

Thanks for the report, could you let me know what React framework and build tooling you are using?

the "use client" is making me think you may be using Next.js and RSC?

Does this happen in all web browsers or only a specific one?

I test the code on NextJS, RemixJS and Vite+React, all 3 the same error.

I was using Chrome (Windows).

from pglite.

Neo-Ciber94 avatar Neo-Ciber94 commented on June 18, 2024

Removing or changing the name of the Indexeddb is not working for me either, is also happening in Firefox:

image

from pglite.

Neo-Ciber94 avatar Neo-Ciber94 commented on June 18, 2024

Not sure where the errors is comming from, is also happening on stackblitz:

https://stackblitz.com/edit/react-pglite-bug-rcynzw?file=src%2FApp.tsx

Be aware that this halts my browser.

image

from pglite.

Neo-Ciber94 avatar Neo-Ciber94 commented on June 18, 2024

@samwillis You are totally right, looks like useEffect took other victim.

Ensuring the instance is created only once solve the issue for me.

I really appreciate your help, Thank you!

from pglite.

samwillis avatar samwillis commented on June 18, 2024

@thruflo i like that. Ideally we should use a weak map to store the references to previously open databases so they can be cleanly garbage collected without having to explicitly close it to remove any reference.

from pglite.

berzi avatar berzi commented on June 18, 2024

If a createOnce() is added, would there be any use for the current syntax that allows multiple instances to be created?

Also is there anything to consider to support workers? Maybe we need a createOnceWithWorkers() too? Even though I don't really know why you would ever need an instance on the main thread in the first place—unless there's a good reason it sounds like an antipattern to even allow it.

from pglite.

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.