S
SolidJS•10mo ago
Dusty

Suspending or creating a resource from a Signal

I'm new to solid and trying to integrate it with Dexie. I looked at solid-dexie (https://github.com/faassen/solid-dexie) but I have a couple issues with it so I'm exploring taking it in my own direction. One problem, however, has me stumped. Dexie exports an observable that is easy to link into with from. My issue is that there is a (very short) period between when the signal is defined and when it actually has a value. After that, it's guaranteed to have a value, and the signal fires repeatedly thereafter whenever the db content updates. This means that I have to litter my code with <Show> tags to check if each Dexie query I make actually has content. If only there was a way I could wait for all my signals to have content before I render anything. Enter Suspense, or so I thought. My problem is that the Dexie queries aren't really a resource (they are signals that can fire multiple times, whereas a resource fires only on demand). I'm not clear if it's safe or sane to return a signal from a resource, but it seems to lack elegance. So I checked the createResource source code and decided that I really just need to increment and decrement the suspense context at just the right time. The problem with that idea is that a very intelligent person realized that it would be imprudent to allow yahoos like me to increment and decrement the suspense context. Therefore getSuspenseContext isn't exported. Soooo... I'm looking for advice. Is there an elegant way to tie an observable signal to suspense using createResource or otherwise?
18 Replies
Dusty
Dusty•10mo ago
One thing I thought might work was using my signal to trigger the resource by passing it as source to createResource, but the resource then returns undefined until the signal initially fiers, which brings me right back to needing <Show> all over.
bigmistqke
bigmistqke•10mo ago
Ye a resource is just a wrapper around an async signal, right. So you could return a promise that you resolve once it is defined and then you can count on Suspense to do its thing. It will still be typed undefined in the beginning so you might still have to ! at some places
Dusty
Dusty•10mo ago
Hah, I just seconds ago got that working, though it.... lacks elegance: https://gist.github.com/dusty-phillips/dde181bbbe887598e576abd32a429c62 well that's nice and readable. 😕 I'll get a gist, one sec.
bigmistqke
bigmistqke•10mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Dusty
Dusty•10mo ago
oooh
bigmistqke
bigmistqke•10mo ago
My favourite repl With createResource you can also map it directly to a store/deepSignal
Dusty
Dusty•10mo ago
not sure I understand.
bigmistqke
bigmistqke•10mo ago
https://docs.solidjs.com/references/api-reference/basic-reactivity/createResource#v150 See 3. A wait, that mb doesn't work with the observable What in ur gist u don't like? Tip for the playground: you can import any npm package and it will import them from cdn
Dusty
Dusty•10mo ago
Main thing I don't like is the "single-use" createEffect, and I'm not sure if trigging the promise off the store itself changing is sensible. In fact I don't need to trigger off the signal; I've simplified a bit. I'll see if I can get Dexie loading in the playground to demo.
bigmistqke
bigmistqke•10mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
bigmistqke
bigmistqke•10mo ago
i do wonder if the effect inside the promise gets cleaned up when it's resolved
Dusty
Dusty•10mo ago
Odd... I'm getting a console error "The requested module 'solid-js' does not provide an export named ResourceReturn.
bigmistqke
bigmistqke•10mo ago
A ye it's that new node rule where u have to do import { type ... Module resolution thingy
Dusty
Dusty•10mo ago
ah, yes. Yeah, here's a demo with it working with a form: https://playground.solidjs.com/anonymous/6ef86395-7a53-4473-a038-98ad1df25255 (it renders the fallback until something is added to the db, though)
bigmistqke
bigmistqke•10mo ago
I m outs right now but will look once I get back
Want results from more Discord servers?
Add your server