Merge "ReplyWidgetVisual: Pass in memory-wrapped store to VE"

This commit is contained in:
jenkins-bot 2022-06-17 22:26:06 +00:00 committed by Gerrit Code Review
commit 9058467403
6 changed files with 94 additions and 70 deletions

View file

@ -72,7 +72,7 @@
"CommentItem.js",
"HeadingItem.js",
"CommentDetails.js",
"MemoryStorage.js",
"createMemoryStorage.js",
"lib/moment-timezone/moment-timezone-with-data-1970-2030.js",
{
"name": "parser/data.json",

View file

@ -24,6 +24,7 @@
"CommentItem": "CommentItem",
"DmMWPingNode": "DmMWPingNode",
"HeadingItem": "HeadingItem",
"MemoryStorage": "MemoryStorage",
"moment": "moment",
"ThreadItemSet": "ThreadItemSet",
"ThreadItem": "ThreadItem"

View file

@ -1,66 +0,0 @@
/**
* MemoryStorage creates a wrapper around mw.SafeStorage objects, duplicating
* their contents in memory, so that even if the underlying storage mechanism
* fails (e.g. quota exceeded), the storage can be relied on before the
* page has been reloaded.
*
* @example
* var sessionStorage = new MemoryStorage( mw.storage.session.store );
* var localStorage = new MemoryStorage( mw.storage.store );
*
* @class
* @extends mw.SafeStorage
* @param {Object} store
*/
function MemoryStorage() {
this.data = {};
// Parent constructor
MemoryStorage.super.apply( this, arguments );
}
// HACK: SafeStorage is not exposed as a public API, but we can
// access it as the constructor of mw.storage.
var SafeStorage = mw.storage.constructor;
/* Inheritance */
OO.inheritClass( MemoryStorage, SafeStorage );
/* Methods */
/**
* @inheritdoc
*/
MemoryStorage.prototype.get = function ( key ) {
if ( Object.prototype.hasOwnProperty.call( this.data, key ) ) {
return this.data[ key ];
} else {
// Parent method
return MemoryStorage.super.prototype.get.apply( this, arguments );
}
};
/**
* @inheritdoc
*/
MemoryStorage.prototype.set = function ( key, value ) {
// Parent method
MemoryStorage.super.prototype.set.apply( this, arguments );
this.data[ key ] = value;
return true;
};
/**
* @inheritdoc
*/
MemoryStorage.prototype.remove = function ( key ) {
// Parent method
MemoryStorage.super.prototype.remove.apply( this, arguments );
delete this.data[ key ];
return true;
};
module.exports = MemoryStorage;

View file

@ -5,8 +5,8 @@ var
pageThreads,
lastControllerScrollOffset,
featuresEnabled = mw.config.get( 'wgDiscussionToolsFeaturesEnabled' ) || {},
MemoryStorage = require( './MemoryStorage.js' ),
storage = new MemoryStorage( mw.storage.session.store ),
createMemoryStorage = require( './createMemoryStorage.js' ),
storage = createMemoryStorage( mw.storage.session ),
Parser = require( './Parser.js' ),
ThreadItemSet = require( './ThreadItemSet.js' ),
CommentDetails = require( './CommentDetails.js' ),

View file

@ -0,0 +1,83 @@
/**
* createMemoryStorage creates a wrapper around mw.SafeStorage objects, duplicating
* their contents in memory, so that even if the underlying storage mechanism
* fails (e.g. quota exceeded), the storage can be relied on before the
* page has been reloaded.
*
* @example
* var sessionStorage = createMemoryStorage( mw.storage.session );
* var localStorage = createMemoryStorage( mw.storage );
*
* @param {mw.SafeStorage} storage
* @return {MemoryStorage}
*/
var createMemoryStorage = function ( storage ) {
/**
* @class
* @extends mw.SafeStorage
*
* @constructor
* @param {Storage|undefined} store The Storage instance to wrap around
*/
function MemoryStorage( store ) {
this.data = {};
// Attempt to populate memory cache with existing data.
// Ignore any errors accessing native Storage object, as in mw.SafeStorage.
try {
for ( var i = 0, l = store.length; i < l; i++ ) {
var key = store.key( i );
this.data[ key ] = store.getItem( key );
}
} catch ( e ) {}
// Parent constructor
MemoryStorage.super.apply( this, arguments );
}
/* Inheritance */
var ParentStorage = storage.constructor;
OO.inheritClass( MemoryStorage, ParentStorage );
/* Methods */
/**
* @inheritdoc
*/
MemoryStorage.prototype.get = function ( key ) {
if ( Object.prototype.hasOwnProperty.call( this.data, key ) ) {
return this.data[ key ];
} else {
// Parent method
return MemoryStorage.super.prototype.get.apply( this, arguments );
}
};
/**
* @inheritdoc
*/
MemoryStorage.prototype.set = function ( key, value ) {
// Parent method
MemoryStorage.super.prototype.set.apply( this, arguments );
this.data[ key ] = value;
return true;
};
/**
* @inheritdoc
*/
MemoryStorage.prototype.remove = function ( key ) {
// Parent method
MemoryStorage.super.prototype.remove.apply( this, arguments );
delete this.data[ key ];
return true;
};
return new MemoryStorage( storage.store );
};
module.exports = createMemoryStorage;

View file

@ -115,9 +115,15 @@ ReplyWidgetVisual.prototype.setup = function ( data, suppressNotifications ) {
focus: [ 'emit', 'bodyFocus' ]
} );
var listStorage = ve.init.platform.createListStorage( widget.storage );
// widget.storage is a MemoryStorage object. Copy over the .data cache so
// that listStorage reads from/writes to the same in-memory cache.
listStorage.data = widget.storage.data;
target.initAutosave( {
suppressNotifications: suppressNotifications,
docId: widget.storagePrefix
docId: widget.storagePrefix,
storage: listStorage
} );
widget.afterSetup();