mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Popups
synced 2024-12-11 07:16:08 +00:00
3f03d681c9
Changes: * Add the ext.popups.experiment module * Add the mw.popups.experiment.isUserInCondition function, which returns a promise that resolves with true if the user is in the experimental condition, otherwise false * If the experiment isn't configured, i.e. wgPopupsExperimentConfig is null or undefined, then the user isn't entered into the experiment, providing a kill switch Bug: T132604 Change-Id: I49597494273e3862711a32e4951c8598e6c8bf59
80 lines
1.9 KiB
JavaScript
80 lines
1.9 KiB
JavaScript
( function ( mw, $ ) {
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
function getToken() {
|
|
var key = 'PopupsExperimentID',
|
|
id = mw.storage.get( key );
|
|
|
|
if ( !id ) {
|
|
id = mw.user.generateRandomSessionId();
|
|
|
|
mw.storage.set( key, id );
|
|
}
|
|
|
|
return id;
|
|
}
|
|
|
|
/**
|
|
* Has the user previously enabled Popups by clicking "Enable previews" in the
|
|
* footer menu?
|
|
*
|
|
* @return {boolean}
|
|
* @ignore
|
|
*/
|
|
function hasUserEnabledFeature() {
|
|
var value = $.jStorage.get( 'mwe-popups-enabled' );
|
|
|
|
return Boolean( value ) && value !== 'false';
|
|
}
|
|
|
|
/**
|
|
* @class mw.popups.experiment
|
|
* @singleton
|
|
*/
|
|
mw.popups.experiment = {};
|
|
|
|
/**
|
|
* Gets whether or not the user has Popups enabled, i.e. whether they are in the experiment
|
|
* condition.
|
|
*
|
|
* The user is in the experiment condition if:
|
|
* * they've enabled Popups by click "Enable previews" in the footer menu, or
|
|
* * they've enabled Popups as a beta feature, or
|
|
* * they aren't in the control bucket of the experiment
|
|
*
|
|
* N.B. that the user isn't entered into the experiment, i.e. they aren't assigned or a bucket,
|
|
* if the experiment isn't configured.
|
|
*
|
|
* @return {jQuery.Promise}
|
|
*/
|
|
mw.popups.experiment.isUserInCondition = function isUserInCondition() {
|
|
var deferred = $.Deferred(),
|
|
config = mw.config.get( 'wgPopupsExperimentConfig' ),
|
|
result;
|
|
|
|
if (
|
|
hasUserEnabledFeature() ||
|
|
|
|
// Users with the beta feature enabled are already in the experimental condition.
|
|
mw.config.get( 'wgPopupsExperimentIsBetaFeatureEnabled', false )
|
|
) {
|
|
deferred.resolve( true );
|
|
} else if ( !config ) {
|
|
deferred.resolve( false );
|
|
} else {
|
|
mw.requestIdleCallback( function () {
|
|
// FIXME: mw.experiments should expose the CONTROL_BUCKET constant, e.g.
|
|
// `mw.experiments.CONTROL_BUCKET`.
|
|
result = mw.experiments.getBucket( config, getToken() ) !== 'control';
|
|
|
|
deferred.resolve( result );
|
|
} );
|
|
}
|
|
|
|
return deferred.promise();
|
|
};
|
|
|
|
}( mediaWiki, jQuery ) );
|