GithubHelp home page GithubHelp logo

Comments (5)

linear avatar linear commented on June 3, 2024

VAX-1857 Why Electric can't load data when phone is offline?

from electric.

thunderfalco avatar thunderfalco commented on June 3, 2024
image when device is offline, electric object always undefined, and return null.

from electric.

kevin-dp avatar kevin-dp commented on June 3, 2024

Hi @thunderfalco,

Below i pasted the relevant snippet from the example app:

const ElectricProviderComponent = ({
  children,
}: {
  children: React.ReactNode
}) => {
  const [electric, setElectric] = useState<Electric>()

  useEffect(() => {
    let isMounted = true

    const init = async () => {
      const config = {
        debug: DEBUG_MODE,
        url: ELECTRIC_URL,
      }

      const conn = SQLite.openDatabaseSync('electric.db')
      const client = await electrify(conn, schema, config)
      await client.connect(authToken())

      if (!isMounted) {
        return
      }

      setElectric(client)
    }

    init()

    return () => {
      isMounted = false
    }
  }, [])

  if (electric === undefined) {
    return null
  }

  return <ElectricProvider db={electric}>{children}</ElectricProvider>
}

The call to useEffect passes an empty array of dependencies, hence, the callback is executed when the component initially renders. Inside that useEffect callback we call the asynchronous init function which electrifies a local SQLite database:

const conn = SQLite.openDatabaseSync('electric.db')
const client = await electrify(conn, schema, config)
await client.connect(authToken())

The last line from the snippet above awaits the call to client.connect, i.e. it waits until we are connected with the Electric sync server. Since you're offline, this call blocks until you come online. Hence, while offline the code does not reach the setElectric(client) call and thus electric is always undefined. To solve this you could decide to only call client.connect once you detect that you're online or you could decide not to await the call. Ofcourse, when you're not connected to the sync server, writes won't propagate to and from other clients.

from electric.

kevin-dp avatar kevin-dp commented on June 3, 2024

I'm closing this issue as I believe my previous comment explains the behavior you're experiencing and this is not a bug. Feel free to re-open with a comment in case there is any problem.

from electric.

thunderfalco avatar thunderfalco commented on June 3, 2024

Thank you

from electric.

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.