refactor(desktop): organize lib modules by domain

This commit is contained in:
t8y2 committed 2026-07-04 13:53:52 +08:00
1 parent 90f0594e93
commit 7e7c4944e9
700 files changed
+1292 -1264

No files matched your search

@@ -0,0 +1,11 @@
import type { DatabaseType } from "@/types/database";
const LOCAL_ONLY_EDITOR_COMPLETION_METADATA_TYPES = new Set<DatabaseType>(["prestosql", "trino"]);
export function usesLocalOnlyEditorCompletionMetadata(databaseType?: DatabaseType): boolean {
return !!databaseType && LOCAL_ONLY_EDITOR_COMPLETION_METADATA_TYPES.has(databaseType);
}
export function usesOnDemandOnlyEditorColumnMetadata(databaseType?: DatabaseType): boolean {
return !!databaseType && LOCAL_ONLY_EDITOR_COMPLETION_METADATA_TYPES.has(databaseType);
}
@@ -0,0 +1,52 @@
import type { TreeNode } from "@/types/database";
import type { SqlCompletionTable } from "@/lib/sql/sqlCompletion";
const TABLE_NODE_TYPES = new Set(["table", "view", "materialized_view"]);
export function completionSchemasFromTree(nodes: readonly TreeNode[], connectionId: string, database: string): string[] {
const seen = new Set<string>();
const schemas: string[] = [];
visitTree(nodes, (node) => {
if (node.connectionId !== connectionId || node.database !== database || node.type !== "schema" || !node.schema) return;
const key = node.schema.toLowerCase();
if (seen.has(key)) return;
seen.add(key);
schemas.push(node.schema);
});
return schemas;
}
export function completionTablesFromTree(nodes: readonly TreeNode[], connectionId: string, database: string, schema?: string): SqlCompletionTable[] {
const preferredSchema = schema?.toLowerCase();
const tables: SqlCompletionTable[] = [];
visitTree(nodes, (node) => {
if (node.connectionId !== connectionId || node.database !== database || !TABLE_NODE_TYPES.has(node.type)) return;
if (preferredSchema && node.schema?.toLowerCase() !== preferredSchema) return;
tables.push({
name: node.tableName || node.label,
schema: node.schema,
type: node.type === "view" || node.type === "materialized_view" ? "view" : "table",
});
});
return dedupeCompletionTables(tables);
}
function visitTree(nodes: readonly TreeNode[], visit: (node: TreeNode) => void) {
for (const node of nodes) {
visit(node);
if (node.children?.length) visitTree(node.children, visit);
if (node.hiddenChildren?.length) visitTree(node.hiddenChildren, visit);
}
}
function dedupeCompletionTables(tables: SqlCompletionTable[]): SqlCompletionTable[] {
const seen = new Set<string>();
const result: SqlCompletionTable[] = [];
for (const table of tables) {
const key = `${table.schema ?? ""}.${table.name}`.toLowerCase();
if (seen.has(key)) continue;
seen.add(key);
result.push(table);
}
return result;
}
@@ -0,0 +1,70 @@
export interface MetadataTaskLimiterEvent {
scope: string;
kind: string;
active: number;
queued: number;
}
type MetadataTaskLimiterLogger = (event: MetadataTaskLimiterEvent & { event: "start" | "queued" | "done" }) => void;
interface QueuedTask<T> {
kind: string;
run: () => Promise<T>;
resolve: (value: T) => void;
reject: (reason?: unknown) => void;
}
interface ScopeState {
active: number;
queue: QueuedTask<unknown>[];
}
export class MetadataTaskLimiter {
private readonly scopes = new Map<string, ScopeState>();
constructor(
private readonly maxActive = 2,
private readonly logger?: MetadataTaskLimiterLogger,
) {}
run<T>(scope: string, kind: string, task: () => Promise<T>): Promise<T> {
const normalizedScope = scope || "global";
const state = this.stateForScope(normalizedScope);
return new Promise<T>((resolve, reject) => {
const queued: QueuedTask<T> = { kind, run: task, resolve, reject };
if (state.active < this.maxActive) {
this.start(normalizedScope, state, queued);
return;
}
state.queue.push(queued as QueuedTask<unknown>);
this.logger?.({ event: "queued", scope: normalizedScope, kind, active: state.active, queued: state.queue.length });
});
}
private stateForScope(scope: string): ScopeState {
let state = this.scopes.get(scope);
if (!state) {
state = { active: 0, queue: [] };
this.scopes.set(scope, state);
}
return state;
}
private start<T>(scope: string, state: ScopeState, task: QueuedTask<T>) {
state.active++;
this.logger?.({ event: "start", scope, kind: task.kind, active: state.active, queued: state.queue.length });
task
.run()
.then(task.resolve, task.reject)
.finally(() => {
state.active--;
this.logger?.({ event: "done", scope, kind: task.kind, active: state.active, queued: state.queue.length });
const next = state.queue.shift();
if (next) {
this.start(scope, state, next);
} else if (state.active === 0) {
this.scopes.delete(scope);
}
});
}
}
@@ -0,0 +1,41 @@
export const SCHEMA_TREE_CACHE_TTL_MS = 15 * 60 * 1000;
export interface SchemaTreeCacheEnvelope<T> {
version: 2;
cachedAt: string;
children: T;
}
export interface DecodedSchemaTreeCache<T> {
children: T;
isStale: boolean;
}
export function encodeSchemaTreeCache<T>(children: T, nowMs = Date.now()): SchemaTreeCacheEnvelope<T> {
return {
version: 2,
cachedAt: new Date(nowMs).toISOString(),
children,
};
}
export function decodeSchemaTreeCache<T>(payload: unknown, nowMs = Date.now(), ttlMs = SCHEMA_TREE_CACHE_TTL_MS): DecodedSchemaTreeCache<T> | null {
if (Array.isArray(payload)) {
return { children: payload as T, isStale: true };
}
if (!payload || typeof payload !== "object") return null;
const envelope = payload as Partial<SchemaTreeCacheEnvelope<unknown>>;
if (envelope.version !== 2 || !Array.isArray(envelope.children) || typeof envelope.cachedAt !== "string") {
return null;
}
const cachedAtMs = Date.parse(envelope.cachedAt);
if (!Number.isFinite(cachedAtMs)) return null;
return {
children: envelope.children as T,
isStale: nowMs - cachedAtMs >= ttlMs,
};
}