-
Notifications
You must be signed in to change notification settings - Fork 135
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
13 changed files
with
315 additions
and
130 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
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,33 +1,54 @@ | ||
import type { DriverFactory } from '../types' | ||
|
||
export interface LocalStorageOptions { | ||
window?: typeof window | ||
localStorage?: typeof window.localStorage | ||
} | ||
|
||
export default <DriverFactory> function (opts?: LocalStorageOptions) { | ||
const _localStorage = opts?.localStorage || (globalThis.localStorage as typeof window.localStorage) | ||
if (!_localStorage) { | ||
export default <DriverFactory>function (opts: LocalStorageOptions = {}) { | ||
if (!opts.window) { | ||
opts.window = typeof window !== 'undefined' ? window : undefined | ||
} | ||
if (!opts.localStorage) { | ||
opts.localStorage = opts.window?.localStorage | ||
} | ||
if (!opts.localStorage) { | ||
throw new Error('localStorage not available') | ||
} | ||
|
||
let _storageListener: (ev: StorageEvent) => void | ||
|
||
return { | ||
hasItem (key) { | ||
return Object.prototype.hasOwnProperty.call(_localStorage, key) | ||
return Object.prototype.hasOwnProperty.call(opts.localStorage!, key) | ||
}, | ||
getItem (key) { | ||
return _localStorage.getItem(key) | ||
return opts.localStorage!.getItem(key) | ||
}, | ||
setItem (key, value) { | ||
return _localStorage.setItem(key, value) | ||
return opts.localStorage!.setItem(key, value) | ||
}, | ||
removeItem (key) { | ||
return _localStorage.removeItem(key) | ||
return opts.localStorage!.removeItem(key) | ||
}, | ||
getKeys () { | ||
return Object.keys(_localStorage) | ||
return Object.keys(opts.localStorage!) | ||
}, | ||
clear () { | ||
_localStorage.clear() | ||
opts.localStorage!.clear() | ||
if (opts.window && _storageListener) { | ||
opts.window.removeEventListener('storage', _storageListener) | ||
} | ||
}, | ||
watch(callback) { | ||
if (opts.window) { | ||
_storageListener = (ev: StorageEvent) => { | ||
if (ev.key) { | ||
callback(ev.newValue ? 'update' : 'remove', ev.key) | ||
} | ||
} | ||
opts.window.addEventListener('storage', _storageListener) | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.