Skip to content

Commit

Permalink
fix(setItems): call driver native setItems only to avoid duplicate …
Browse files Browse the repository at this point in the history
…write (#392)
  • Loading branch information
juliusmarminge committed Mar 14, 2024
1 parent d8316dc commit 85bdbb7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/storage.ts
Expand Up @@ -231,7 +231,7 @@ export function createStorage<T extends StorageValue>(
async setItems(items, commonOptions) {
await runBatch(items, commonOptions, async (batch) => {
if (batch.driver.setItems) {
await asyncCall(
return asyncCall(
batch.driver.setItems,
batch.items.map((item) => ({
key: item.relativeKey,
Expand Down
30 changes: 30 additions & 0 deletions test/storage.test.ts
Expand Up @@ -160,3 +160,33 @@ describe("utils", () => {
expect(async () => await storage.setItem("foo", [])).not.toThrow();
});
});

describe("Regression", () => {
it("setItems doeesn't upload twice", async () => {
/**
* https://github.com/unjs/unstorage/pull/392
*/

const setItem = vi.fn();
const setItems = vi.fn();

const driver = memory();
const storage = createStorage({
driver: {
...driver,
setItem: (...args) => {
setItem(...args);
return driver.setItem?.(...args);
},
setItems: (...args) => {
setItems(...args);
return driver.setItems?.(...args);
},
},
});

await storage.setItems([{ key: "foo.txt", value: "bar" }]);
expect(setItem).toHaveBeenCalledTimes(0);
expect(setItems).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 85bdbb7

Please sign in to comment.