|
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; |
9 | 5 | import { type EntityDef, Mask } from './types.js'; |
10 | 6 | import { GcKeyType } from './GcManager.js'; |
11 | 7 | import { Entity } from './proxy.js'; |
@@ -95,6 +91,53 @@ const objectWrappingHandler: ProxyHandler<Record<string, unknown>> = { |
95 | 91 | }, |
96 | 92 | }; |
97 | 93 |
|
| 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 | + |
98 | 141 | // ====================================================== |
99 | 142 |
|
100 | 143 | export class EntityInstance { |
@@ -272,14 +315,17 @@ function createProxy( |
272 | 315 | const entityClass = validatorDef._entityClass; |
273 | 316 | const entityConfig = validatorDef._entityConfig; |
274 | 317 | const proto = entityClass ? entityClass.prototype : Entity.prototype; |
| 318 | + if (entityClass !== undefined) { |
| 319 | + ensureEntitySnapshotRegistered(entityClass as unknown as new (...args: unknown[]) => object); |
| 320 | + } |
275 | 321 | const typenameField = shape.typenameField; |
276 | 322 |
|
277 | 323 | const wrappedMethods = new Map<string, (...args: unknown[]) => unknown>(); |
278 | 324 | const filterCache = new Map<string, { source: unknown[]; filtered: unknown[] }>(); |
279 | 325 |
|
280 | 326 | const toJSON = () => ({ __entityRef: key }); |
281 | 327 |
|
282 | | - let entityRelay: DiscriminatedReactivePromise<Record<string, unknown>> | undefined; |
| 328 | + let entityRelay: ReactivePromise<Record<string, unknown>> | undefined; |
283 | 329 |
|
284 | 330 | if (entityConfig?.hasSubscribe && methods && '__subscribe' in methods) { |
285 | 331 | entityRelay = relay(state => { |
|
0 commit comments