forked from unjs/unstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
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
3 changed files
with
35 additions
and
117 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
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 |
---|---|---|
@@ -1,77 +1,16 @@ | ||
import { createRequiredError, defineDriver } from "./utils"; | ||
import { defineDriver } from "./utils"; | ||
import localstorage, { type LocalStorageOptions } from "./localstorage"; | ||
|
||
export interface SessionStorageOptions { | ||
base?: string; | ||
window?: typeof window; | ||
sessionStorage?: typeof window.sessionStorage; | ||
} | ||
export interface SessionStorageOptions extends LocalStorageOptions {} | ||
|
||
const DRIVER_NAME = "session-storage"; | ||
|
||
export default defineDriver((opts: SessionStorageOptions = {}) => { | ||
if (!opts.window) { | ||
opts.window = typeof window === "undefined" ? undefined : window; | ||
} | ||
if (!opts.sessionStorage) { | ||
opts.sessionStorage = opts.window?.sessionStorage; | ||
} | ||
if (!opts.sessionStorage) { | ||
throw createRequiredError(DRIVER_NAME, "sessionStorage"); | ||
} | ||
|
||
const r = (key: string) => (opts.base ? opts.base + ":" : "") + key; | ||
|
||
let _storageListener: undefined | ((ev: StorageEvent) => void); | ||
const _unwatch = () => { | ||
if (_storageListener) { | ||
opts.window!.removeEventListener("storage", _storageListener); | ||
} | ||
_storageListener = undefined; | ||
}; | ||
|
||
return { | ||
name: DRIVER_NAME, | ||
options: opts, | ||
getInstance: () => opts.sessionStorage!, | ||
hasItem(key) { | ||
return Object.prototype.hasOwnProperty.call(opts.sessionStorage, r(key)); | ||
}, | ||
getItem(key) { | ||
return opts.sessionStorage!.getItem(r(key)); | ||
}, | ||
setItem(key, value) { | ||
return opts.sessionStorage!.setItem(r(key), value); | ||
}, | ||
removeItem(key) { | ||
return opts.sessionStorage!.removeItem(r(key)); | ||
}, | ||
getKeys() { | ||
return Object.keys(opts.sessionStorage!); | ||
}, | ||
clear() { | ||
if (opts.base) { | ||
for (const key of Object.keys(opts.sessionStorage!)) { | ||
opts.sessionStorage?.removeItem(key); | ||
} | ||
} else { | ||
opts.sessionStorage!.clear(); | ||
} | ||
if (opts.window && _storageListener) { | ||
opts.window.removeEventListener("storage", _storageListener); | ||
} | ||
}, | ||
watch(callback) { | ||
if (!opts.window) { | ||
return _unwatch; | ||
} | ||
_storageListener = ({ key, newValue }: StorageEvent) => { | ||
if (key) { | ||
callback(newValue ? "update" : "remove", key); | ||
} | ||
}; | ||
opts.window!.addEventListener("storage", _storageListener); | ||
|
||
return _unwatch; | ||
}, | ||
...localstorage({ | ||
windowKey: "sessionStorage", | ||
...opts, | ||
}), | ||
}; | ||
}); |