🐛 Silence SignalDB warnings for reads outside reactive scopes

Every read outside a reactive scope passed { reactive: false } and made
Cursor.depend() log a warning. Such reads now use a silent reactivity
adapter, and all, get, pendingCount and save release the cursor again
with cleanup(), so nothing is registered and nothing leaks. Reads inside
$derived or $effect stay reactive as before.

Vitest now resolves the browser export of Svelte. Without it
svelte/reactivity is the server stub, createSubscriber never starts and
no effect re-runs after a write in tests.
This commit is contained in:
2026-12-01 17:02:53 +01:00
parent 73359d3f39
commit bd2ec79a69
4 changed files with 137 additions and 13 deletions
+42 -11
View File
@@ -1,4 +1,12 @@
import { Collection, DefaultDataAdapter } from '@signaldb/core';
import {
Collection,
DefaultDataAdapter,
createReactivityAdapter,
type BaseItem,
type Cursor,
type ReactivityAdapter,
type Selector
} from '@signaldb/core';
import { SyncManager } from '@signaldb/sync';
import svelteReactivityAdapter from '@signaldb/svelte';
import prepareIndexedDB from '@signaldb/indexeddb';
@@ -109,10 +117,28 @@ export const status = $state({
});
const reactivity = svelteReactivityAdapter;
/** Reports a reactive scope where there is none, so a cursor reads silently and wakes nobody. */
const silent: ReactivityAdapter = createReactivityAdapter({
create: () => ({ depend() {}, notify() {} }),
isInScope: () => true
});
/** Reactive when called inside a reactive scope, a plain read otherwise. */
function readOptions(): { reactive: false } | undefined {
return reactivity.isInScope?.() ? undefined : { reactive: false };
function inScope(): boolean {
return reactivity.isInScope ? reactivity.isInScope() : true;
}
/** Reads outside a reactive scope: registers no dependency and releases the cursor's observer. */
function readOnce<T extends BaseItem, R>(
c: Collection<T, string>,
selector: Selector<T> | undefined,
read: (cursor: Cursor<T, T>) => R
): R {
const cursor = c.find(selector, { reactive: silent });
try {
return read(cursor);
} finally {
cursor.cleanup();
}
}
const cursorKey = (uid: string, name: string) => `wotra-cursor:${uid}:${name}`;
@@ -134,7 +160,8 @@ function onSynced(name: string) {
class WotraSync extends SyncManager<Record<string, unknown>, AnyDoc, string> {
pending(): number {
return this.changes.find({}, readOptions()).count();
const changes = this.changes;
return inScope() ? changes.find({}).count() : readOnce(changes, {}, (cursor) => cursor.count());
}
/** Lets writes in flight finish, since the sync manager rejects them after dispose(). */
async settle(): Promise<void> {
@@ -358,14 +385,18 @@ export function pendingCount(): number {
}
export function all<K extends CollectionName>(name: K, includeDeleted = false): Doc<PayloadMap[K]>[] {
if (!cols) return [];
const c = cols[name];
return (includeDeleted ? c.find({}, readOptions()) : c.find({ deleted: false }, readOptions())).fetch() as unknown as Doc<PayloadMap[K]>[];
const c = cols?.[name];
if (!c) return [];
const selector = includeDeleted ? {} : { deleted: false };
const rows = inScope() ? c.find(selector).fetch() : readOnce(c, selector, (cursor) => cursor.fetch());
return rows as unknown as Doc<PayloadMap[K]>[];
}
export function get<K extends CollectionName>(name: K, id: string): Doc<PayloadMap[K]> | undefined {
if (!cols) return undefined;
return cols[name].findOne({ id }, readOptions()) as Doc<PayloadMap[K]> | undefined;
const c = cols?.[name];
if (!c) return undefined;
const row = inScope() ? c.findOne({ id }) : readOnce(c, { id }, (cursor) => cursor.fetch()[0]);
return row as Doc<PayloadMap[K]> | undefined;
}
export async function save<K extends CollectionName>(name: K, id: string | null, data: PayloadMap[K], deleted = false): Promise<string> {
@@ -373,7 +404,7 @@ export async function save<K extends CollectionName>(name: K, id: string | null,
const c = cols[name];
const rid = id ?? newId();
const now = Date.now();
const prev = c.findOne({ id: rid }, { reactive: false });
const prev = readOnce(c, { id: rid }, (cursor) => cursor.fetch()[0]);
const cua = Math.max(now, (prev?.client_updated_at ?? 0) + 1);
const rec: AnyDoc = {
...pick(name, data),