Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 31x 31x 6x 4x 6x 4x 31x 13x 48x 48x 5x 1x 5x 5x 48x 13x 48x 7x 48x 13x 123x 123x 62x 46x 46x 46x 46x 3x 43x 13x 59x 34x 25x 25x 13x 12x 12x 37x 8x 8x 8x 8x 2x 8x 29x 13x 3x 3x 3x 2x 2x 3x 7x 7x 7x 7x 7x 7x 1x 13x | /** * @classdesc * Conflict-safe storage extending a ve.init.SafeStorage instance * * Implements conflict handling for localStorage: * Any time the storage is used and it is detected that another process has * modified the underlying data, all the managed keys are restored from an * in-memory cache. There is no merging of data, all managed keys are either * completely overwritten, or deleted if they were not originally set. * * This would be namespaced ve.init.ConflictableStorage, but as a generated class * it is never exported. * * @class ve.init.ConflictableStorage * @extends ve.init.SafeStorage * @hideconstructor */ /** * Create a conflictable storage object, extending a safe storage object. * * @param {ve.init.SafeStorage} storage Storage class to extend * @return {ve.init.ConflictableStorage} [description] */ ve.init.createConflictableStorage = function ( storage ) { const conflictKey = '__conflictId'; const EXPIRY_PREFIX = '_EXPIRY_'; /** * @constructor * @param {Storage|undefined} store The Storage instance to wrap around */ function ConflictableStorage() { // Parent constructor ConflictableStorage.super.apply( this, arguments ); this.storageMayConflict = false; this.conflictBackup = {}; this.conflictableKeys = {}; this.conflictId = null; } /* Inheritance */ // Dynamically extend the class of the storage object, in case // it is a sub-class of SafeStorage. const ParentStorage = storage.constructor; OO.inheritClass( ConflictableStorage, ParentStorage ); /* Methods */ /** * @inheritdoc */ ConflictableStorage.prototype.set = function ( key, value ) { Iif ( key === conflictKey ) { throw new Error( 'Can\'t set key ' + conflictKey + ' directly.' ); } if ( this.storageMayConflict ) { if ( this.isConflicted() ) { this.overwriteFromBackup(); } if ( Object.prototype.hasOwnProperty.call( this.conflictableKeys, key ) ) { this.conflictBackup[ key ] = value; } } // Parent method return ConflictableStorage.super.prototype.set.apply( this, arguments ); }; /** * @inheritdoc */ ConflictableStorage.prototype.remove = function ( key ) { Iif ( key === conflictKey ) { throw new Error( 'Can\'t remove key ' + conflictKey + ' directly.' ); } if ( this.storageMayConflict ) { if ( this.isConflicted() ) { this.overwriteFromBackup(); } Eif ( Object.prototype.hasOwnProperty.call( this.conflictableKeys, key ) ) { delete this.conflictBackup[ key ]; } } // Parent method return ConflictableStorage.super.prototype.remove.apply( this, arguments ); }; /** * @inheritdoc */ ConflictableStorage.prototype.get = function () { if ( this.isConflicted() ) { this.overwriteFromBackup(); } // Parent method return ConflictableStorage.super.prototype.get.apply( this, arguments ); }; /** * @inheritdoc */ ConflictableStorage.prototype.setExpires = function ( key ) { // Parent method ConflictableStorage.super.prototype.setExpires.apply( this, arguments ); if ( this.storageMayConflict ) { if ( Object.prototype.hasOwnProperty.call( this.conflictableKeys, key ) ) { let expiryAbsolute = null; try { expiryAbsolute = this.store.getItem( EXPIRY_PREFIX + key ); } catch ( e ) {} if ( expiryAbsolute ) { this.conflictBackup[ EXPIRY_PREFIX + key ] = expiryAbsolute; } else { delete this.conflictBackup[ EXPIRY_PREFIX + key ]; } } } }; /** * Check if another process has written to the shared storage, leaving * our data in a conflicted state. * * @return {boolean} Data is conflicted */ ConflictableStorage.prototype.isConflicted = function () { if ( !this.storageMayConflict ) { return false; } // Read directly from store to avoid any caching used by sub-classes try { return this.store.getItem( conflictKey ) !== this.conflictId; } catch ( e ) { return false; } }; /** * Overwrite data in the store from our in-memory backup * * Only keys added in #addConflictableKeys are restored */ ConflictableStorage.prototype.overwriteFromBackup = function () { // Call parent method directly when setting conflict key ConflictableStorage.super.prototype.set.call( this, conflictKey, this.conflictId ); for ( const key in this.conflictableKeys ) { if ( Object.prototype.hasOwnProperty.call( this.conflictBackup, key ) && this.conflictBackup[ key ] !== null ) { const expiryKey = EXPIRY_PREFIX + key; const expiryAbsolute = this.conflictBackup[ expiryKey ]; let expiry = null; if ( expiryAbsolute ) { expiry = expiryAbsolute - Math.floor( Date.now() / 1000 ); } // Call parent methods directly when restoring ConflictableStorage.super.prototype.set.call( this, key, this.conflictBackup[ key ], expiry ); } else { ConflictableStorage.super.prototype.remove.call( this, key, this.conflictBackup[ key ] ); } } }; /** * Add keys which will need to be conflict-aware * * @param {Object} keys Object with conflict-aware keys as keys, and value set to true */ ConflictableStorage.prototype.addConflictableKeys = function ( keys ) { ve.extendObject( this.conflictableKeys, keys ); this.storageMayConflict = true; if ( !this.conflictId ) { this.conflictId = Math.random().toString( 36 ).slice( 2 ); // Call parent method directly when setting conflict key ConflictableStorage.super.prototype.set.call( this, conflictKey, this.conflictId ); } for ( const key in keys ) { Eif ( Object.prototype.hasOwnProperty.call( keys, key ) ) { this.conflictBackup[ key ] = this.get( key ); let expiryAbsolute = null; try { expiryAbsolute = this.store.getItem( EXPIRY_PREFIX + key ); } catch ( e ) {} if ( expiryAbsolute ) { this.conflictBackup[ EXPIRY_PREFIX + key ] = expiryAbsolute; } } } }; return new ConflictableStorage( storage.store ); }; |