Debian Codegraph @

extensions/VisualEditor/lib/ve/src/init/ve.init.ConflictableStorage.js

raw symbols SCIP
1 /**
2 * @classdesc
3 * Conflict-safe storage extending a ve.init.SafeStorage instance
4 *
5 * Implements conflict handling for localStorage:
6 * Any time the storage is used and it is detected that another process has
7 * modified the underlying data, all the managed keys are restored from an
8 * in-memory cache. There is no merging of data, all managed keys are either
9 * completely overwritten, or deleted if they were not originally set.
10 *
11 * This would be namespaced ve.init.ConflictableStorage, but as a generated class
12 * it is never exported.
13 *
14 * @class ve.init.ConflictableStorage
15 * @extends ve.init.SafeStorage
16 * @hideconstructor
17 */
18
19 /**
20 * Create a conflictable storage object, extending a safe storage object.
21 *
22 * @param {ve.init.SafeStorage} storage Storage class to extend
23 * @return {ve.init.ConflictableStorage} [description]
24 */
25 ve.init.createConflictableStorage = function ( storage ) {
26 const conflictKey = '__conflictId';
27 const EXPIRY_PREFIX = '_EXPIRY_';
28
29 /**
30 * @constructor
31 * @param {Storage|undefined} store The Storage instance to wrap around
32 */
33 function ConflictableStorage() {
34 // Parent constructor
35 ConflictableStorage.super.apply( this, arguments );
36
37 this.storageMayConflict = false;
38 this.conflictBackup = {};
39 this.conflictableKeys = {};
40 this.conflictId = null;
41 }
42
43 /* Inheritance */
44
45 // Dynamically extend the class of the storage object, in case
46 // it is a sub-class of SafeStorage.
47 const ParentStorage = storage.constructor;
48 OO.inheritClass( ConflictableStorage, ParentStorage );
49
50 /* Methods */
51
52 /**
53 * @inheritdoc
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 // Parent method
69 return ConflictableStorage.super.prototype.set.apply( this, arguments );
70 };
71
72 /**
73 * @inheritdoc
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 // Parent method
89 return ConflictableStorage.super.prototype.remove.apply( this, arguments );
90 };
91
92 /**
93 * @inheritdoc
94 */
95 ConflictableStorage.prototype.get = function () {
96 if ( this.isConflicted() ) {
97 this.overwriteFromBackup();
98 }
99
100 // Parent method
101 return ConflictableStorage.super.prototype.get.apply( this, arguments );
102 };
103
104 /**
105 * @inheritdoc
106 */
107 ConflictableStorage.prototype.setExpires = function ( key ) {
108 // Parent method
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 * Check if another process has written to the shared storage, leaving
129 * our data in a conflicted state.
130 *
131 * @return {boolean} Data is conflicted
132 */
133 ConflictableStorage.prototype.isConflicted = function () {
134 if ( !this.storageMayConflict ) {
135 return false;
136 }
137 // Read directly from store to avoid any caching used by sub-classes
138 try {
139 return this.store.getItem( conflictKey ) !== this.conflictId;
140 } catch ( e ) {
141 return false;
142 }
143 };
144
145 /**
146 * Overwrite data in the store from our in-memory backup
147 *
148 * Only keys added in #addConflictableKeys are restored
149 */
150 ConflictableStorage.prototype.overwriteFromBackup = function () {
151 // Call parent method directly when setting conflict key
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 // Call parent methods directly when restoring
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 * Add keys which will need to be conflict-aware
173 *
174 * @param {Object} keys Object with conflict-aware keys as keys, and value set to true
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 // Call parent method directly when setting conflict key
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 };