Skip to content

Commit f95fb61

Browse files
committed
refactor: extract save accounts entry wrapper
1 parent 95cca7b commit f95fb61

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

lib/storage.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
importAccountsSnapshot,
1919
} from "./storage/account-port.js";
2020
import { saveAccountsToDisk } from "./storage/account-save.js";
21+
import { saveAccountsEntry } from "./storage/account-save-entry.js";
2122
import { buildBackupMetadata } from "./storage/backup-metadata-builder.js";
2223
import {
2324
ACCOUNTS_BACKUP_SUFFIX,
@@ -1836,8 +1837,10 @@ export async function withAccountAndFlaggedStorageTransaction<T>(
18361837
* @throws StorageError with platform-aware hints on failure
18371838
*/
18381839
export async function saveAccounts(storage: AccountStorageV3): Promise<void> {
1839-
return withStorageLock(async () => {
1840-
await saveAccountsUnlocked(storage);
1840+
return saveAccountsEntry({
1841+
storage,
1842+
withStorageLock,
1843+
saveUnlocked: saveAccountsUnlocked,
18411844
});
18421845
}
18431846

lib/storage/account-save-entry.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { AccountStorageV3 } from "../storage.js";
2+
3+
export async function saveAccountsEntry(params: {
4+
storage: AccountStorageV3;
5+
withStorageLock: <T>(fn: () => Promise<T>) => Promise<T>;
6+
saveUnlocked: (storage: AccountStorageV3) => Promise<void>;
7+
}): Promise<void> {
8+
return params.withStorageLock(async () => {
9+
await params.saveUnlocked(params.storage);
10+
});
11+
}

test/account-save-entry.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { saveAccountsEntry } from "../lib/storage/account-save-entry.js";
3+
4+
describe("account save entry", () => {
5+
it("delegates save through the storage lock", async () => {
6+
const saveUnlocked = vi.fn(async () => undefined);
7+
await saveAccountsEntry({
8+
storage: {
9+
version: 3,
10+
accounts: [],
11+
activeIndex: 0,
12+
activeIndexByFamily: {},
13+
},
14+
withStorageLock: async (fn) => fn(),
15+
saveUnlocked,
16+
});
17+
18+
expect(saveUnlocked).toHaveBeenCalledWith({
19+
version: 3,
20+
accounts: [],
21+
activeIndex: 0,
22+
activeIndexByFamily: {},
23+
});
24+
});
25+
});

0 commit comments

Comments
 (0)