| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
//----------------------------------------------------------------------------- |
| 4 |
// Global |
| 5 |
//----------------------------------------------------------------------------- |
| 6 |
|
| 7 |
const { |
| 8 |
Object: { keys: $keys }, |
| 9 |
StorageEvent, |
| 10 |
String, |
| 11 |
} = globalThis; |
| 12 |
|
| 13 |
//----------------------------------------------------------------------------- |
| 14 |
// Exports |
| 15 |
//----------------------------------------------------------------------------- |
| 16 |
|
| 17 |
export class MockStorage { |
| 18 |
get length() { |
| 19 |
return $keys(this).length; |
| 20 |
} |
| 21 |
|
| 22 |
/** @type {typeof Storage.prototype.clear} */ |
| 23 |
clear() { |
| 24 |
for (const key in this) { |
| 25 |
delete this[key]; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
/** @type {typeof Storage.prototype.getItem} */ |
| 30 |
getItem(key) { |
| 31 |
key = String(key); |
| 32 |
return this[key] ?? null; |
| 33 |
} |
| 34 |
|
| 35 |
/** @type {typeof Storage.prototype.key} */ |
| 36 |
key(index) { |
| 37 |
return $keys(this).at(index); |
| 38 |
} |
| 39 |
|
| 40 |
/** @type {typeof Storage.prototype.removeItem} */ |
| 41 |
removeItem(key) { |
| 42 |
key = String(key); |
| 43 |
delete this[key]; |
| 44 |
window.dispatchEvent(new StorageEvent("storage", { key, newValue: null })); |
| 45 |
} |
| 46 |
|
| 47 |
/** @type {typeof Storage.prototype.setItem} */ |
| 48 |
setItem(key, value) { |
| 49 |
key = String(key); |
| 50 |
value = String(value); |
| 51 |
this[key] = value; |
| 52 |
window.dispatchEvent(new StorageEvent("storage", { key, newValue: value })); |
| 53 |
} |
| 54 |
} |