| 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 |
ve.init.createConflictableStorage = function ( storage ) { |
| 26 |
const conflictKey = '__conflictId'; |
| 27 |
const EXPIRY_PREFIX = '_EXPIRY_'; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
function ConflictableStorage() { |
| 34 |
|
| 35 |
ConflictableStorage.super.apply( this, arguments ); |
| 36 |
|
| 37 |
this.storageMayConflict = false; |
| 38 |
this.conflictBackup = {}; |
| 39 |
this.conflictableKeys = {}; |
| 40 |
this.conflictId = null; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
const ParentStorage = storage.constructor; |
| 48 |
OO.inheritClass( ConflictableStorage, ParentStorage ); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
ConflictableStorage.prototype.set = function ( key, value ) { |
| 56 |
if ( key === conflictKey ) { |
| 57 |
throw new Error( 'Can\'t set key ' + conflictKey + ' directly.' ); |
| 58 |
} |
| 59 |
if ( this.storageMayConflict ) { |
| 60 |
if ( this.isConflicted() ) { |
| 61 |
this.overwriteFromBackup(); |
| 62 |
} |
| 63 |
if ( Object.prototype.hasOwnProperty.call( this.conflictableKeys, key ) ) { |
| 64 |
this.conflictBackup[ key ] = value; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
return ConflictableStorage.super.prototype.set.apply( this, arguments ); |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
ConflictableStorage.prototype.remove = function ( key ) { |
| 76 |
if ( key === conflictKey ) { |
| 77 |
throw new Error( 'Can\'t remove key ' + conflictKey + ' directly.' ); |
| 78 |
} |
| 79 |
if ( this.storageMayConflict ) { |
| 80 |
if ( this.isConflicted() ) { |
| 81 |
this.overwriteFromBackup(); |
| 82 |
} |
| 83 |
if ( Object.prototype.hasOwnProperty.call( this.conflictableKeys, key ) ) { |
| 84 |
delete this.conflictBackup[ key ]; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
return ConflictableStorage.super.prototype.remove.apply( this, arguments ); |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
ConflictableStorage.prototype.get = function () { |
| 96 |
if ( this.isConflicted() ) { |
| 97 |
this.overwriteFromBackup(); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
return ConflictableStorage.super.prototype.get.apply( this, arguments ); |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
ConflictableStorage.prototype.setExpires = function ( key ) { |
| 108 |
|
| 109 |
ConflictableStorage.super.prototype.setExpires.apply( this, arguments ); |
| 110 |
|
| 111 |
if ( this.storageMayConflict ) { |
| 112 |
if ( Object.prototype.hasOwnProperty.call( this.conflictableKeys, key ) ) { |
| 113 |
let expiryAbsolute = null; |
| 114 |
try { |
| 115 |
expiryAbsolute = this.store.getItem( EXPIRY_PREFIX + key ); |
| 116 |
} catch ( e ) {} |
| 117 |
|
| 118 |
if ( expiryAbsolute ) { |
| 119 |
this.conflictBackup[ EXPIRY_PREFIX + key ] = expiryAbsolute; |
| 120 |
} else { |
| 121 |
delete this.conflictBackup[ EXPIRY_PREFIX + key ]; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
ConflictableStorage.prototype.isConflicted = function () { |
| 134 |
if ( !this.storageMayConflict ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
try { |
| 139 |
return this.store.getItem( conflictKey ) !== this.conflictId; |
| 140 |
} catch ( e ) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
ConflictableStorage.prototype.overwriteFromBackup = function () { |
| 151 |
|
| 152 |
ConflictableStorage.super.prototype.set.call( this, conflictKey, this.conflictId ); |
| 153 |
|
| 154 |
for ( const key in this.conflictableKeys ) { |
| 155 |
if ( Object.prototype.hasOwnProperty.call( this.conflictBackup, key ) && this.conflictBackup[ key ] !== null ) { |
| 156 |
const expiryKey = EXPIRY_PREFIX + key; |
| 157 |
const expiryAbsolute = this.conflictBackup[ expiryKey ]; |
| 158 |
let expiry = null; |
| 159 |
if ( expiryAbsolute ) { |
| 160 |
expiry = expiryAbsolute - Math.floor( Date.now() / 1000 ); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
ConflictableStorage.super.prototype.set.call( this, key, this.conflictBackup[ key ], expiry ); |
| 165 |
} else { |
| 166 |
ConflictableStorage.super.prototype.remove.call( this, key, this.conflictBackup[ key ] ); |
| 167 |
} |
| 168 |
} |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
ConflictableStorage.prototype.addConflictableKeys = function ( keys ) { |
| 177 |
ve.extendObject( this.conflictableKeys, keys ); |
| 178 |
|
| 179 |
this.storageMayConflict = true; |
| 180 |
|
| 181 |
if ( !this.conflictId ) { |
| 182 |
this.conflictId = Math.random().toString( 36 ).slice( 2 ); |
| 183 |
|
| 184 |
ConflictableStorage.super.prototype.set.call( this, conflictKey, this.conflictId ); |
| 185 |
} |
| 186 |
|
| 187 |
for ( const key in keys ) { |
| 188 |
if ( Object.prototype.hasOwnProperty.call( keys, key ) ) { |
| 189 |
this.conflictBackup[ key ] = this.get( key ); |
| 190 |
|
| 191 |
let expiryAbsolute = null; |
| 192 |
try { |
| 193 |
expiryAbsolute = this.store.getItem( EXPIRY_PREFIX + key ); |
| 194 |
} catch ( e ) {} |
| 195 |
|
| 196 |
if ( expiryAbsolute ) { |
| 197 |
this.conflictBackup[ EXPIRY_PREFIX + key ] = expiryAbsolute; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
}; |
| 203 |
|
| 204 |
return new ConflictableStorage( storage.store ); |
| 205 |
}; |