Skip to content

Svelte 5 : important recent regression with DexieJs LiveQuery that stopped working #12341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tobiasBora opened this issue Jul 8, 2024 · 8 comments · Fixed by #12486
Closed
Labels
Milestone

Comments

@tobiasBora
Copy link

tobiasBora commented Jul 8, 2024

Describe the bug

I recently tried to upgrade my svelte 5 installation (it used to work fine with a previous version of svelte 5), and now I'm basically unable to use Dexie.js as liveQuery just outputs an undefined value if I refresh the page, until I write to the database. This happens even on trivial repositories (I just copied the quickstart of Dexie.js). Since an image is worth a thousand words:

Peek 08-07-2024 09-41

Reproduction

Clone https://github.com/tobiasBora/debugLiveQuery, start with npm run dev -- --open, add a new friend, refresh the page: the friend should be gone. Then add another friend: both the new friend and the old friend should appear.

Logs

No error or non-trivial log of any kind is visible.

System Info

System:
    OS: Linux 6.1 NixOS 24.05 (Uakari) 24.05 (Uakari)
    CPU: (8) x64 Intel(R) Core(TM) i5-8365U CPU @ 1.60GHz
    Memory: 929.63 MB / 15.45 GB
    Container: Yes
    Shell: 5.9 - /home/leo/.nix-profile/bin/zsh
  Binaries:
    Node: 21.6.2 - /nix/store/va3sggfgfb709lm31bzvpjfyapjdy435-nodejs-21.6.2/bin/node
    npm: 10.2.4 - /nix/store/va3sggfgfb709lm31bzvpjfyapjdy435-nodejs-21.6.2/bin/npm
  Browsers:
    Chromium: 122.0.6261.94
  npmPackages:
    svelte: ^5.0.0-next.1 => 5.0.0-next.175

[email protected] /tmp/debugLiveQuery/my-app
├── @fontsource/[email protected]
├── @neoconfetti/[email protected]
├── @sveltejs/[email protected]
├── @sveltejs/[email protected]
├── @sveltejs/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

Severity

blocking all usage of svelte

@tobiasBora
Copy link
Author

I tried to check, and the issue appears between next.173 (working) and next.174 (not working), and persists at least until the last version next.175.

@trueadm
Copy link
Contributor

trueadm commented Jul 8, 2024

Maybe related to #12330.

@trueadm trueadm added the bug label Jul 8, 2024
@trueadm trueadm added this to the 5.0 milestone Jul 8, 2024
@tobiasBora
Copy link
Author

Hum… So next.173 is actually working for this specific example, so it seems something broke from 173 to 174. To be honest, in my actual app I also couldn't use 173 because of a mysterious error:

The `this={...}` property of a `<svelte:component>` must be a Svelte component, if defined

so I finally use 172 that seems to work fine for now.

@trueadm
Copy link
Contributor

trueadm commented Jul 8, 2024

Hum… So next.173 is actually working for this specific example, so it seems something broke from 173 to 174. To be honest, in my actual app I also couldn't use 173 because of a mysterious error:

The `this={...}` property of a `<svelte:component>` must be a Svelte component, if defined

so I finally use 172 that seems to work fine for now.

Do you use stores? I'm looking through the changes between 173-174 [email protected]@5.0.0-next.174

@tobiasBora
Copy link
Author

tobiasBora commented Jul 8, 2024

In my whole code I do use stores when I got the must be a Svelte component yes, but I have not myself created a store in the MWE that fails for the code that I linked above (but maybe liveQuery used it internally?). Actually you can maybe just bisect the commits with the above MWE I posted? It might be the most efficient here.

@trueadm
Copy link
Contributor

trueadm commented Jul 9, 2024

Can you try using the latest version please?

@tobiasBora
Copy link
Author

tobiasBora commented Jul 10, 2024

Is it already published? I can only upgrade to 175 to test on my MWE https://github.com/tobiasBora/debugLiveQuery

@legowhales
Copy link

Maybe it has to do with the fact that we need to call subscription.unsubscribe() and not subscription() to unsubscribe the liveQuery observable:

From the Dexie's docs:

import { liveQuery } from "dexie";
import { db } from './db';

const friendsObservable = liveQuery (
  () => db.friends
    .where('age')
    .between(50, 75)
    .toArray()
);

// Subscribe
const subscription = friendsObservable.subscribe({
  next: result => console.log("Got result:", JSON.stringify(result)),
  error: error => console.error(error)
});

// Unsubscribe
subscription.unsubscribe();

You could create a wrapper around liveQuery that creates a readable store (and should work for both Svelte 4 & 5):

import { liveQuery } from 'dexie';
import { readable, type Readable } from 'svelte/store';

export function useQuery<T>(querier: () => T | Promise<T>): Readable<T> {
  return readable<T>(undefined, (set) => {
    return liveQuery(querier).subscribe(set).unsubscribe;
  })
}

I don't know why it worked before without doing this though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants