import {DEFAULT_TOTAL_BUCKETS, DIRECTION_UP, qualifiesForRollout, qualifiesForRolloutSync} from '../utils/updates'
/**
* Represents a rollout class that checks if a client qualifies for a rollout based on provided parameters.
*/
class Rollout {
/**
* Constructs a new Rollout instance.
* @param {Object} props - The properties for the Rollout instance.
* @param {number} props.percentage - The rollout percentage.
* @param {string} props.seed - The rollout seed to use with every hash.
* @param {string} [props.direction=DIRECTION_UP] - The rollout direction.
* @param {number} [props.totalBuckets=DEFAULT_TOTAL_BUCKETS] - The total number of buckets.
* @throws {Error} Throws an error if the rollout percentage is not defined or not a number.
*/
constructor (props = {}) {
if (props.percentage === undefined || props.percentage === null || isNaN(props.percentage)) {
throw new Error('Rollout percentage must be defined and a number value')
}
this.direction = props.direction || DIRECTION_UP
this.totalBuckets = props.totalBuckets || DEFAULT_TOTAL_BUCKETS
this.percentage = props.percentage
this.seed = props.seed
}
/**
* Checks if a client's identifier qualifies for the rollout.
* @param {string} clientIdentifier - The client identifier.
* @returns {Object} - An object containing the qualification result.
* @property {boolean} isQualified - Whether the client qualifies or not.
* @property {string} hashedIdentifier - The hashed client identifier.
* @property {number} clientBucket - The bucket value.
*/
async identifierQualifies (clientIdentifier) {
return qualifiesForRollout({
clientIdentifier: clientIdentifier,
seed: this.seed,
rolloutDirection: this.direction,
rolloutPercentage: this.percentage,
totalBuckets: this.totalBuckets
})
}
async identifierQualifiesSync (clientIdentifier) {
return qualifiesForRolloutSync({
clientIdentifier: clientIdentifier,
seed: this.seed,
rolloutDirection: this.direction,
rolloutPercentage: this.percentage,
totalBuckets: this.totalBuckets
})
}
}
export default Rollout