Skip to content

Commit 4de2456

Browse files
authored
Merge pull request #21 from Signalium/update-to-signalium-v3
feat(signalium): bump to Signalium v3
2 parents 70f2cd7 + 7288ebf commit 4de2456

18 files changed

Lines changed: 411 additions & 162 deletions

‎.changeset/upgrade-signalium-v3.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'fetchium': minor
3+
---
4+
5+
Upgrade to Signalium v3 and React 19.
6+
7+
### Peer dependency bumps
8+
9+
- `signalium` peer is now `>=3.0.0` (was `>=2.1.7`).
10+
- `react` peer is now `>=19.0.0` (was `>=18.3.1`). Required by Signalium v3 for `React.cache` and React 19's thenable handling in `<Suspense>`.
11+
12+
### API changes
13+
14+
- `QueryPromise<T>` now resolves to `ReactivePromise<QueryResult<T>>`. In Signalium v3 `ReactivePromise<T>` is itself the discriminated union (the v2 `DiscriminatedReactivePromise<T>` alias has been removed upstream), so `if (p.isReady)` narrows `p.value` to `T` directly without the previous extra type gymnastics. No call-site changes are required for code that already used `QueryPromise<T>`.
15+
- `useQuery` is now a thin wrapper around v3's deep-by-default `useReactive`. The previous hand-rolled `cloneDeep` proxy implementation has been removed — Signalium v3 provides structurally-shared snapshots natively, so `React.memo` on subtree props now correctly skips re-renders when the underlying data is unchanged.
16+
17+
### Internal
18+
19+
- Registers a custom Signalium snapshot for entity proxies (`registerCustomSnapshot`), so `useReactive` / `useQuery` correctly walk into entities at the React boundary and re-render on field changes.
20+
- All `useReactive(fn, ...args)` call sites in tests updated to the v3 thunk form `useReactive(() => fn(...args))`.
21+
- `SuspendSignalsProvider` references replaced with `PauseSignalsProvider` (renamed upstream in v3).

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ dist
44
.turbo
55
packages/fetchium/*.js
66
packages/fetchium/*.d.ts
7+
packages/fetchium/stores/
78
packages/fetchium/plugin/docs/
89
__screenshots__/
910
.next

‎docs/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"@tailwindcss/postcss": "^4.0.0",
2222
"@tailwindcss/typography": "^0.5.7",
2323
"@types/node": "^25.5.0",
24-
"@types/react": "^18.2.47",
25-
"@types/react-dom": "^18.2.18",
24+
"@types/react": "^19.0.0",
25+
"@types/react-dom": "^19.0.0",
2626
"@types/react-highlight-words": "^0.16.4",
2727
"classnames": "^2.5.1",
2828
"clsx": "^2.1.0",

‎docs/src/app/api/fetchium-react/page.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function useQuery<T extends Query>(
2424
): QueryPromise<T>;
2525
```
2626

27-
React hook for fetching a query. Subscribes the component to the query's reactive state, re-rendering when the query result changes. Internally uses Signalium's `useReactive` to bridge the reactive signal system with React's rendering cycle.
27+
React hook for fetching a query. Subscribes the component to the query's reactive state, re-rendering when the query result changes. Internally uses Signalium's `useReactive` (deep-by-default in v3) to bridge the reactive signal system with React's rendering cycle.
2828

2929
#### Parameters
3030

@@ -35,7 +35,7 @@ React hook for fetching a query. Subscribes the component to the query's reactiv
3535

3636
#### Returns
3737

38-
`QueryPromise<T>` — a `DiscriminatedReactivePromise` that provides the query state.
38+
`QueryPromise<T>` — a `ReactivePromise` that provides the query state.
3939

4040
The returned promise object has the following properties:
4141

@@ -130,6 +130,7 @@ const SearchResults = component(() => {
130130

131131
#### Notes
132132

133-
- `useQuery` calls `useReactive` twice internally: once for the query itself and once to subscribe to the resolved value for deep entity tracking via Signalium's `CONSUME_DEEP` protocol.
134-
- The `value` property returns a **deep clone** of the query result to prevent accidental mutation of the entity cache. Use `draft()` from `fetchium` if you need a mutable copy for mutations.
133+
- `useQuery` is a thin wrapper around Signalium v3's `useReactive`, which is deep-by-default. It returns a **structurally-shared snapshot** of the query result, so memoized children that receive subtrees as props keep stable references when the underlying data is unchanged.
134+
- Fetchium registers a custom snapshot for entity proxies so the snapshot walks into entities (instead of returning them by reference), giving you correct re-rendering on entity field changes.
135+
- The snapshot is a plain-object copy of the query result to prevent accidental mutation of the entity cache. Use `draft()` from `fetchium` if you need a mutable copy for mutations.
135136
- When used with React Suspense, reading `.value` on a pending query will suspend the component.

‎docs/src/app/api/fetchium/page.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ function fetchQuery<T extends Query>(
405405
): QueryPromise<T>;
406406
```
407407

408-
Fetches a query reactively. Must be called within a reactive context where `QueryClientContext` is provided. Returns a `QueryPromise` (a `DiscriminatedReactivePromise`) that resolves to the query result.
408+
Fetches a query reactively. Must be called within a reactive context where `QueryClientContext` is provided. Returns a `QueryPromise` (a `ReactivePromise`) that resolves to the query result.
409409

410410
| Parameter | Type | Description |
411411
| ------------ | ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
@@ -599,7 +599,7 @@ The `t` object provides a declarative type definition DSL for describing query p
599599

600600
| Type | Definition | Description |
601601
| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
602-
| `QueryPromise<T extends Query>` | `DiscriminatedReactivePromise<QueryResult<T>>` | The return type of `fetchQuery`. A reactive promise. |
602+
| `QueryPromise<T extends Query>` | `ReactivePromise<QueryResult<T>>` | The return type of `fetchQuery`. A reactive promise. |
603603
| `QueryResult<T extends Query>` | `ExtractType<T['result']> & { __refetch(), __fetchNext(), __hasNext, __isFetchingNext }` | The resolved value of a query. Includes pagination helpers. |
604604
| `QueryCacheOptions` | `{ maxCount?: number; cacheTime?: number }` | Persistent storage cache settings. `cacheTime` is in minutes (default: 1440 / 24 hours). `maxCount` is the LRU queue size. |
605605
| `QueryConfigOptions` | `{ gcTime?, staleTime?, debounce?, networkMode?, retry?, refreshStaleOnReconnect?, subscribe? }` | Instance-level query configuration. See property table below. |

‎package-lock.json‎

Lines changed: 52 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"packageManager": "npm@8.5.0",
2525
"dependencies": {
2626
"@changesets/cli": "^2.29.8",
27-
"@types/react": "^18.3.12",
27+
"@types/react": "^19.0.0",
2828
"eslint": "^9.15.0",
2929
"eslint-plugin-react-hooks": "^5.0.0",
3030
"eslint-plugin-react-refresh": "^0.4.14",

‎packages/fetchium/package.json‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@
139139
"fetchium-agents": "./plugin/install.mjs"
140140
},
141141
"peerDependencies": {
142-
"react": ">=18.3.1",
143-
"signalium": ">=2.1.7"
142+
"react": ">=19.0.0",
143+
"signalium": ">=3.0.0"
144144
},
145145
"peerDependenciesMeta": {
146146
"react": {
@@ -173,10 +173,10 @@
173173
"@vitejs/plugin-react": "^5.0.0",
174174
"@vitest/browser": "^3.0.6",
175175
"playwright": "^1.50.1",
176-
"react": "^18.3.1",
177-
"react-dom": "^18.3.1",
176+
"react": "^19.0.0",
177+
"react-dom": "^19.0.0",
178178
"rollup-plugin-const-enum": "^1.1.4",
179-
"signalium": "2.3.1",
179+
"signalium": "3.0.0",
180180
"vite": "^7.1.2",
181181
"vite-plugin-babel": "^1.3.0",
182182
"vite-plugin-dts": "^4.5.4",

‎packages/fetchium/src/EntityInstance.ts‎

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import {
2-
relay,
3-
type DiscriminatedReactivePromise,
4-
type Notifier,
5-
notifier,
6-
reactiveMethod,
7-
setScopeOwner,
8-
} from 'signalium';
1+
import { relay, type ReactivePromise, type Notifier, notifier, reactiveMethod, setScopeOwner } from 'signalium';
2+
import { registerCustomSnapshot } from 'signalium/utils';
3+
4+
type SnapshotFn = (current: unknown, prev: unknown) => unknown;
95
import { type EntityDef, Mask } from './types.js';
106
import { GcKeyType } from './GcManager.js';
117
import { Entity } from './proxy.js';
@@ -95,6 +91,53 @@ const objectWrappingHandler: ProxyHandler<Record<string, unknown>> = {
9591
},
9692
};
9793

94+
// ======================================================
95+
// Custom snapshot for entity proxies — bridges Signalium v3's `useReactive`
96+
// structural snapshot mechanism into our entity proxy graph.
97+
//
98+
// Without this, the snapshot encounters the entity proxy (whose prototype is
99+
// the user-defined entity class), doesn't recognize it, and returns it as-is.
100+
// React then never re-renders on data changes because the proxy reference is
101+
// stable. Walking the proxy via `Object.keys` + property access establishes
102+
// reactive dependencies on the entity's notifier and produces a plain-object
103+
// snapshot whose unchanged subtrees keep stable references.
104+
// ======================================================
105+
106+
const snapshotEntity = (current: object, prev: unknown, snap: SnapshotFn): unknown => {
107+
const obj = current as Record<string, unknown>;
108+
const keys = Object.keys(obj);
109+
const prevObj =
110+
prev !== null && typeof prev === 'object' && !Array.isArray(prev) ? (prev as Record<string, unknown>) : undefined;
111+
let changed = !prevObj || Object.keys(prevObj).length !== keys.length;
112+
const result: Record<string, unknown> = {};
113+
for (let i = 0; i < keys.length; i++) {
114+
const key = keys[i];
115+
const val = obj[key];
116+
let next: unknown;
117+
if (typeof val === 'function') {
118+
// Methods (e.g. __refetch, entity methods) are bound to the original
119+
// proxy via closure, so preserve the reference directly.
120+
next = val;
121+
} else {
122+
next = snap(val, prevObj?.[key]);
123+
}
124+
result[key] = next;
125+
if (!changed && next !== prevObj?.[key]) changed = true;
126+
}
127+
return changed ? result : prevObj;
128+
};
129+
130+
const registeredEntityProtos = new WeakSet<object>();
131+
132+
function ensureEntitySnapshotRegistered(ctor: new (...args: unknown[]) => object): void {
133+
const proto = ctor.prototype;
134+
if (registeredEntityProtos.has(proto)) return;
135+
registeredEntityProtos.add(proto);
136+
registerCustomSnapshot(ctor, snapshotEntity as Parameters<typeof registerCustomSnapshot>[1]);
137+
}
138+
139+
ensureEntitySnapshotRegistered(Entity as unknown as new (...args: unknown[]) => object);
140+
98141
// ======================================================
99142

100143
export class EntityInstance {
@@ -272,14 +315,17 @@ function createProxy(
272315
const entityClass = validatorDef._entityClass;
273316
const entityConfig = validatorDef._entityConfig;
274317
const proto = entityClass ? entityClass.prototype : Entity.prototype;
318+
if (entityClass !== undefined) {
319+
ensureEntitySnapshotRegistered(entityClass as unknown as new (...args: unknown[]) => object);
320+
}
275321
const typenameField = shape.typenameField;
276322

277323
const wrappedMethods = new Map<string, (...args: unknown[]) => unknown>();
278324
const filterCache = new Map<string, { source: unknown[]; filtered: unknown[] }>();
279325

280326
const toJSON = () => ({ __entityRef: key });
281327

282-
let entityRelay: DiscriminatedReactivePromise<Record<string, unknown>> | undefined;
328+
let entityRelay: ReactivePromise<Record<string, unknown>> | undefined;
283329

284330
if (entityConfig?.hasSubscribe && methods && '__subscribe' in methods) {
285331
entityRelay = relay(state => {

‎packages/fetchium/src/QueryResult.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { relay, type RelayState, DiscriminatedReactivePromise } from 'signalium';
1+
import { relay, type RelayState, ReactivePromise } from 'signalium';
22
import { NetworkMode, type QueryResult, type EntityDef } from './types.js';
33
import {
44
type QueryClient,
@@ -25,7 +25,7 @@ export class QueryInstance<T extends Query> {
2525
storageKey: number = -1;
2626

2727
/** The public-facing ReactivePromise returned to consumers. */
28-
readonly relay: DiscriminatedReactivePromise<QueryResult<T>>;
28+
readonly relay: ReactivePromise<QueryResult<T>>;
2929

3030
private queryClient: QueryClient;
3131
private initialized: boolean = false;
@@ -355,7 +355,7 @@ export class QueryInstance<T extends Query> {
355355
// Public methods
356356
// ======================================================
357357

358-
refetch = (): DiscriminatedReactivePromise<QueryResult<T>> => {
358+
refetch = (): ReactivePromise<QueryResult<T>> => {
359359
if (this.relayState.isPending) return this.relay;
360360
this.runQueryImmediately();
361361
return this.relay;

0 commit comments

Comments
 (0)