🐛 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:
@@ -1,5 +1,8 @@
|
||||
import { Collection } from '@signaldb/core';
|
||||
import { tick } from 'svelte';
|
||||
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { all, closeDb, get, openDb, pendingCount, remove, save, status, syncNow, toRecord, update } from './db.svelte';
|
||||
import { watch } from './testing.svelte';
|
||||
|
||||
const mocks = vi.hoisted(() => {
|
||||
const server = new Map<string, Map<string, Record<string, unknown>>>();
|
||||
@@ -44,6 +47,7 @@ vi.mock('$lib/pb', () => ({
|
||||
|
||||
let n = 0;
|
||||
const settingsRow = { effective_from: '2026-01-01', hours_per_week: 40, workdays_mask: 31, timezone: 'UTC' };
|
||||
const settled = () => new Promise<void>((resolve) => setTimeout(resolve, 0));
|
||||
|
||||
beforeEach(async () => {
|
||||
status.online = false;
|
||||
@@ -95,6 +99,61 @@ describe('local collections', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('read scopes', () => {
|
||||
it('event style reads neither warn nor keep an observer', async () => {
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
let created = 0;
|
||||
let disposed = 0;
|
||||
const collection = Collection.getCollections().find((c) => c.name === 'balance_adjustments');
|
||||
if (!collection) throw new Error('collection missing');
|
||||
collection.on('observer.created', () => (created += 1));
|
||||
collection.on('observer.disposed', () => (disposed += 1));
|
||||
try {
|
||||
const id = await save('balance_adjustments', null, { delta_ms: 1000, note: '', effective_at: 1 });
|
||||
await update('balance_adjustments', id, { delta_ms: 2000 });
|
||||
expect(all('balance_adjustments')[0]?.delta_ms).toBe(2000);
|
||||
expect(get('balance_adjustments', id)?.delta_ms).toBe(2000);
|
||||
expect(pendingCount()).toBe(2);
|
||||
await remove('balance_adjustments', id);
|
||||
expect(all('balance_adjustments')).toHaveLength(0);
|
||||
expect(get('balance_adjustments', id)?.deleted).toBe(true);
|
||||
expect(pendingCount()).toBe(3);
|
||||
await settled();
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
expect(created).toBeGreaterThan(0);
|
||||
expect(disposed).toBe(created);
|
||||
} finally {
|
||||
warn.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('reads inside an effect update after every write', async () => {
|
||||
const id = await save('balance_adjustments', null, { delta_ms: 1000, note: 'first', effective_at: 1 });
|
||||
const total = watch(() => all('balance_adjustments').reduce((sum, a) => sum + a.delta_ms, 0));
|
||||
const note = watch(() => get('balance_adjustments', id)?.note);
|
||||
try {
|
||||
await tick();
|
||||
expect(total.current).toBe(1000);
|
||||
expect(note.current).toBe('first');
|
||||
await update('balance_adjustments', id, { delta_ms: 3000, note: 'second' });
|
||||
await tick();
|
||||
expect(total.current).toBe(3000);
|
||||
expect(note.current).toBe('second');
|
||||
await remove('balance_adjustments', id);
|
||||
await tick();
|
||||
expect(total.current).toBe(0);
|
||||
expect(note.current).toBe('second');
|
||||
} finally {
|
||||
total.dispose();
|
||||
note.dispose();
|
||||
}
|
||||
const runs = total.runs;
|
||||
await save('balance_adjustments', null, { delta_ms: 5, note: '', effective_at: 1 });
|
||||
await tick();
|
||||
expect(total.runs).toBe(runs);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sync boundary', () => {
|
||||
it('toRecord turns end_time 0 into null and coerces the metadata', () => {
|
||||
const doc = toRecord('entries', {
|
||||
|
||||
Reference in New Issue
Block a user