Skip to content

Potential improvements for Vue's reactivity system #277

@sandros94

Description

@sandros94

Long story short: the current main classes for PGlite use a number of private methods, but since Vue's reactivity is based on proxying this effectively breaks its usability (official comment).

This becomes quite important when working with composables instead of components.

An example of an ideal DX composable would be:

import { computed, ref, onMounted, onBeforeUnmount } from 'vue'
import type { PGliteOptions } from '@electric-sql/pglite'
import { PGlite } from '@electric-sql/pglite'

export function usePGlite(options?: PGliteOptions) {
  // initialize an empty ref, so that Vue can start tracking it
  const pg = ref<PGlite>()

  // onMounted required in SSR (for Nuxt you can use onNuxtReady)
  onMounted(async () => {
    pg.value = await PGlite.create(options)
  })

  // safely closing the connection
  onBeforeUnmount(() => {
    if (!isClosed.value || pg.value === undefined) return
    pg.value.close()
  })

  const isReady = computed(() => !!pg.value?.ready)
  const isClosed = computed(() => !!pg.value?.closed)

  return {
    pg,
    isReady,
    isClosed,
  }
}

Same applies for workers and in a Nuxt context.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions