-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { defineDriver } from './utils' | ||
import type { Driver, StorageValue } from '../types' | ||
import { normalizeKey } from './utils' | ||
|
||
export interface OverlayStorageOptions { | ||
layers: Driver[] | ||
} | ||
|
||
const OVERLAY_REMOVED = '__OVERLAY_REMOVED__' | ||
|
||
export default defineDriver((options: OverlayStorageOptions) => { | ||
return { | ||
async hasItem (key) { | ||
for (const layer of options.layers) { | ||
if (await layer.hasItem(key)) { | ||
if (layer === options.layers[0]) { | ||
if (await options.layers[0]?.getItem(key) === OVERLAY_REMOVED) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
} | ||
return false | ||
}, | ||
async getItem (key) { | ||
for (const layer of options.layers) { | ||
const value = await layer.getItem(key) | ||
if (value === OVERLAY_REMOVED) { | ||
return null | ||
} | ||
if (value !== null) { | ||
return value | ||
} | ||
} | ||
return null | ||
}, | ||
// TODO: Support native meta | ||
// async getMeta (key) {}, | ||
async setItem(key, value) { | ||
await options.layers[0]?.setItem(key, value) | ||
}, | ||
async removeItem (key) { | ||
await options.layers[0]?.setItem(key, OVERLAY_REMOVED) | ||
}, | ||
async getKeys(base) { | ||
const allKeys = await Promise.all(options.layers.map(async layer => { | ||
const keys = await layer.getKeys(base) | ||
return keys.map(key => normalizeKey(key)) | ||
})) | ||
const uniqueKeys = Array.from(new Set(allKeys.flat())) | ||
const existingKeys = await Promise.all(uniqueKeys.map(async key => { | ||
if (await options.layers[0]?.getItem(key) === OVERLAY_REMOVED) { | ||
return false | ||
} | ||
return key | ||
})) | ||
return existingKeys.filter(Boolean) as string[] | ||
}, | ||
async dispose() { | ||
// TODO: Graceful error handling | ||
await Promise.all(options.layers.map(async layer => { | ||
if (layer.dispose) { | ||
await layer.dispose() | ||
} | ||
})) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { describe } from 'vitest' | ||
import driver from '../../src/drivers/overlay' | ||
import memory from '../../src/drivers/memory' | ||
import { testDriver } from './utils' | ||
|
||
describe('drivers: overlay', () => { | ||
const [s1, s2] = [memory(), memory()] | ||
testDriver({ | ||
driver: driver({ | ||
layers: [s1, s2] | ||
}) | ||
}) | ||
}) |