Skip to content

Commit 8486bbc

Browse files
authored
refactor(core): unify fff runtime adapters (anomalyco#41827)
1 parent 2621dde commit 8486bbc

3 files changed

Lines changed: 79 additions & 188 deletions

File tree

Lines changed: 7 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,15 @@
1-
import {
2-
FileFinder,
3-
type DirItem,
4-
type DirSearchResult,
5-
type FileItem,
6-
type InitOptions,
7-
type MixedItem,
8-
type MixedSearchResult,
9-
type SearchResult,
10-
} from "@ff-labs/fff-bun"
1+
import { FileFinder } from "@ff-labs/fff-bun"
2+
import { bind } from "./fff"
3+
4+
export type { Directory, DirSearch, File, Init, Mixed, MixedSearch, Picker, Result, Search } from "./fff"
115

126
declare global {
137
const FFF_LIBC: "gnu" | "musl"
148
}
159

16-
export type Result<T> = { ok: true; value: T } | { ok: false; error: string }
17-
18-
export type Init = InitOptions
19-
20-
export interface Search {
21-
items: FileItem[]
22-
scores: SearchResult["scores"]
23-
totalMatched: number
24-
totalFiles: number
25-
}
10+
const adapter = bind(FileFinder)
2611

27-
export interface DirSearch {
28-
items: DirItem[]
29-
scores: DirSearchResult["scores"]
30-
totalMatched: number
31-
totalDirs: number
32-
}
33-
34-
export interface MixedSearch {
35-
items: MixedItem[]
36-
scores: MixedSearchResult["scores"]
37-
totalMatched: number
38-
totalFiles: number
39-
totalDirs: number
40-
}
41-
42-
export type File = FileItem
43-
export type Directory = DirItem
44-
export type Mixed = MixedItem
45-
export interface Picker {
46-
destroy(): void
47-
isScanning(): boolean
48-
waitForScan(timeoutMs?: number): Promise<Result<boolean>>
49-
refreshGitStatus(): Result<number>
50-
fileSearch(
51-
query: string,
52-
opts?: {
53-
currentFile?: string
54-
pageIndex?: number
55-
pageSize?: number
56-
},
57-
): Result<Search>
58-
directorySearch(
59-
query: string,
60-
opts?: {
61-
currentFile?: string
62-
pageIndex?: number
63-
pageSize?: number
64-
},
65-
): Result<DirSearch>
66-
mixedSearch(
67-
query: string,
68-
opts?: {
69-
currentFile?: string
70-
pageIndex?: number
71-
pageSize?: number
72-
},
73-
): Result<MixedSearch>
74-
trackQuery(query: string, file: string): Result<boolean>
75-
getHistoricalQuery(offset: number): Result<string | null>
76-
}
77-
78-
export function available() {
79-
return FileFinder.isAvailable()
80-
}
81-
82-
export function create(opts: Init): Result<Picker> {
83-
const made = FileFinder.create(opts)
84-
if (!made.ok) return made
85-
const pick = made.value
86-
return {
87-
ok: true,
88-
value: {
89-
destroy: () => pick.destroy(),
90-
isScanning: () => pick.isScanning(),
91-
waitForScan: (timeoutMs) => pick.waitForScan(timeoutMs),
92-
refreshGitStatus: () => pick.refreshGitStatus(),
93-
fileSearch: (query, next) => pick.fileSearch(query, next),
94-
directorySearch: (query, next) => pick.directorySearch(query, next),
95-
mixedSearch: (query, next) => pick.mixedSearch(query, next),
96-
trackQuery: (query, file) => pick.trackQuery(query, file),
97-
getHistoricalQuery: (offset) => pick.getHistoricalQuery(offset),
98-
},
99-
}
100-
}
12+
export const available = adapter.available
13+
export const create = adapter.create
10114

10215
export * as Fff from "./fff.bun"
Lines changed: 6 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,12 @@
1-
import type {
2-
DirItem,
3-
DirSearchResult,
4-
FileItem,
5-
InitOptions,
6-
MixedItem,
7-
MixedSearchResult,
8-
SearchResult,
9-
} from "@ff-labs/fff-node"
1+
import { bind } from "./fff"
102

11-
const { FileFinder } = await import("@ff-labs/fff-node").catch(() => ({ FileFinder: undefined }))
12-
13-
export type Result<T> = { ok: true; value: T } | { ok: false; error: string }
14-
15-
export type Init = InitOptions
16-
17-
export interface Search {
18-
items: FileItem[]
19-
scores: SearchResult["scores"]
20-
totalMatched: number
21-
totalFiles: number
22-
}
3+
export type { Directory, DirSearch, File, Init, Mixed, MixedSearch, Picker, Result, Search } from "./fff"
234

24-
export interface DirSearch {
25-
items: DirItem[]
26-
scores: DirSearchResult["scores"]
27-
totalMatched: number
28-
totalDirs: number
29-
}
30-
31-
export interface MixedSearch {
32-
items: MixedItem[]
33-
scores: MixedSearchResult["scores"]
34-
totalMatched: number
35-
totalFiles: number
36-
totalDirs: number
37-
}
38-
39-
export type File = FileItem
40-
export type Directory = DirItem
41-
export type Mixed = MixedItem
42-
export interface Picker {
43-
destroy(): void
44-
isScanning(): boolean
45-
waitForScan(timeoutMs?: number): Promise<Result<boolean>>
46-
refreshGitStatus(): Result<number>
47-
fileSearch(
48-
query: string,
49-
opts?: {
50-
currentFile?: string
51-
pageIndex?: number
52-
pageSize?: number
53-
},
54-
): Result<Search>
55-
directorySearch(
56-
query: string,
57-
opts?: {
58-
currentFile?: string
59-
pageIndex?: number
60-
pageSize?: number
61-
},
62-
): Result<DirSearch>
63-
mixedSearch(
64-
query: string,
65-
opts?: {
66-
currentFile?: string
67-
pageIndex?: number
68-
pageSize?: number
69-
},
70-
): Result<MixedSearch>
71-
trackQuery(query: string, file: string): Result<boolean>
72-
getHistoricalQuery(offset: number): Result<string | null>
73-
}
5+
const { FileFinder } = await import("@ff-labs/fff-node").catch(() => ({ FileFinder: undefined }))
746

75-
export function available() {
76-
return FileFinder?.isAvailable() ?? false
77-
}
7+
const adapter = bind(FileFinder, "fff unavailable on node runtime")
788

79-
export function create(opts: Init): Result<Picker> {
80-
if (!FileFinder) return { ok: false, error: "fff unavailable on node runtime" }
81-
const made = FileFinder.create(opts)
82-
if (!made.ok) return made
83-
const pick = made.value
84-
return {
85-
ok: true,
86-
value: {
87-
destroy: () => pick.destroy(),
88-
isScanning: () => pick.isScanning(),
89-
waitForScan: (timeoutMs) => pick.waitForScan(timeoutMs),
90-
refreshGitStatus: () => pick.refreshGitStatus(),
91-
fileSearch: (query, next) => pick.fileSearch(query, next),
92-
directorySearch: (query, next) => pick.directorySearch(query, next),
93-
mixedSearch: (query, next) => pick.mixedSearch(query, next),
94-
trackQuery: (query, file) => pick.trackQuery(query, file),
95-
getHistoricalQuery: (offset) => pick.getHistoricalQuery(offset),
96-
},
97-
}
98-
}
9+
export const available = adapter.available
10+
export const create = adapter.create
9911

10012
export * as Fff from "./fff.node"
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
export type Result<T> = { ok: true; value: T } | { ok: false; error: string }
2+
3+
export interface Init {
4+
basePath: string
5+
aiMode?: boolean
6+
disableMmapCache?: boolean
7+
disableContentIndexing?: boolean
8+
}
9+
10+
export interface SearchOptions {
11+
currentFile?: string
12+
pageIndex?: number
13+
pageSize?: number
14+
}
15+
16+
export interface File {
17+
relativePath: string
18+
}
19+
20+
export interface Directory {
21+
relativePath: string
22+
}
23+
24+
export type Mixed = { type: "file"; item: File } | { type: "directory"; item: Directory }
25+
26+
export interface Score {
27+
total: number
28+
}
29+
30+
export interface Search {
31+
items: File[]
32+
scores: Score[]
33+
}
34+
35+
export interface DirSearch {
36+
items: Directory[]
37+
scores: Score[]
38+
}
39+
40+
export interface MixedSearch {
41+
items: Mixed[]
42+
scores: Score[]
43+
}
44+
45+
export interface Picker {
46+
destroy(): void
47+
fileSearch(query: string, options?: SearchOptions): Result<Search>
48+
directorySearch(query: string, options?: SearchOptions): Result<DirSearch>
49+
mixedSearch(query: string, options?: SearchOptions): Result<MixedSearch>
50+
}
51+
52+
export interface Backend {
53+
isAvailable(): boolean
54+
create(options: Init): Result<Picker>
55+
}
56+
57+
export function bind(backend: Backend | undefined, unavailable = "fff unavailable") {
58+
return {
59+
available: () => backend?.isAvailable() ?? false,
60+
create: (options: Init): Result<Picker> =>
61+
backend?.create(options) ?? {
62+
ok: false,
63+
error: unavailable,
64+
},
65+
}
66+
}

0 commit comments

Comments
 (0)