@jasonshimmy/vite-plugin-cer-app 0.23.0 → 0.23.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/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Changelog
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
+ ## [v0.23.1] - 2026-04-17
5
+
6
+ - fix: fix useContentSearch loading state, debounce, and automatic retry on fetch failure (4d0207a)
7
+
4
8
  ## [v0.23.0] - 2026-04-17
5
9
 
6
10
  - feat: enhance useContentSearch with loading state and update related tests (d1cee0e)
package/commits.txt CHANGED
@@ -1 +1 @@
1
- - feat: enhance useContentSearch with loading state and update related tests (d1cee0e)
1
+ - fix: fix useContentSearch loading state, debounce, and automatic retry on fetch failure (4d0207a)
@@ -5,6 +5,9 @@ import type { ContentSearchResult } from '../../types/content.js';
5
5
  * Returns the same Promise on repeated calls — the index is built at most once
6
6
  * per session regardless of how many search components are mounted.
7
7
  *
8
+ * If the fetch fails the singleton is cleared so the next search attempt
9
+ * retries automatically (no page reload required after a transient error).
10
+ *
8
11
  * @internal Exported for unit testing only.
9
12
  */
10
13
  export declare function loadIndex(): Promise<unknown>;
@@ -1 +1 @@
1
- {"version":3,"file":"use-content-search.d.ts","sourceRoot":"","sources":["../../../src/runtime/composables/use-content-search.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAA;AACzE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AASjE;;;;;;GAMG;AACH,wBAAsB,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAiBlD;AAED,uEAAuE;AACvE,wBAAgB,mBAAmB,IAAI,IAAI,CAE1C;AAID,MAAM,WAAW,sBAAsB;IACrC,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;IAC5B,OAAO,EAAE,aAAa,CAAC,mBAAmB,EAAE,CAAC,CAAA;IAC7C,mJAAmJ;IACnJ,OAAO,EAAE,aAAa,CAAC,OAAO,CAAC,CAAA;CAChC;AAuDD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,gBAAgB,IAAI,sBAAsB,CAEzD"}
1
+ {"version":3,"file":"use-content-search.d.ts","sourceRoot":"","sources":["../../../src/runtime/composables/use-content-search.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAA;AACzE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AASjE;;;;;;;;;GASG;AACH,wBAAgB,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAuB5C;AAED,uEAAuE;AACvE,wBAAgB,mBAAmB,IAAI,IAAI,CAE1C;AA+BD,MAAM,WAAW,sBAAsB;IACrC,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;IAC5B,OAAO,EAAE,aAAa,CAAC,mBAAmB,EAAE,CAAC,CAAA;IAC7C,mJAAmJ;IACnJ,OAAO,EAAE,aAAa,CAAC,OAAO,CAAC,CAAA;CAChC;AA4ED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,gBAAgB,IAAI,sBAAsB,CAEzD"}
@@ -1,4 +1,4 @@
1
- import { createComposable, ref, useOnConnected, watch, } from '@jasonshimmy/custom-elements-runtime';
1
+ import { createComposable, getCurrentComponentContext, ref, useOnConnected, useOnDisconnected, watch, } from '@jasonshimmy/custom-elements-runtime';
2
2
  import { contentSearchIndexUrl } from '../content/client.js';
3
3
  // ─── Module-level singleton ───────────────────────────────────────────────────
4
4
  // The MiniSearch instance is built lazily on first search from the manifest.
@@ -9,12 +9,15 @@ let _indexPromise = null;
9
9
  * Returns the same Promise on repeated calls — the index is built at most once
10
10
  * per session regardless of how many search components are mounted.
11
11
  *
12
+ * If the fetch fails the singleton is cleared so the next search attempt
13
+ * retries automatically (no page reload required after a transient error).
14
+ *
12
15
  * @internal Exported for unit testing only.
13
16
  */
14
- export async function loadIndex() {
17
+ export function loadIndex() {
15
18
  if (_indexPromise)
16
19
  return _indexPromise;
17
- _indexPromise = (async () => {
20
+ const attempt = (async () => {
18
21
  const [{ default: MiniSearch }, raw] = await Promise.all([
19
22
  import('minisearch'),
20
23
  fetch(contentSearchIndexUrl()).then((r) => {
@@ -29,55 +32,94 @@ export async function loadIndex() {
29
32
  idField: '_path',
30
33
  });
31
34
  })();
32
- return _indexPromise;
35
+ _indexPromise = attempt;
36
+ // Clear the singleton on failure so the next call retries the fetch.
37
+ // The === guard ensures a newer concurrent attempt is not accidentally cleared.
38
+ attempt.catch(() => {
39
+ if (_indexPromise === attempt)
40
+ _indexPromise = null;
41
+ });
42
+ return attempt;
33
43
  }
34
44
  /** Resets the module-level singleton. Used in tests only. @internal */
35
45
  export function resetIndexSingleton() {
36
46
  _indexPromise = null;
37
47
  }
48
+ const _STATE_KEY = '_cerSearchDebounce';
49
+ function getDebounceState(ctx) {
50
+ if (!Object.prototype.hasOwnProperty.call(ctx, _STATE_KEY)) {
51
+ Object.defineProperty(ctx, _STATE_KEY, {
52
+ value: { seq: 0, timer: null },
53
+ writable: false, // object ref is fixed; its properties are still mutable
54
+ enumerable: false, // invisible to the reactive proxy set-trap
55
+ configurable: false,
56
+ });
57
+ }
58
+ return ctx[_STATE_KEY];
59
+ }
38
60
  const _factory = createComposable(() => {
39
61
  const query = ref('');
40
62
  const results = ref([]);
41
63
  const loading = ref(false);
42
- // Pre-warm index on mount
64
+ // Debounce state lives on the component context, not in local render-body variables.
65
+ // Local variables are re-created on every re-render; the context object is stable
66
+ // for the lifetime of the element. Storing state here lets the render-body
67
+ // watch() (see below) pick up the same timer and sequence counter across re-renders
68
+ // without the watcher accumulation that occurs when watch() is placed inside
69
+ // useOnConnected() (which runs once per mount but is not registered for cleanup
70
+ // by the reactive system, leaking watchers on every disconnect + reconnect cycle).
71
+ const state = getDebounceState(getCurrentComponentContext());
72
+ // Pre-warm the index on first mount so the first real search is faster.
43
73
  useOnConnected(() => {
44
74
  loadIndex().catch(() => { });
45
75
  });
46
- // Monotonic counter to discard stale async results
47
- let _seq = 0;
48
- let _debounceTimer = null;
76
+ // Cancel any in-flight debounce on unmount so stale async work doesn't land
77
+ // after the component is gone.
78
+ useOnDisconnected(() => {
79
+ if (state.timer !== null) {
80
+ clearTimeout(state.timer);
81
+ state.timer = null;
82
+ }
83
+ state.seq++; // discard any in-flight async search
84
+ loading.value = false;
85
+ });
86
+ // watch() is in the render body so the reactive system registers it under the
87
+ // current component and tears it down automatically on re-render and disconnect.
88
+ // The mutable state (seq / timer) lives on the context (above) and persists
89
+ // across re-renders — new watcher instances see the same timer and counter,
90
+ // which is what makes debounce cancellation correct even after a re-render.
49
91
  watch(query, (q) => {
50
- if (_debounceTimer !== null) {
51
- clearTimeout(_debounceTimer);
52
- _debounceTimer = null;
92
+ if (state.timer !== null) {
93
+ clearTimeout(state.timer);
94
+ state.timer = null;
53
95
  }
54
96
  if (!q) {
55
97
  // Increment seq so any in-flight async search is discarded when it resolves
56
- _seq++;
98
+ state.seq++;
57
99
  loading.value = false;
58
100
  results.value = [];
59
101
  return;
60
102
  }
61
103
  // Signal loading immediately so the UI can respond before the debounce fires
62
104
  loading.value = true;
63
- _debounceTimer = setTimeout(async () => {
64
- _debounceTimer = null;
65
- const seq = ++_seq;
105
+ state.timer = setTimeout(async () => {
106
+ state.timer = null;
107
+ const seq = ++state.seq;
66
108
  try {
67
109
  const index = await loadIndex();
68
- if (seq !== _seq)
110
+ if (seq !== state.seq)
69
111
  return; // stale — a newer query is in flight
70
112
  results.value = index.search(q, { prefix: true });
71
113
  }
72
114
  catch {
73
- if (seq !== _seq)
115
+ if (seq !== state.seq)
74
116
  return;
75
117
  results.value = [];
76
118
  }
77
119
  finally {
78
120
  // Only clear loading for the most recent search; a newer in-flight search
79
121
  // keeps loading=true until it settles.
80
- if (seq === _seq)
122
+ if (seq === state.seq)
81
123
  loading.value = false;
82
124
  }
83
125
  }, 200);
@@ -1 +1 @@
1
- {"version":3,"file":"use-content-search.js","sourceRoot":"","sources":["../../../src/runtime/composables/use-content-search.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,GAAG,EACH,cAAc,EACd,KAAK,GACN,MAAM,sCAAsC,CAAA;AAG7C,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AAE5D,iFAAiF;AAEjF,6EAA6E;AAC7E,iFAAiF;AACjF,IAAI,aAAa,GAA4B,IAAI,CAAA;AAEjD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS;IAC7B,IAAI,aAAa;QAAE,OAAO,aAAa,CAAA;IACvC,aAAa,GAAG,CAAC,KAAK,IAAI,EAAE;QAC1B,MAAM,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE,GAAG,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;YACvD,MAAM,CAAC,YAAY,CAAC;YACpB,KAAK,CAAC,qBAAqB,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE;gBACxC,IAAI,CAAC,CAAC,CAAC,EAAE;oBAAE,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAA;gBACvE,OAAO,CAAC,CAAC,IAAI,EAAE,CAAA;YACjB,CAAC,CAAC;SACH,CAAC,CAAA;QACF,OAAO,UAAU,CAAC,QAAQ,CAAC,GAAG,EAAE;YAC9B,MAAM,EAAE,CAAC,OAAO,EAAE,aAAa,CAAC;YAChC,WAAW,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,aAAa,CAAC;YAC9C,OAAO,EAAE,OAAO;SACjB,CAAC,CAAA;IACJ,CAAC,CAAC,EAAE,CAAA;IACJ,OAAO,aAAa,CAAA;AACtB,CAAC;AAED,uEAAuE;AACvE,MAAM,UAAU,mBAAmB;IACjC,aAAa,GAAG,IAAI,CAAA;AACtB,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YAC5E,CAAC;YAAC,MAAM,CAAC;gBACP,IAAI,GAAG,KAAK,IAAI;oBAAE,OAAM;gBACxB,OAAO,CAAC,KAAK,GAAG,EAAE,CAAA;YACpB,CAAC;oBAAS,CAAC;gBACT,0EAA0E;gBAC1E,uCAAuC;gBACvC,IAAI,GAAG,KAAK,IAAI;oBAAE,OAAO,CAAC,KAAK,GAAG,KAAK,CAAA;YACzC,CAAC;QACH,CAAC,EAAE,GAAG,CAAC,CAAA;IACT,CAAC,CAAC,CAAA;IAEF,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,CAAA;AACpC,CAAC,CAAC,CAAA;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,UAAU,gBAAgB;IAC9B,OAAO,QAAQ,EAAE,CAAA;AACnB,CAAC"}
1
+ {"version":3,"file":"use-content-search.js","sourceRoot":"","sources":["../../../src/runtime/composables/use-content-search.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,0BAA0B,EAC1B,GAAG,EACH,cAAc,EACd,iBAAiB,EACjB,KAAK,GACN,MAAM,sCAAsC,CAAA;AAG7C,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AAE5D,iFAAiF;AAEjF,6EAA6E;AAC7E,iFAAiF;AACjF,IAAI,aAAa,GAA4B,IAAI,CAAA;AAEjD;;;;;;;;;GASG;AACH,MAAM,UAAU,SAAS;IACvB,IAAI,aAAa;QAAE,OAAO,aAAa,CAAA;IACvC,MAAM,OAAO,GAAG,CAAC,KAAK,IAAI,EAAE;QAC1B,MAAM,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE,GAAG,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;YACvD,MAAM,CAAC,YAAY,CAAC;YACpB,KAAK,CAAC,qBAAqB,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE;gBACxC,IAAI,CAAC,CA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AAG,KAAK,CAAA;YACrB,OAAO,CAAC,KAAK,GAAG,EAAE,CAAA;YAClB,OAAM;QACR,CAAC;QAED,6EAA6E;QAC7E,OAAO,CAAC,KAAK,GAAG,IAAI,CAAA;QAEpB,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC,KAAK,IAAI,EAAE;YAClC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAA;YAClB,MAAM,GAAG,GAAG,EAAE,KAAK,CAAC,GAAG,CAAA;YAEvB,IAAI,CAAC;gBACH,MAAM,KAAK,GAAG,MAAM,SAAS,EAA+E,CAAA;gBAC5G,IAAI,GAAG,KAAK,KAAK,CAAC,GAAG;oBAAE,OAAM,CAAC,qCAAqC;gBACnE,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAA0B,CAAA;YAC5E,CAAC;YAAC,MAAM,CAAC;gBACP,IAAI,GAAG,KAAK,KAAK,CAAC,GAAG;oBAAE,OAAM;gBAC7B,OAAO,CAAC,KAAK,GAAG,EAAE,CAAA;YACpB,CAAC;oBAAS,CAAC;gBACT,0EAA0E;gBAC1E,uCAAuC;gBACvC,IAAI,GAAG,KAAK,KAAK,CAAC,GAAG;oBAAE,OAAO,CAAC,KAAK,GAAG,KAAK,CAAA;YAC9C,CAAC;QACH,CAAC,EAAE,GAAG,CAAC,CAAA;IACT,CAAC,CAAC,CAAA;IAEF,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,CAAA;AACpC,CAAC,CAAC,CAAA;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,UAAU,gBAAgB;IAC9B,OAAO,QAAQ,EAAE,CAAA;AACnB,CAAC"}
package/docs/content.md CHANGED
@@ -473,18 +473,19 @@ Important behavior notes:
473
473
 
474
474
  **Auto-imported** in pages, layouts, and components.
475
475
 
476
- Returns reactive `query` and `results` refs. The MiniSearch index is loaded once on the client (lazily, the first time the composable is used). Search is debounce-free results update synchronously on each keypress, but the initial index fetch is async.
476
+ Returns reactive `query`, `results`, and `loading` refs. The MiniSearch index is loaded lazily the first time the component mounts (pre-warmed via `useOnConnected`) and cached for the lifetime of the session. Input is debounced (200 ms) so the index is not queried on every keystroke.
477
477
 
478
478
  ```ts
479
- const { query, results } = useContentSearch()
479
+ const { query, results, loading } = useContentSearch()
480
480
  ```
481
481
 
482
482
  ### Return value
483
483
 
484
484
  ```ts
485
485
  interface UseContentSearchReturn {
486
- query: Ref<string> // bind to an <input> value
486
+ query: Ref<string> // bind with :model or @input
487
487
  results: Ref<ContentSearchResult[]> // reactive search results
488
+ loading: Ref<boolean> // true from first keystroke until results arrive
488
489
  }
489
490
  ```
490
491
 
@@ -492,10 +493,11 @@ interface UseContentSearchReturn {
492
493
 
493
494
  ```ts
494
495
  component('page-search', () => {
495
- const { query, results } = useContentSearch()
496
+ const { query, results, loading } = useContentSearch()
496
497
 
497
498
  return html`
498
499
  <input type="search" :model="${query}" placeholder="Search…" />
500
+ ${loading.value ? html`<p>Searching…</p>` : ''}
499
501
  <ul>
500
502
  ${each(results.value, r => html`
501
503
  <li><a :href="${r._path}">${r.title}</a></li>
@@ -505,7 +507,7 @@ component('page-search', () => {
505
507
  })
506
508
  ```
507
509
 
508
- Search activates when `query.value.length >= 2`. Empty or single-character queries return an empty array.
510
+ `loading` becomes `true` as soon as the user types anything and returns to `false` once results arrive or the query is cleared. An empty query clears results immediately without waiting for the debounce. Search is always client-side in SSR mode the component renders with empty results and hydrates on mount.
509
511
 
510
512
  ### Searched fields
511
513
 
@@ -303,6 +303,63 @@ describe('Content search — useContentSearch()', () => {
303
303
  setSearchQuery('')
304
304
  cy.get('[data-cy=content-search-result]').should('not.exist')
305
305
  })
306
+
307
+ it('shows no-results message for an unmatched query', () => {
308
+ // Use a query with no underscores — MiniSearch tokenizes on \p{P} which
309
+ // includes underscores, splitting e.g. "foo_bar" into two tokens that can
310
+ // accidentally match real content via OR semantics.
311
+ cy.visit('/content-search')
312
+ cy.wait('@searchIndex')
313
+ setSearchQuery('zzznomatch')
314
+ // Wait for the empty state — implicitly waits for loading to clear first
315
+ cy.get('[data-cy=content-search-empty]', { timeout: 8000 }).should('exist')
316
+ cy.get('[data-cy=content-search-result]').should('not.exist')
317
+ cy.get('[data-cy=content-search-loading]').should('not.exist')
318
+ })
319
+
320
+ it('sequential search: search → clear → search again returns correct results', () => {
321
+ cy.visit('/content-search')
322
+ cy.wait('@searchIndex')
323
+
324
+ // First search
325
+ setSearchQuery('Hello')
326
+ cy.get('[data-cy=content-search-result]', { timeout: 8000 }).should('contain', 'Hello World')
327
+
328
+ // Clear — results gone
329
+ setSearchQuery('')
330
+ cy.get('[data-cy=content-search-result]').should('not.exist')
331
+ cy.get('[data-cy=content-search-empty]').should('not.exist')
332
+
333
+ // Second search with a different term
334
+ setSearchQuery('Getting')
335
+ cy.get('[data-cy=content-search-result]', { timeout: 8000 }).should('contain', 'Getting Started')
336
+ // First search result must not bleed through
337
+ cy.get('[data-cy=content-search-result]').each(($el) => {
338
+ expect($el.text()).not.to.include('Hello World')
339
+ })
340
+ })
341
+
342
+ it('rapid typing triggers only one search request (debounce)', () => {
343
+ cy.visit('/content-search')
344
+ cy.wait('@searchIndex')
345
+
346
+ // Type characters in quick succession — each triggers a new watch callback
347
+ // but only the last one should produce a network request after 200 ms.
348
+ cy.intercept('GET', '/_content/search-index.json').as('secondIndex')
349
+ setSearchQuery('G')
350
+ setSearchQuery('Ge')
351
+ setSearchQuery('Get')
352
+ setSearchQuery('Gett')
353
+ setSearchQuery('Getti')
354
+ setSearchQuery('Getting')
355
+
356
+ // Results arrive for the final query
357
+ cy.get('[data-cy=content-search-result]', { timeout: 8000 }).should('contain', 'Getting Started')
358
+
359
+ // The search index is cached after the first pre-warm fetch (singleton), so
360
+ // no additional network request should occur for these follow-on searches.
361
+ cy.get('@secondIndex.all').should('have.length', 0)
362
+ })
306
363
  })
307
364
 
308
365
  // ─── /content-fallback ────────────────────────────────────────────────────────
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jasonshimmy/vite-plugin-cer-app",
3
- "version": "0.23.0",
3
+ "version": "0.23.1",
4
4
  "description": "Nuxt-style meta-framework for @jasonshimmy/custom-elements-runtime",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -0,0 +1,405 @@
1
+ /**
2
+ * Tests for the useContentSearch() composable.
3
+ *
4
+ * Covers:
5
+ * - Initial state — empty query, empty results, loading=false
6
+ * - loading state — true immediately on typing, false after results arrive
7
+ * - Debounce timing — results are withheld until 200 ms after the last keystroke
8
+ * - Timer cancellation — a new query before 200 ms resets the debounce clock
9
+ * - Empty-query path — clears loading + results immediately, cancels pending timer
10
+ * - Disconnect cleanup — pending timer is cancelled and loading reset on unmount
11
+ * - Results content — correct items returned for each query
12
+ *
13
+ * @jasonshimmy/custom-elements-runtime is mocked so these tests run without a
14
+ * real DOM or component context. The watch() callback is available immediately
15
+ * after useContentSearch() is called (it is registered during the render-body
16
+ * call, not inside useOnConnected), so no triggerConnected() is needed before
17
+ * simulateType().
18
+ *
19
+ * Note: verifying that debounced input triggers exactly one loadIndex() call
20
+ * is an end-to-end concern exercised in content.cy.ts — the seq-stale guard
21
+ * discards duplicate search results even when debounce is absent, making the
22
+ * count indistinguishable at the unit level.
23
+ */
24
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
25
+ import { buildSearchIndex } from '../../plugin/content/search.js'
26
+ import type { ContentItem } from '../../types/content.js'
27
+
28
+ // ─── Sample content ───────────────────────────────────────────────────────────
29
+
30
+ const ITEMS: ContentItem[] = [
31
+ {
32
+ _path: '/blog/hello',
33
+ _file: 'blog/hello.md',
34
+ _type: 'markdown',
35
+ title: 'Hello World',
36
+ description: 'A post about hello',
37
+ body: '<p>Hello</p>',
38
+ toc: [],
39
+ },
40
+ {
41
+ _path: '/docs/start',
42
+ _file: 'docs/start.md',
43
+ _type: 'markdown',
44
+ title: 'Getting Started',
45
+ description: 'How to get started',
46
+ body: '<p>Start</p>',
47
+ toc: [],
48
+ },
49
+ ]
50
+
51
+ // ─── Runtime mock ─────────────────────────────────────────────────────────────
52
+ //
53
+ // The mock simulates just enough of the runtime for these unit tests:
54
+ // _currentMockContext — fresh plain object each test; receives _cerSearchDebounce
55
+ // _connectedCallbacks — useOnConnected callbacks (pre-warm only in new implementation)
56
+ // _disconnectedCallbacks — useOnDisconnected callbacks (timer cleanup)
57
+ // _watchCallback — the single watch(query, cb) handler registered during render
58
+
59
+ let _currentMockContext: Record<string, unknown> = {}
60
+ let _connectedCallbacks: Array<() => void> = []
61
+ let _disconnectedCallbacks: Array<() => void> = []
62
+ let _watchCallback: ((q: string) => void) | null = null
63
+
64
+ vi.mock('@jasonshimmy/custom-elements-runtime', () => ({
65
+ // Run the factory immediately; getCurrentComponentContext() returns the mock context.
66
+ createComposable: (fn: () => unknown) => () => fn(),
67
+ // Minimal reactive ref: plain object with getter/setter.
68
+ ref: (initial: unknown) => {
69
+ let _val = initial
70
+ return {
71
+ get value() { return _val },
72
+ set value(v: unknown) { _val = v },
73
+ }
74
+ },
75
+ // Capture the single watch() callback registered by the composable.
76
+ watch: (_state: unknown, cb: (val: string) => void) => {
77
+ _watchCallback = cb
78
+ },
79
+ // Capture useOnConnected callbacks for manual triggering (pre-warm only).
80
+ useOnConnected: (cb: () => void) => {
81
+ _connectedCallbacks.push(cb)
82
+ },
83
+ // Capture useOnDisconnected callbacks for manual triggering.
84
+ useOnDisconnected: (cb: () => void) => {
85
+ _disconnectedCallbacks.push(cb)
86
+ },
87
+ // Return the fresh mock context so getDebounceState() can attach _cerSearchDebounce.
88
+ getCurrentComponentContext: () => _currentMockContext,
89
+ }))
90
+
91
+ // Static imports resolve AFTER vi.mock hoisting, so the mock is in place when
92
+ // use-content-search.js's module-level createComposable() call executes.
93
+ import { useContentSearch, resetIndexSingleton } from '../../runtime/composables/use-content-search.js'
94
+ import type { UseContentSearchReturn } from '../../runtime/composables/use-content-search.js'
95
+
96
+ // ─── Helpers ──────────────────────────────────────────────────────────────────
97
+
98
+ /** Flush all useOnConnected callbacks (simulates component mount). */
99
+ function triggerConnected(): void {
100
+ for (const cb of _connectedCallbacks) cb()
101
+ }
102
+
103
+ /** Flush all useOnDisconnected callbacks (simulates component unmount). */
104
+ function triggerDisconnected(): void {
105
+ for (const cb of _disconnectedCallbacks) cb()
106
+ }
107
+
108
+ /** Simulate the user typing a value into the search input. */
109
+ function simulateType(q: string): void {
110
+ if (!_watchCallback) throw new Error('watch callback not registered — did you call useContentSearch()?')
111
+ _watchCallback(q)
112
+ }
113
+
114
+ // Convenience typed accessor
115
+ type Ref<T> = { value: T }
116
+
117
+ // ─── Tests ────────────────────────────────────────────────────────────────────
118
+
119
+ describe('useContentSearch() composable', () => {
120
+ let result: UseContentSearchReturn
121
+ let originalFetch: typeof globalThis.fetch
122
+
123
+ beforeEach(async () => {
124
+ // Fresh context object for each test so _cerSearchDebounce doesn't bleed
125
+ _currentMockContext = {}
126
+ _connectedCallbacks = []
127
+ _disconnectedCallbacks = []
128
+ _watchCallback = null
129
+
130
+ // Each test gets a fresh index singleton so loadIndex() re-fetches.
131
+ resetIndexSingleton()
132
+
133
+ originalFetch = globalThis.fetch
134
+ const indexJson = buildSearchIndex(ITEMS)
135
+ globalThis.fetch = vi.fn().mockResolvedValue({
136
+ ok: true,
137
+ text: () => Promise.resolve(indexJson),
138
+ } as unknown as Response)
139
+
140
+ // Calling useContentSearch() runs the factory, which calls watch() and
141
+ // registers useOnConnected/useOnDisconnected callbacks synchronously.
142
+ result = useContentSearch()
143
+ })
144
+
145
+ afterEach(() => {
146
+ globalThis.fetch = originalFetch
147
+ vi.useRealTimers()
148
+ })
149
+
150
+ // ─── Shape ─────────────────────────────────────────────────────────────────
151
+
152
+ it('returns query, results, and loading refs', () => {
153
+ expect(result).toHaveProperty('query')
154
+ expect(result).toHaveProperty('results')
155
+ expect(result).toHaveProperty('loading')
156
+ })
157
+
158
+ it('initialises with empty query, empty results, and loading=false', () => {
159
+ expect((result.query as Ref<string>).value).toBe('')
160
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
161
+ expect((result.loading as Ref<boolean>).value).toBe(false)
162
+ })
163
+
164
+ it('registers a watch callback during the render call (not after mount)', () => {
165
+ // The watch() is in the render body: available immediately after useContentSearch()
166
+ expect(_watchCallback).not.toBeNull()
167
+ })
168
+
169
+ it('attaches _cerSearchDebounce to the component context', () => {
170
+ expect(_currentMockContext).toHaveProperty('_cerSearchDebounce')
171
+ const state = _currentMockContext['_cerSearchDebounce'] as { seq: number; timer: unknown }
172
+ expect(state.seq).toBe(0)
173
+ expect(state.timer).toBeNull()
174
+ })
175
+
176
+ it('registers a useOnDisconnected callback for timer cleanup', () => {
177
+ expect(_disconnectedCallbacks).toHaveLength(1)
178
+ })
179
+
180
+ // ─── loading state ─────────────────────────────────────────────────────────
181
+
182
+ it('sets loading=true immediately when a non-empty query is set', () => {
183
+ vi.useFakeTimers()
184
+ simulateType('Hello')
185
+ expect((result.loading as Ref<boolean>).value).toBe(true)
186
+ })
187
+
188
+ it('keeps loading=true while the debounce timer is pending', () => {
189
+ vi.useFakeTimers()
190
+ simulateType('Hello')
191
+ vi.advanceTimersByTime(100) // 100 ms < 200 ms debounce
192
+ expect((result.loading as Ref<boolean>).value).toBe(true)
193
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
194
+ })
195
+
196
+ it('sets loading=false once results arrive after the debounce window', async () => {
197
+ vi.useFakeTimers()
198
+ simulateType('Hello')
199
+ await vi.runAllTimersAsync()
200
+ expect((result.loading as Ref<boolean>).value).toBe(false)
201
+ })
202
+
203
+ it('clears loading immediately when query is reset to empty string', () => {
204
+ vi.useFakeTimers()
205
+ simulateType('Hello')
206
+ expect((result.loading as Ref<boolean>).value).toBe(true)
207
+ simulateType('')
208
+ expect((result.loading as Ref<boolean>).value).toBe(false)
209
+ })
210
+
211
+ // ─── Debounce timing ───────────────────────────────────────────────────────
212
+
213
+ it('withholds results until 200 ms after the last keystroke', () => {
214
+ vi.useFakeTimers()
215
+ simulateType('Hello')
216
+ vi.advanceTimersByTime(199) // 1 ms before debounce fires
217
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
218
+ })
219
+
220
+ it('delivers results after the 200 ms debounce window elapses', async () => {
221
+ vi.useFakeTimers()
222
+ simulateType('Hello')
223
+ await vi.runAllTimersAsync()
224
+ expect((result.results as Ref<unknown[]>).value.length).toBeGreaterThan(0)
225
+ })
226
+
227
+ it('resets the debounce clock when a new query arrives before 200 ms', async () => {
228
+ vi.useFakeTimers()
229
+
230
+ simulateType('Getting') // timer-A starts at t=0
231
+ vi.advanceTimersByTime(100) // t=100 — timer-A still pending (100 < 200)
232
+ simulateType('Hello') // cancels timer-A, timer-B starts at t=100
233
+ vi.advanceTimersByTime(100) // t=200 — only 100 ms since timer-B started; still pending
234
+
235
+ // No results yet — timer-B hasn't fired
236
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
237
+
238
+ await vi.runAllTimersAsync() // timer-B fires at t=300; search runs with 'Hello'
239
+
240
+ const paths = (result.results as Ref<{ _path: string }[]>).value.map(r => r._path)
241
+ expect(paths).toContain('/blog/hello') // 'Hello' prefix matched Hello World
242
+ expect(paths).not.toContain('/docs/start') // 'Getting' timer was cancelled
243
+ })
244
+
245
+ it('cancels the pending timer and prevents results when query is cleared mid-debounce', async () => {
246
+ vi.useFakeTimers()
247
+ simulateType('Hello') // debounce timer starts
248
+ vi.advanceTimersByTime(100) // partway through window
249
+ simulateType('') // clears timer; loading + results reset immediately
250
+
251
+ expect((result.loading as Ref<boolean>).value).toBe(false)
252
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
253
+
254
+ await vi.runAllTimersAsync() // advance remaining time — no timer should fire
255
+ expect((result.results as Ref<unknown[]>).value).toEqual([]) // still empty
256
+ })
257
+
258
+ // ─── Disconnect cleanup ────────────────────────────────────────────────────
259
+
260
+ it('cancels the pending timer on disconnect', async () => {
261
+ vi.useFakeTimers()
262
+ simulateType('Hello')
263
+ vi.advanceTimersByTime(100) // timer is pending
264
+
265
+ triggerDisconnected()
266
+
267
+ // Timer should be cancelled — advancing past the debounce window produces no results
268
+ await vi.runAllTimersAsync()
269
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
270
+ })
271
+
272
+ it('resets loading to false on disconnect', () => {
273
+ vi.useFakeTimers()
274
+ simulateType('Hello')
275
+ expect((result.loading as Ref<boolean>).value).toBe(true)
276
+
277
+ triggerDisconnected()
278
+ expect((result.loading as Ref<boolean>).value).toBe(false)
279
+ })
280
+
281
+ // ─── Result shape and content ──────────────────────────────────────────────
282
+
283
+ it('result items include _path and title', async () => {
284
+ vi.useFakeTimers()
285
+ simulateType('Hello')
286
+ await vi.runAllTimersAsync()
287
+ const first = (result.results as Ref<Record<string, unknown>[]>).value[0]
288
+ expect(first).toHaveProperty('_path')
289
+ expect(first).toHaveProperty('title')
290
+ })
291
+
292
+ it('searching "Hello" returns Hello World and not Getting Started', async () => {
293
+ vi.useFakeTimers()
294
+ simulateType('Hello')
295
+ await vi.runAllTimersAsync()
296
+ const paths = (result.results as Ref<{ _path: string }[]>).value.map(r => r._path)
297
+ expect(paths).toContain('/blog/hello')
298
+ expect(paths).not.toContain('/docs/start')
299
+ })
300
+
301
+ it('searching "Getting" returns Getting Started and not Hello World', async () => {
302
+ vi.useFakeTimers()
303
+ simulateType('Getting')
304
+ await vi.runAllTimersAsync()
305
+ const paths = (result.results as Ref<{ _path: string }[]>).value.map(r => r._path)
306
+ expect(paths).toContain('/docs/start')
307
+ expect(paths).not.toContain('/blog/hello')
308
+ })
309
+
310
+ it('clears results immediately when query is reset from non-empty to empty', async () => {
311
+ vi.useFakeTimers()
312
+ simulateType('Hello')
313
+ await vi.runAllTimersAsync()
314
+ expect((result.results as Ref<unknown[]>).value.length).toBeGreaterThan(0)
315
+
316
+ simulateType('')
317
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
318
+ })
319
+
320
+ // ─── Pre-warm ──────────────────────────────────────────────────────────────
321
+
322
+ it('registers a useOnConnected callback for index pre-warming', () => {
323
+ expect(_connectedCallbacks).toHaveLength(1)
324
+ })
325
+
326
+ it('pre-warms the index on mount (triggers a fetch)', async () => {
327
+ triggerConnected()
328
+ // fetch is called by the pre-warm (loadIndex inside useOnConnected)
329
+ expect(globalThis.fetch).toHaveBeenCalledTimes(1)
330
+ })
331
+
332
+ // ─── Re-render stability ───────────────────────────────────────────────────
333
+ //
334
+ // The core fix: _seq and _timer live on the component context, not in local
335
+ // factory-body variables. These tests confirm that calling the factory a second
336
+ // time (simulating a component re-render) reuses the same state object rather
337
+ // than resetting it, so a timer set during one render can be cancelled by the
338
+ // new watcher registered on the next render.
339
+
340
+ it('reuses the same debounce state object when the factory runs again with the same context', () => {
341
+ // First render already called useContentSearch() in beforeEach.
342
+ const state1 = _currentMockContext['_cerSearchDebounce']
343
+ expect(state1).toBeDefined()
344
+
345
+ // Simulate re-render: reset captured callbacks, call factory again.
346
+ _connectedCallbacks = []
347
+ _disconnectedCallbacks = []
348
+ _watchCallback = null
349
+ useContentSearch()
350
+
351
+ const state2 = _currentMockContext['_cerSearchDebounce']
352
+
353
+ // Must be the identical object — not re-created.
354
+ expect(state2).toBe(state1)
355
+ })
356
+
357
+ it('a new keystroke after re-render cancels the timer that was set in the previous render', () => {
358
+ vi.useFakeTimers()
359
+
360
+ // First render (done in beforeEach). Type something → timer-A starts.
361
+ simulateType('Getting')
362
+ const state = _currentMockContext['_cerSearchDebounce'] as {
363
+ timer: ReturnType<typeof setTimeout> | null
364
+ seq: number
365
+ }
366
+ const timerA = state.timer
367
+ expect(timerA).not.toBeNull()
368
+
369
+ // Simulate re-render: new watch callback registered, same context state.
370
+ _watchCallback = null
371
+ useContentSearch()
372
+
373
+ // The new watcher fires. It reads state.timer (still timerA) and cancels it,
374
+ // then starts timer-B.
375
+ simulateType('Hello')
376
+
377
+ expect(state.timer).not.toBeNull()
378
+ expect(state.timer).not.toBe(timerA) // timer-A was replaced by timer-B
379
+ })
380
+
381
+ it('seq counter is not reset to 0 when the factory runs again (shared state)', async () => {
382
+ vi.useFakeTimers()
383
+
384
+ // First render: type something, let the debounce fire.
385
+ simulateType('Getting')
386
+ await vi.runAllTimersAsync() // timer fires → seq incremented to 1
387
+
388
+ const state = _currentMockContext['_cerSearchDebounce'] as { seq: number }
389
+ expect(state.seq).toBe(1)
390
+
391
+ // Simulate re-render: fresh callbacks, same context.
392
+ _connectedCallbacks = []
393
+ _disconnectedCallbacks = []
394
+ _watchCallback = null
395
+ useContentSearch()
396
+
397
+ // Type again on the new watcher and let it fire.
398
+ simulateType('Hello')
399
+ await vi.runAllTimersAsync() // seq incremented to 2
400
+
401
+ // If state were re-initialised on re-render, seq would be 1 again.
402
+ // Shared state means it continues from where it left off.
403
+ expect(state.seq).toBe(2)
404
+ })
405
+ })
@@ -7,11 +7,10 @@
7
7
  * - loadIndex() error path — fetch failure rejects cleanly; singleton reset allows retry
8
8
  * - loadIndex() returns a searchable MiniSearch instance
9
9
  *
10
- * Note: The full useContentSearch() composable (debounce, stale-seq guard,
11
- * useOnConnected pre-warm) requires a component context provided by the
12
- * custom-elements-runtime and is exercised by the e2e suite in content.cy.ts.
13
- * Specifically: input is debounced (300 ms) and an empty query immediately
14
- * clears results + increments the seq counter to cancel any in-flight search.
10
+ * The full useContentSearch() composable (debounce, loading state, stale-seq
11
+ * guard) is tested in use-content-search-composable.test.ts, which mocks the
12
+ * runtime to exercise the watch callback and fake-timer debounce logic directly.
13
+ * End-to-end behaviour is covered by content.cy.ts.
15
14
  */
16
15
  import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
17
16
  import MiniSearch from 'minisearch'
@@ -101,7 +100,7 @@ describe('loadIndex', () => {
101
100
  resetIndexSingleton()
102
101
 
103
102
  const index = await loadIndex() as ReturnType<typeof MiniSearch.loadJSON>
104
- const results = index.search('Hello', { prefix: true }) as Array<{ _path: string }>
103
+ const results = index.search('Hello', { prefix: true }) as unknown as Array<{ _path: string }>
105
104
  expect(results.some((r) => r._path === '/blog/hello')).toBe(true)
106
105
  })
107
106
 
@@ -138,4 +137,27 @@ describe('loadIndex', () => {
138
137
  const index = await loadIndex() as ReturnType<typeof MiniSearch.loadJSON>
139
138
  expect(index).toBeDefined()
140
139
  })
140
+
141
+ it('automatically retries after a fetch failure without manual singleton reset', async () => {
142
+ const { loadIndex, resetIndexSingleton } = await import('../../runtime/composables/use-content-search.js')
143
+ resetIndexSingleton()
144
+
145
+ // First call fails — singleton should be cleared automatically
146
+ globalThis.fetch = vi.fn().mockResolvedValue({
147
+ ok: false,
148
+ status: 503,
149
+ } as unknown as Response)
150
+ await expect(loadIndex()).rejects.toThrow()
151
+
152
+ // Second call without resetIndexSingleton — should retry and succeed
153
+ const indexJson = buildIndex()
154
+ globalThis.fetch = vi.fn().mockResolvedValue({
155
+ ok: true,
156
+ text: () => Promise.resolve(indexJson),
157
+ } as unknown as Response)
158
+ const index = await loadIndex() as ReturnType<typeof MiniSearch.loadJSON>
159
+ expect(index).toBeDefined()
160
+ // Confirm the new singleton is cached (second call reuses it)
161
+ expect(await loadIndex()).toBe(index)
162
+ })
141
163
  })
@@ -1,7 +1,9 @@
1
1
  import {
2
2
  createComposable,
3
+ getCurrentComponentContext,
3
4
  ref,
4
5
  useOnConnected,
6
+ useOnDisconnected,
5
7
  watch,
6
8
  } from '@jasonshimmy/custom-elements-runtime'
7
9
  import type { ReactiveState } from '@jasonshimmy/custom-elements-runtime'
@@ -19,11 +21,14 @@ let _indexPromise: Promise<unknown> | null = null
19
21
  * Returns the same Promise on repeated calls — the index is built at most once
20
22
  * per session regardless of how many search components are mounted.
21
23
  *
24
+ * If the fetch fails the singleton is cleared so the next search attempt
25
+ * retries automatically (no page reload required after a transient error).
26
+ *
22
27
  * @internal Exported for unit testing only.
23
28
  */
24
- export async function loadIndex(): Promise<unknown> {
29
+ export function loadIndex(): Promise<unknown> {
25
30
  if (_indexPromise) return _indexPromise
26
- _indexPromise = (async () => {
31
+ const attempt = (async () => {
27
32
  const [{ default: MiniSearch }, raw] = await Promise.all([
28
33
  import('minisearch'),
29
34
  fetch(contentSearchIndexUrl()).then((r) => {
@@ -37,7 +42,13 @@ export async function loadIndex(): Promise<unknown> {
37
42
  idField: '_path',
38
43
  })
39
44
  })()
40
- return _indexPromise
45
+ _indexPromise = attempt
46
+ // Clear the singleton on failure so the next call retries the fetch.
47
+ // The === guard ensures a newer concurrent attempt is not accidentally cleared.
48
+ attempt.catch(() => {
49
+ if (_indexPromise === attempt) _indexPromise = null
50
+ })
51
+ return attempt
41
52
  }
42
53
 
43
54
  /** Resets the module-level singleton. Used in tests only. @internal */
@@ -45,6 +56,33 @@ export function resetIndexSingleton(): void {
45
56
  _indexPromise = null
46
57
  }
47
58
 
59
+ // ─── Per-component debounce state ────────────────────────────────────────────
60
+
61
+ // Stores the debounce timer handle and stale-seq counter directly on the
62
+ // component context object (non-enumerable, same pattern the runtime uses for
63
+ // _hookCallbacks). This makes the values stable across re-renders — the context
64
+ // object is fixed for the lifetime of the element instance — without leaking
65
+ // into the reactive proxy or triggering spurious updates.
66
+
67
+ interface DebounceState {
68
+ seq: number
69
+ timer: ReturnType<typeof setTimeout> | null
70
+ }
71
+
72
+ const _STATE_KEY = '_cerSearchDebounce'
73
+
74
+ function getDebounceState(ctx: Record<string, unknown>): DebounceState {
75
+ if (!Object.prototype.hasOwnProperty.call(ctx, _STATE_KEY)) {
76
+ Object.defineProperty(ctx, _STATE_KEY, {
77
+ value: { seq: 0, timer: null } as DebounceState,
78
+ writable: false, // object ref is fixed; its properties are still mutable
79
+ enumerable: false, // invisible to the reactive proxy set-trap
80
+ configurable: false,
81
+ })
82
+ }
83
+ return (ctx as Record<string, DebounceState>)[_STATE_KEY]
84
+ }
85
+
48
86
  // ─── Composable ───────────────────────────────────────────────────────────────
49
87
 
50
88
  export interface UseContentSearchReturn {
@@ -59,24 +97,45 @@ const _factory = createComposable((): UseContentSearchReturn => {
59
97
  const results = ref<ContentSearchResult[]>([])
60
98
  const loading = ref(false)
61
99
 
62
- // Pre-warm index on mount
100
+ // Debounce state lives on the component context, not in local render-body variables.
101
+ // Local variables are re-created on every re-render; the context object is stable
102
+ // for the lifetime of the element. Storing state here lets the render-body
103
+ // watch() (see below) pick up the same timer and sequence counter across re-renders
104
+ // without the watcher accumulation that occurs when watch() is placed inside
105
+ // useOnConnected() (which runs once per mount but is not registered for cleanup
106
+ // by the reactive system, leaking watchers on every disconnect + reconnect cycle).
107
+ const state = getDebounceState(getCurrentComponentContext()! as Record<string, unknown>)
108
+
109
+ // Pre-warm the index on first mount so the first real search is faster.
63
110
  useOnConnected(() => {
64
111
  loadIndex().catch(() => {/* silently ignore pre-warm errors */})
65
112
  })
66
113
 
67
- // Monotonic counter to discard stale async results
68
- let _seq = 0
69
- let _debounceTimer: ReturnType<typeof setTimeout> | null = null
114
+ // Cancel any in-flight debounce on unmount so stale async work doesn't land
115
+ // after the component is gone.
116
+ useOnDisconnected(() => {
117
+ if (state.timer !== null) {
118
+ clearTimeout(state.timer)
119
+ state.timer = null
120
+ }
121
+ state.seq++ // discard any in-flight async search
122
+ loading.value = false
123
+ })
70
124
 
125
+ // watch() is in the render body so the reactive system registers it under the
126
+ // current component and tears it down automatically on re-render and disconnect.
127
+ // The mutable state (seq / timer) lives on the context (above) and persists
128
+ // across re-renders — new watcher instances see the same timer and counter,
129
+ // which is what makes debounce cancellation correct even after a re-render.
71
130
  watch(query, (q: string) => {
72
- if (_debounceTimer !== null) {
73
- clearTimeout(_debounceTimer)
74
- _debounceTimer = null
131
+ if (state.timer !== null) {
132
+ clearTimeout(state.timer)
133
+ state.timer = null
75
134
  }
76
135
 
77
136
  if (!q) {
78
137
  // Increment seq so any in-flight async search is discarded when it resolves
79
- _seq++
138
+ state.seq++
80
139
  loading.value = false
81
140
  results.value = []
82
141
  return
@@ -85,21 +144,21 @@ const _factory = createComposable((): UseContentSearchReturn => {
85
144
  // Signal loading immediately so the UI can respond before the debounce fires
86
145
  loading.value = true
87
146
 
88
- _debounceTimer = setTimeout(async () => {
89
- _debounceTimer = null
90
- const seq = ++_seq
147
+ state.timer = setTimeout(async () => {
148
+ state.timer = null
149
+ const seq = ++state.seq
91
150
 
92
151
  try {
93
152
  const index = await loadIndex() as { search(q: string, opts?: { prefix?: boolean }): ContentSearchResult[] }
94
- if (seq !== _seq) return // stale — a newer query is in flight
153
+ if (seq !== state.seq) return // stale — a newer query is in flight
95
154
  results.value = index.search(q, { prefix: true }) as ContentSearchResult[]
96
155
  } catch {
97
- if (seq !== _seq) return
156
+ if (seq !== state.seq) return
98
157
  results.value = []
99
158
  } finally {
100
159
  // Only clear loading for the most recent search; a newer in-flight search
101
160
  // keeps loading=true until it settles.
102
- if (seq === _seq) loading.value = false
161
+ if (seq === state.seq) loading.value = false
103
162
  }
104
163
  }, 200)
105
164
  })