All files / mobile.startup showOnPageReload.js

41.66% Statements 5/12
25% Branches 1/4
50% Functions 1/2
41.66% Lines 5/12

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  1x                   1x 1x             1x                                                               1x  
var
	storageKey = 'mobileFrontend/toast';
 
/**
 * Show the previously saved toast data and delete it from storage
 *
 * @memberof Toast
 * @instance
 * @private
 */
function showPending() {
	var data = mw.storage.get( storageKey );
	Iif ( data ) {
		data = JSON.parse( data );
		mw.notify( data.content, data.options );
		mw.storage.remove( storageKey );
	}
}
 
mw.requestIdleCallback( showPending );
 
/**
 * Save the toast data in storage so that we can show it on page reload.
 * Also check whether there is a pending message that's not shown yet.
 * If yes, output a warning message and discard this message.
 * This is to ensure that the page needs to be reloaded before adding
 * a new message for showing later.
 *
 * @memberof Toast
 * @instance
 * @param {string} content Content to be placed in element
 * @param {Object|string} [options]
 *  If a string (deprecated) CSS class to add to the element
 *  If an object, more options for the notification see mw.notification.show.
 *  For backwards compatibility reasons if a string is given it will be
 *  treated as options.type
 */
function showOnPageReload( content, options ) {
	if ( mw.storage.get( storageKey ) ) {
		mw.log.warn(
			'A pending toast message already exits. ' +
				'The page should have been reloaded by now.'
		);
		return;
	}
	mw.storage.set( storageKey, JSON.stringify( {
		content,
		options
	} ) );
}
 
module.exports = { showOnPageReload };