1
1
/**
2
2
* An implementation of @see smuggler-api.StorageApi that persists the data
3
- * on user device and is based on browser extension APIs .
3
+ * in a storage only available to browser extensions (most likely - locally on device) .
4
4
*
5
5
* It is intended to work in all browser extension contexts (such as background,
6
- * content etc). See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage
6
+ * content etc).
7
+ * See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage
7
8
* for more information.
9
+ *
10
+ * NOTE: At the time of this writing dev tools of some browsers (including the
11
+ * Chromium-based ones) do not including anything to inspect the contents
12
+ * of the underlying storage. See https://stackoverflow.com/a/27432365/3375765
13
+ * for workarounds.
8
14
*/
9
15
10
16
import {
@@ -117,6 +123,10 @@ class YekLavStore {
117
123
this . store = store
118
124
}
119
125
126
+ // TODO[snikitin@outlook .com] Add a note that the code that uses this
127
+ // method should try to combine everything it needs to do into ONE
128
+ // call to set() in hopes to retain at least some data consistency
129
+ // guarantees.
120
130
set ( items : YekLav [ ] ) : Promise < void > {
121
131
for ( const item of items ) {
122
132
if ( item . yek . yek . kind !== item . lav . lav . kind ) {
@@ -128,7 +138,7 @@ class YekLavStore {
128
138
let records : Record < string , any > = { }
129
139
for ( const item of items ) {
130
140
const key = this . stringify ( item . yek )
131
- if ( records . keys ( ) . indexOf ( key ) !== - 1 ) {
141
+ if ( Object . keys ( records ) . indexOf ( key ) !== - 1 ) {
132
142
throw new Error ( `Attempted to set more than 1 value for key '${ key } '` )
133
143
}
134
144
records [ key ] = item . lav
@@ -605,15 +615,16 @@ async function advanceUserIngestionProgress(
605
615
return { ack : true }
606
616
}
607
617
608
- export function makeLocalStorageApi (
618
+ export function makeBrowserExtStorageApi (
609
619
browserStore : browser . Storage . StorageArea
610
620
) : StorageApi {
611
621
const store = new YekLavStore ( browserStore )
612
622
613
623
const throwUnimplementedError = ( endpoint : string ) => {
614
624
return ( ..._ : any [ ] ) : never => {
615
625
throw new Error (
616
- `Attempted to call an ${ endpoint } endpoint of local StorageApi which hasn't been implemented yet`
626
+ `Attempted to call an ${ endpoint } endpoint of browser ` +
627
+ "extension's local StorageApi which hasn't been implemented yet"
617
628
)
618
629
}
619
630
}
0 commit comments