refactor(data): move Result utility to utils and simplify API

Relocate the Result type from `@/data/result` to `@/utils/result` and
replace the discriminated-union API (`kind`/`value`) with a simpler
boolean-based API (`success`/`data`) across all repositories and
storage classes for improved readability and consistency.
This commit is contained in:
2026-06-09 10:34:40 +08:00
parent c767322db6
commit 904cb3638a
16 changed files with 81 additions and 122 deletions
+21 -21
View File
@@ -15,7 +15,7 @@
* 数据量大时考虑升级 schema 加索引。
*/
import { Result, type Result as ResultT } from "../result";
import { Result, type Result as ResultT } from "@/utils/result";
import { LocalChatDB } from "./local_chat_db";
import { LocalMessage } from "./local_message";
@@ -47,9 +47,9 @@ export class LocalChatStorage {
async init(): Promise<ResultT<void>> {
try {
await this.db.open();
return Result.success(undefined);
return Result.ok(undefined);
} catch (e) {
return Result.failure(e);
return Result.err(e);
}
}
@@ -58,9 +58,9 @@ export class LocalChatStorage {
async saveMessage(message: LocalMessage): Promise<ResultT<void>> {
try {
await this.db.messages.add(message.toRow());
return Result.success(undefined);
return Result.ok(undefined);
} catch (e) {
return Result.failure(e);
return Result.err(e);
}
}
@@ -75,9 +75,9 @@ export class LocalChatStorage {
}
},
);
return Result.success(undefined);
return Result.ok(undefined);
} catch (e) {
return Result.failure(e);
return Result.err(e);
}
}
@@ -88,17 +88,17 @@ export class LocalChatStorage {
const rows = await this.db.messages.toArray();
// 按 dbId 升序(与 Dart `box.values.toList()` 插入序语义一致)
rows.sort((a, b) => (a.dbId ?? 0) - (b.dbId ?? 0));
return Result.success(rows.map((r) => LocalMessage.fromRow(r)));
return Result.ok(rows.map((r) => LocalMessage.fromRow(r)));
} catch (e) {
return Result.failure(e);
return Result.err(e);
}
}
async getMessageCount(): Promise<ResultT<number>> {
try {
return Result.success(await this.db.messages.count());
return Result.ok(await this.db.messages.count());
} catch (e) {
return Result.failure(e);
return Result.err(e);
}
}
@@ -110,9 +110,9 @@ export class LocalChatStorage {
.filter((r) => r.sessionId === sessionId)
.toArray();
rows.sort((a, b) => (a.dbId ?? 0) - (b.dbId ?? 0));
return Result.success(rows.map((r) => LocalMessage.fromRow(r)));
return Result.ok(rows.map((r) => LocalMessage.fromRow(r)));
} catch (e) {
return Result.failure(e);
return Result.err(e);
}
}
@@ -121,9 +121,9 @@ export class LocalChatStorage {
async clearAll(): Promise<ResultT<void>> {
try {
await this.db.messages.clear();
return Result.success(undefined);
return Result.ok(undefined);
} catch (e) {
return Result.failure(e);
return Result.err(e);
}
}
@@ -134,30 +134,30 @@ export class LocalChatStorage {
async deleteMessage(index: number): Promise<ResultT<void>> {
try {
if (!Number.isInteger(index) || index < 0) {
return Result.failure(
return Result.err(
new RangeError(`deleteMessage: index ${index} out of range`),
);
}
const rows = await this.db.messages.toArray();
if (index >= rows.length) {
return Result.failure(
return Result.err(
new RangeError(`deleteMessage: index ${index} out of range`),
);
}
const target = rows[index]!;
await this.db.messages.delete(target.dbId!);
return Result.success(undefined);
return Result.ok(undefined);
} catch (e) {
return Result.failure(e);
return Result.err(e);
}
}
async close(): Promise<ResultT<void>> {
try {
this.db.close();
return Result.success(undefined);
return Result.ok(undefined);
} catch (e) {
return Result.failure(e);
return Result.err(e);
}
}
}