mediawiki-extensions-Popups/resources/ext.popups.experiment.js
Sam Smith 3f03d681c9 Add ext.popups.experiment module
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
2016-05-18 17:28:46 +01:00

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 ) );