@smilodon/react 1.10.0 → 1.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -45,6 +45,145 @@ The Quick Start examples are self-contained. Longer feature snippets below are f
45
45
 
46
46
  In focused JSX examples, `items`, `groups`, `bigItems`, and similar values are component-local application data unless the snippet declares them; imports and hook state should be added in the containing component as needed.
47
47
 
48
+ ## Acknowledged data loading (Phase 2F-D development candidate)
49
+
50
+ The current local Phase 2F-D candidate integrates the core acknowledged-loading facade with the React adapter. It is not part of the published `1.10.0` package, and Firefox/WebKit certification, independent review, and remaining contract coverage are pending. Treat the following as implementation guidance for this candidate, not a release-readiness or cross-browser compatibility claim.
51
+
52
+ Acknowledged loading is opt-in through per-instance `config.dataLoading`; there is no global acknowledged-loading default. If the config is omitted, the adapter keeps the legacy protocol and existing callback behavior. The host owns fetch, cancellation, and retry policy; the core owns request identity, stale-completion rejection, canonical acceptance, and subsequent DOM projection.
53
+
54
+ ### Remote cursor search with cancellation and failures
55
+
56
+ This illustrative component handles remote search and load-more requests through one host function. It sends the exact query (including an empty string), passes page/cursor position and fetch limit to the server, echoes request identity on completion, aborts cancelled requests, and reports failures without an automatic retry.
57
+
58
+ ```tsx
59
+ import { useEffect, useRef } from 'react'
60
+ import {
61
+ Select,
62
+ type DataRequest,
63
+ type DataRequestCompletion,
64
+ type SelectHandle,
65
+ type SelectItem,
66
+ } from '@smilodon/react'
67
+
68
+ type SearchPage = {
69
+ items: SelectItem[]
70
+ hasMore: boolean
71
+ nextCursor?: string | number
72
+ }
73
+
74
+ export function RemoteSelect() {
75
+ const selectRef = useRef<SelectHandle>(null)
76
+ const controllers = useRef(new Map<string, AbortController>())
77
+
78
+ useEffect(() => () => {
79
+ for (const controller of controllers.current.values()) controller.abort()
80
+ controllers.current.clear()
81
+ }, [])
82
+
83
+ async function load(request: DataRequest) {
84
+ const controller = new AbortController()
85
+ controllers.current.set(request.requestId, controller)
86
+
87
+ try {
88
+ const params = new URLSearchParams({ q: request.query, limit: String(request.fetchLimit) })
89
+ if (request.position.mode === 'page') {
90
+ params.set('page', String(request.position.page))
91
+ } else if (request.position.cursor !== undefined) {
92
+ params.set('cursor', String(request.position.cursor))
93
+ }
94
+
95
+ const response = await fetch(`/api/options?${params}`, { signal: controller.signal })
96
+ if (!response.ok) throw new Error(`Search failed: ${response.status}`)
97
+
98
+ const page = await response.json() as SearchPage
99
+ if (!Array.isArray(page.items) || typeof page.hasMore !== 'boolean') {
100
+ throw new Error('The options endpoint returned an invalid page.')
101
+ }
102
+ if (request.position.mode === 'cursor' && page.hasMore && page.nextCursor === undefined) {
103
+ throw new Error('A non-terminal cursor page must include nextCursor.')
104
+ }
105
+
106
+ const completion: DataRequestCompletion = page.hasMore
107
+ ? {
108
+ requestId: request.requestId,
109
+ queryGeneration: request.queryGeneration,
110
+ status: 'succeeded',
111
+ items: page.items,
112
+ hasMore: true,
113
+ ...(page.nextCursor === undefined ? {} : { nextCursor: page.nextCursor }),
114
+ }
115
+ : {
116
+ requestId: request.requestId,
117
+ queryGeneration: request.queryGeneration,
118
+ status: 'exhausted',
119
+ items: page.items,
120
+ hasMore: false,
121
+ }
122
+
123
+ const result = selectRef.current?.completeDataRequest(completion)
124
+ if (result !== 'accepted') {
125
+ // Stale, unknown, invalid, or resource-limited completions are not projected.
126
+ console.info('Response not accepted:', result)
127
+ }
128
+ } catch (error) {
129
+ if (!controller.signal.aborted) {
130
+ selectRef.current?.completeDataRequest({
131
+ requestId: request.requestId,
132
+ queryGeneration: request.queryGeneration,
133
+ status: 'failed',
134
+ error: { message: error instanceof Error ? error.message : String(error), retryable: true },
135
+ })
136
+ }
137
+ } finally {
138
+ controllers.current.delete(request.requestId)
139
+ }
140
+ }
141
+
142
+ return (
143
+ <Select
144
+ ref={selectRef}
145
+ searchable
146
+ config={{
147
+ dataLoading: {
148
+ protocol: 'acknowledged',
149
+ searchMode: 'remote',
150
+ paginationMode: 'cursor',
151
+ fetchLimit: 25,
152
+ },
153
+ }}
154
+ onSearch={(_query, _results, _count, request) => {
155
+ if (request) void load(request)
156
+ }}
157
+ onLoadMore={(_page, request) => {
158
+ if (request) void load(request)
159
+ }}
160
+ onDataRequestCancel={({ requestId }) => {
161
+ controllers.current.get(requestId)?.abort()
162
+ controllers.current.delete(requestId)
163
+ }}
164
+ onDataProjection={(detail) => {
165
+ if (detail.status === 'failed') console.error(detail.error.message)
166
+ }}
167
+ onDataLoadingConfigError={({ issues }) => {
168
+ console.error('Rejected data-loading config:', issues)
169
+ }}
170
+ />
171
+ )
172
+ }
173
+ ```
174
+
175
+ The coordinator rejects stale responses by request ID and query generation; the host should still abort cancelled network work to save resources. An accepted response does not mean projection is finished: observe `onDataProjection` for the projection result. A failed request remains failed until a new explicit request is made; the sample deliberately does not retry automatically.
176
+
177
+ ### Other useful scenarios
178
+
179
+ - **Empty query:** clearing search still invokes `onSearch` with `request.query === ''`. `URLSearchParams` serializes it as an explicit empty `q` parameter; do not omit it if the endpoint treats an empty query specially.
180
+ - **Page-based pagination:** change `paginationMode` to `'page'`; the request's `position.page` is the page to request. Keep the API's page numbering consistent with `firstPage`.
181
+ - **Hybrid search:** use `searchMode: 'hybrid'` only when the host intends the runtime's hybrid search behavior; the host still answers the remote request with its identity and generation.
182
+ - **React Strict Mode:** the adapter owns readiness and initial request setup. Do not call a second manual request from an effect; keep each host response keyed by the request ID supplied in the callback. Browser certification for this scenario remains pending outside the reported Chromium run.
183
+ - **External busy state:** the React `loading` prop and consumer `aria-busy` remain presentation inputs. The adapter composes them with core busy state; the acknowledged request snapshot's `busy` does not include external presentation loading.
184
+ - **Legacy compatibility:** omit `config.dataLoading` to retain the legacy protocol. Existing `onSearch(query, results, count)` and `onLoadMore(page)` calls remain usable; the new request argument is optional in those callback signatures.
185
+ - **Controlled selection:** keep `value` in application state and update it through `onChange`. Replacing remote result pages does not transfer ownership of the selected value from the application to the transport layer.
186
+
48
187
  ## Infinite Render Loop: Root-Cause Review (No External Dependencies)
49
188
 
50
189
  If you see `Maximum update depth exceeded`, follow this checklist before shipping: