2022-08-26 08:22:42 +02:00
|
|
|
import {
|
|
|
|
IExperimentalOptions,
|
|
|
|
IExternalFlagResolver,
|
|
|
|
IFlagContext,
|
|
|
|
IFlags,
|
|
|
|
IFlagResolver,
|
2022-12-20 16:10:06 +01:00
|
|
|
IFlagKey,
|
2022-08-26 08:22:42 +02:00
|
|
|
} from '../types/experimental';
|
|
|
|
export default class FlagResolver implements IFlagResolver {
|
|
|
|
private experiments: IFlags;
|
|
|
|
|
|
|
|
private externalResolver: IExternalFlagResolver;
|
|
|
|
|
|
|
|
constructor(expOpt: IExperimentalOptions) {
|
|
|
|
this.experiments = expOpt.flags;
|
|
|
|
this.externalResolver = expOpt.externalResolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAll(context?: IFlagContext): IFlags {
|
|
|
|
const flags: IFlags = { ...this.experiments };
|
|
|
|
|
2022-12-20 16:10:06 +01:00
|
|
|
Object.keys(flags).forEach((flagName: IFlagKey) => {
|
2022-08-26 08:22:42 +02:00
|
|
|
if (!this.experiments[flagName])
|
|
|
|
flags[flagName] = this.externalResolver.isEnabled(
|
|
|
|
flagName,
|
|
|
|
context,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2022-12-20 16:10:06 +01:00
|
|
|
isEnabled(expName: IFlagKey, context?: IFlagContext): boolean {
|
2022-08-26 08:22:42 +02:00
|
|
|
if (this.experiments[expName]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this.externalResolver.isEnabled(expName, context);
|
|
|
|
}
|
|
|
|
}
|