Fossil SCM
Simplified the previous commit considerably.
Commit
525bec99a33336a17ef369be409e7a2be280017a1d3b67b39a59e03b2306b399
Parent
a863592e08d31a4…
1 file changed
+11
-13
+11
-13
| --- src/fossil.storage.js | ||
| +++ src/fossil.storage.js | ||
| @@ -30,10 +30,17 @@ | ||
| 30 | 30 | }, |
| 31 | 31 | removeItem: function(k){delete this.$[k]}, |
| 32 | 32 | clear: function(){this.$={}} |
| 33 | 33 | }); |
| 34 | 34 | |
| 35 | + /** | |
| 36 | + For the dummy storage we need to differentiate between | |
| 37 | + $storage and its real property storage for hasOwnProperty() | |
| 38 | + to work properly... | |
| 39 | + */ | |
| 40 | + const $storageHolder = $storage.hasOwnProperty('$') ? $storage.$ : $storage; | |
| 41 | + | |
| 35 | 42 | /** |
| 36 | 43 | A proxy for localStorage or sessionStorage or a |
| 37 | 44 | page-instance-local proxy, if neither one is availble. |
| 38 | 45 | |
| 39 | 46 | Which exact storage implementation is uses is unspecified, and |
| @@ -45,15 +52,11 @@ | ||
| 45 | 52 | set: (k,v)=>$storage.setItem(k,v), |
| 46 | 53 | /** Sets storage key k to JSON.stringify(v). */ |
| 47 | 54 | setJSON: (k,v)=>$storage.setItem(k,JSON.stringify(v)), |
| 48 | 55 | /** Returns the value for the given storage key, or |
| 49 | 56 | dflt if the key is not found in the storage. */ |
| 50 | - get: function(k,dflt){ | |
| 51 | - return ( | |
| 52 | - this.isTransient() ? $storage.$ : $storage | |
| 53 | - ).hasOwnProperty(k) ? $storage.getItem(k) : dflt; | |
| 54 | - }, | |
| 57 | + get: (k,dflt)=>$storageHolder.hasOwnProperty(k) ? $storage.getItem(k) : dflt, | |
| 55 | 58 | /** Returns the JSON.parse()'d value of the given |
| 56 | 59 | storage key's value, or dflt is the key is not |
| 57 | 60 | found or JSON.parse() fails. */ |
| 58 | 61 | getJSON: function f(k,dflt){ |
| 59 | 62 | try { |
| @@ -62,30 +65,25 @@ | ||
| 62 | 65 | } |
| 63 | 66 | catch(e){return dflt} |
| 64 | 67 | }, |
| 65 | 68 | /** Returns true if the storage contains the given key, |
| 66 | 69 | else false. */ |
| 67 | - contains: function(k){ | |
| 68 | - return ( | |
| 69 | - this.isTransient() ? $storage.$ : $storage | |
| 70 | - ).hasOwnProperty(k); | |
| 71 | - }, | |
| 70 | + contains: (k)=>$storageHolder.hasOwnProperty(k), | |
| 72 | 71 | /** Removes the given key from the storage. */ |
| 73 | 72 | remove: (k)=>$storage.removeItem(k), |
| 74 | 73 | /** Clears ALL keys from the storage. */ |
| 75 | 74 | clear: ()=>$storage.clear(), |
| 76 | 75 | /** Returns an array of all keys currently in the storage. */ |
| 77 | - keys: ()=>Object.keys($storage), | |
| 76 | + keys: ()=>Object.keys($storageHolder), | |
| 78 | 77 | /** Returns true if this storage is transient (only available |
| 79 | 78 | until the page is reloaded), indicating that fileStorage |
| 80 | 79 | and sessionStorage are unavailable. */ |
| 81 | - isTransient: ()=>!($storage===window.localStorage | |
| 82 | - ||$storage===window.sessionStorage), | |
| 80 | + isTransient: ()=>$storageHolder!==$storage, | |
| 83 | 81 | /** Returns a symbolic name for the current storage mechanism. */ |
| 84 | 82 | storageImplName: function(){ |
| 85 | 83 | if($storage===window.localStorage) return 'localStorage'; |
| 86 | 84 | else if($storage===window.sessionStorage) return 'sessionStorage'; |
| 87 | 85 | else return 'transient'; |
| 88 | 86 | } |
| 89 | 87 | }; |
| 90 | 88 | |
| 91 | 89 | })(window.fossil); |
| 92 | 90 |
| --- src/fossil.storage.js | |
| +++ src/fossil.storage.js | |
| @@ -30,10 +30,17 @@ | |
| 30 | }, |
| 31 | removeItem: function(k){delete this.$[k]}, |
| 32 | clear: function(){this.$={}} |
| 33 | }); |
| 34 | |
| 35 | /** |
| 36 | A proxy for localStorage or sessionStorage or a |
| 37 | page-instance-local proxy, if neither one is availble. |
| 38 | |
| 39 | Which exact storage implementation is uses is unspecified, and |
| @@ -45,15 +52,11 @@ | |
| 45 | set: (k,v)=>$storage.setItem(k,v), |
| 46 | /** Sets storage key k to JSON.stringify(v). */ |
| 47 | setJSON: (k,v)=>$storage.setItem(k,JSON.stringify(v)), |
| 48 | /** Returns the value for the given storage key, or |
| 49 | dflt if the key is not found in the storage. */ |
| 50 | get: function(k,dflt){ |
| 51 | return ( |
| 52 | this.isTransient() ? $storage.$ : $storage |
| 53 | ).hasOwnProperty(k) ? $storage.getItem(k) : dflt; |
| 54 | }, |
| 55 | /** Returns the JSON.parse()'d value of the given |
| 56 | storage key's value, or dflt is the key is not |
| 57 | found or JSON.parse() fails. */ |
| 58 | getJSON: function f(k,dflt){ |
| 59 | try { |
| @@ -62,30 +65,25 @@ | |
| 62 | } |
| 63 | catch(e){return dflt} |
| 64 | }, |
| 65 | /** Returns true if the storage contains the given key, |
| 66 | else false. */ |
| 67 | contains: function(k){ |
| 68 | return ( |
| 69 | this.isTransient() ? $storage.$ : $storage |
| 70 | ).hasOwnProperty(k); |
| 71 | }, |
| 72 | /** Removes the given key from the storage. */ |
| 73 | remove: (k)=>$storage.removeItem(k), |
| 74 | /** Clears ALL keys from the storage. */ |
| 75 | clear: ()=>$storage.clear(), |
| 76 | /** Returns an array of all keys currently in the storage. */ |
| 77 | keys: ()=>Object.keys($storage), |
| 78 | /** Returns true if this storage is transient (only available |
| 79 | until the page is reloaded), indicating that fileStorage |
| 80 | and sessionStorage are unavailable. */ |
| 81 | isTransient: ()=>!($storage===window.localStorage |
| 82 | ||$storage===window.sessionStorage), |
| 83 | /** Returns a symbolic name for the current storage mechanism. */ |
| 84 | storageImplName: function(){ |
| 85 | if($storage===window.localStorage) return 'localStorage'; |
| 86 | else if($storage===window.sessionStorage) return 'sessionStorage'; |
| 87 | else return 'transient'; |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | })(window.fossil); |
| 92 |
| --- src/fossil.storage.js | |
| +++ src/fossil.storage.js | |
| @@ -30,10 +30,17 @@ | |
| 30 | }, |
| 31 | removeItem: function(k){delete this.$[k]}, |
| 32 | clear: function(){this.$={}} |
| 33 | }); |
| 34 | |
| 35 | /** |
| 36 | For the dummy storage we need to differentiate between |
| 37 | $storage and its real property storage for hasOwnProperty() |
| 38 | to work properly... |
| 39 | */ |
| 40 | const $storageHolder = $storage.hasOwnProperty('$') ? $storage.$ : $storage; |
| 41 | |
| 42 | /** |
| 43 | A proxy for localStorage or sessionStorage or a |
| 44 | page-instance-local proxy, if neither one is availble. |
| 45 | |
| 46 | Which exact storage implementation is uses is unspecified, and |
| @@ -45,15 +52,11 @@ | |
| 52 | set: (k,v)=>$storage.setItem(k,v), |
| 53 | /** Sets storage key k to JSON.stringify(v). */ |
| 54 | setJSON: (k,v)=>$storage.setItem(k,JSON.stringify(v)), |
| 55 | /** Returns the value for the given storage key, or |
| 56 | dflt if the key is not found in the storage. */ |
| 57 | get: (k,dflt)=>$storageHolder.hasOwnProperty(k) ? $storage.getItem(k) : dflt, |
| 58 | /** Returns the JSON.parse()'d value of the given |
| 59 | storage key's value, or dflt is the key is not |
| 60 | found or JSON.parse() fails. */ |
| 61 | getJSON: function f(k,dflt){ |
| 62 | try { |
| @@ -62,30 +65,25 @@ | |
| 65 | } |
| 66 | catch(e){return dflt} |
| 67 | }, |
| 68 | /** Returns true if the storage contains the given key, |
| 69 | else false. */ |
| 70 | contains: (k)=>$storageHolder.hasOwnProperty(k), |
| 71 | /** Removes the given key from the storage. */ |
| 72 | remove: (k)=>$storage.removeItem(k), |
| 73 | /** Clears ALL keys from the storage. */ |
| 74 | clear: ()=>$storage.clear(), |
| 75 | /** Returns an array of all keys currently in the storage. */ |
| 76 | keys: ()=>Object.keys($storageHolder), |
| 77 | /** Returns true if this storage is transient (only available |
| 78 | until the page is reloaded), indicating that fileStorage |
| 79 | and sessionStorage are unavailable. */ |
| 80 | isTransient: ()=>$storageHolder!==$storage, |
| 81 | /** Returns a symbolic name for the current storage mechanism. */ |
| 82 | storageImplName: function(){ |
| 83 | if($storage===window.localStorage) return 'localStorage'; |
| 84 | else if($storage===window.sessionStorage) return 'sessionStorage'; |
| 85 | else return 'transient'; |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | })(window.fossil); |
| 90 |