signaldb: svelteReactivityAdapter with persistence causes an error
Thank you again for releasing svelteReactivityAdapter! I have found a bug in it.
Reproduction
App.svelte:
<script lang="ts">
import { Collection } from '@signaldb/core'
import createLocalStorageAdapter from '@signaldb/localstorage'
import svelteReactivityAdapter from '@signaldb/svelte'
const Posts = new Collection({
persistence: createLocalStorageAdapter('posts'),
reactivity: svelteReactivityAdapter,
})
$effect(() => {
const cursor = Posts.find({})
console.log("app:fetch")
items = cursor.fetch()
return () => {
cursor.cleanup()
};
});
</script>
Error:
Uncaught TypeError: update is not a function
full stacktrace
Uncaught TypeError: update is not a function
notify index.svelte.js:25
set index17.mjs:14
_Collection2 index2.mjs:91
emit index12.mjs:89
emit index12.mjs:88
loadPersistentData index2.mjs:143
setTimeout handler*loadPersistentData index2.mjs:143
_Collection2 index2.mjs:207
promise callback*_Collection2 index2.mjs:203
App App.svelte:19
effect2 hmr.js:47
update_reaction runtime.js:430
update_effect runtime.js:596
create_effect effects.js:118
branch effects.js:360
wrapper hmr.js:38
update_reaction runtime.js:430
update_effect runtime.js:596
create_effect effects.js:118
block effects.js:352
wrapper hmr.js:28
unmount2 render.js:229
update_reaction runtime.js:430
update_effect runtime.js:596
create_effect effects.js:118
branch effects.js:360
unmount2 render.js:211
update_reaction runtime.js:430
update_effect runtime.js:596
create_effect effects.js:118
component_root effects.js:244
_mount render.js:208
mount render.js:73
<anonymous> main.ts:5
@signaldb_svelte.js:40:9
Investigation of the cause
I added a console.log for debugging and found that reactivity.create() was called in a context where it was not in scope. As a result, createSubscriber did not execute its callback to set the update function.
It seems that this happens during the initialization of the Collection.
Debugging Code:
+ let calls = 0
const svelteReactivityAdapter = createReactivityAdapter({
create() {
+ const id = `${(new Date()).getTime()}-${calls}`
+ calls++;
+ console.log(`create: ${id} tracking: ${$effect.tracking()}`);
let update;
- const subscribe = createSubscriber(u => update = u);
+ const subscribe = createSubscriber(u => {
+ update = u;
+ console.log("createSubscriber:callback", id, u)
+ });
return {
depend() {
+ console.log("depend", id)
subscribe();
},
notify() {
+ console.log("notify", id, update);
update();
},
};
},
isInScope: () => !!$effect.tracking(),
});
export default svelteReactivityAdapter;
Log outputs:
create: 1743206737019-0 tracking: false
create: 1743206737019-1 tracking: false
app:fetch
create: 1743206737021-2 tracking: true
depend 1743206737021-2
createSubscriber:callback 1743206737021-2 > function stop()
notify 1743206737021-2 > function stop()
notify 1743206737021-2 > function stop()
notify 1743206737021-2 > function stop()
notify 1743206737021-2 > function stop()
notify 1743206737021-2 > function stop()
notify 1743206737021-2 > function stop()
app:fetch
create: 1743206737022-3 tracking: true
depend 1743206737022-3
createSubscriber:callback 1743206737022-3 > function stop()
notify 1743206737019-0 undefined
Uncaught TypeError: update is not a function
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 17 (17 by maintainers)
Just to be thorough, I reviewed the Svelte and SignalDB code again, and it looks like the current implementation of
SvelteReactivityAdapteris appropriate.During initialization of a Collection, dependencies are created, but due to how Svelte’s
createSubscriberworks,dep.#updatecan only be obtained ifdep.depend()is called within the scope. On the other hand, in SignalDB,dep.depend()is called insidecollection.isPulling(), but whether that method is called is left up to the end user. So it’s possible fordep.#updateto beundefined.https://github.com/maxnowack/signaldb/blob/cb63b41c0986f15e555d5552c2dfae8c667fcfca/packages/base/core/src/Collection/index.ts#L245-L246
Okay, maybe we should start with setting up proper testing for the svelte adapter
Okay, got it. Thanks for the clarification!
I could not reproduce the issue inside a test but can confirm, that the issue is gone now 😃
I just released
@signaldb/svelte@1.1.1which should resolve theTypeError: cancel is not a function.@signalize I’ll have a look if I can extract the failing code into a unit test… The code is in a private repo 😦 and the error happens on page navigation. Lets see if I can reproduce it without sveltekit.
Fix from #1595 released in
@signaldb/svelte@1.1.0🎉Fix released in
@signaldb/core@v1.5.1🎉Thanks for analyzing this! It looks like I forgot to implement scope checking when using internal signals. Addressed in #1578 🙂
As a temporary workaround, the following fix can be applied. However, I am unsure what the ideal implementation should be when create is executed outside the scope, or whether it is even appropriate for it to be called outside the scope in the first place.