signaldb icon indicating copy to clipboard operation
signaldb copied to clipboard

solid-js reactivity not working

Open KarlitosD opened this issue 1 year ago β€’ 7 comments

I was testing signaldb in my solid-js project, but I noticed that adding an element to the collection triggered the effect.

import { Collection } from 'signaldb';
import solidReactivityAdapter from 'signaldb-plugin-solid'
import { createEffect } from 'solid-js';


const posts = new Collection({
  reactivity: solidReactivityAdapter,
});

createEffect(() => {
  console.log(posts.find({ author: 'John' }).count());
});

// ---------------------- INSERTS ---------------------------

(async () => {
  await posts.insert({ id: crypto.randomUUID(), author: 'John' });
  await new Promise((r) => setTimeout(r, 1000));
  await posts.insert({ id: crypto.randomUUID(), author: 'Kevin' });
  await new Promise((r) => setTimeout(r, 1000));
  await posts.insert({ id: crypto.randomUUID(), author: 'David' });
  console.log('Finish inserts');
})();

Testing the same example but with vue and @maverick-js/signals works as expected.

Then check in the documentation to make a custom adapter and add some console.log. It turns out that it never calls the β€œcreate” function.

import { Collection } from 'signaldb';
//import solidReactivityAdapter from 'signaldb-plugin-solid'
import { createEffect } from 'solid-js';

const solidReactivityAdapter = createReactivityAdapter({
  create: () => {
    console.log('Create');
    const [depend, rerun] = createSignal(0);
    return {
      depend: () => {
        console.log('Depend');
        depend();
      },
      notify: () => {
        console.log('Notify');
        rerun(depend() + 1);
      },
    };
  },
  isInScope: undefined,
  onDispose: (callback) => {
    onCleanup(callback);
  },
});

const posts = new Collection({
  reactivity: solidReactivityAdapter,
});

createEffect(() => {
  console.log(posts.find({ author: 'John' }).count());
});

// ---------------------- INSERTS ---------------------------

(async () => {
  await posts.insert({ id: crypto.randomUUID(), author: 'John' });
  await new Promise((r) => setTimeout(r, 1000));
  await posts.insert({ id: crypto.randomUUID(), author: 'Kevin' });
  await new Promise((r) => setTimeout(r, 1000));
  await posts.insert({ id: crypto.randomUUID(), author: 'David' });
  console.log('Finish inserts');
})();

I don't know what could be happening, I also leave a link to stackblitz you can try to replicate my experience.

KarlitosD avatar May 05 '24 18:05 KarlitosD

Hi @KarlitosD, it looks like you're in a server environment or at least the provided stackblitz is running a nodejs environment. It seems that solid-js doesn't run effects on server side. To get around this, you can import createEffect directly from the browser bundle solid-js/dist/solid. I'm also doing this to run the signaldb tests for the solid-js integration (see https://github.com/maxnowack/signaldb/blob/main/packages/plugin-solid/tests/reactivity.spec.ts#L7).

You just have to adjust your import from

import { createEffect } from 'solid-js';

to

import { createEffect } from 'solid-js/dist/solid';

maxnowack avatar May 06 '24 14:05 maxnowack

But it also seem that there is an issue with imports inside of signaldb-plugin-solid. I'll look into that. The solution mentioned above should work if you implement the reactivity adapter by yourself, as you've done it in the stackblitz.

maxnowack avatar May 06 '24 14:05 maxnowack

The issue with the imports is resolved in [email protected]

maxnowack avatar May 06 '24 14:05 maxnowack

Thanks for the help πŸ˜€ At first I was testing on my vite project, but seeing that the adapter was not working I decided to take it to a stackblitz, so I could install vue and @maverick-js/signals and see if that was the expected behavior in the other libraries, without messing up my project dependencies.

I didn't realize that solid on the server was not running effects.

I will try to implement it again in my project with the latest version, again thanks for your quick response.

KarlitosD avatar May 06 '24 17:05 KarlitosD

You're welcome! Thanks for pointing me in the direction to find the imports issue in signaldb-plugin-solid πŸ™‚ Feel free to ask if you need assistance!

maxnowack avatar May 07 '24 02:05 maxnowack

Hello me again πŸ˜…, related to this it keeps happening to me in a vite project, here I leave the stackblitz with the updated dependencies.

Sorry for the inconvenience again πŸ˜“

KarlitosD avatar May 09 '24 03:05 KarlitosD

It seems that there is an error in stackblitz while setting up dependencies. I'm not sure if this is related to signaldb

maxnowack avatar May 10 '24 07:05 maxnowack

@KarlitosD I just downloaded the project locally to try it out. It seems to work properly without stackblitz. Can you confirm this?

maxnowack avatar May 16 '24 06:05 maxnowack

closing as no response received. Feel free to reopen if the issue persists

maxnowack avatar May 27 '24 09:05 maxnowack

Hi @KarlitosD, it looks like you're in a server environment or at least the provided stackblitz is running a nodejs environment. It seems that solid-js doesn't run effects on server side. To get around this, you can import createEffect directly from the browser bundle solid-js/dist/solid. I'm also doing this to run the signaldb tests for the solid-js integration (see https://github.com/maxnowack/signaldb/blob/main/packages/plugin-solid/tests/reactivity.spec.ts#L7).

You just have to adjust your import from

import { createEffect } from 'solid-js';

to

import { createEffect } from 'solid-js/dist/solid';

I had the opposite issue. Reactivity wasn't working and using the normal solid import fixed it.

obedm503 avatar Jul 10 '24 00:07 obedm503

The issue is finally resolved in [email protected] πŸŽ‰

maxnowack avatar Sep 11 '24 10:09 maxnowack