import {Flag} from '../Flag'; import {FlagImpl} from './FlagImpl'; import {FlagProvider} from '../FlagProvider'; import {FlagCreationOptions} from '../FlagCreationOptions'; /** * An implementation of FlagProvider. */ export class FlagProviderImpl implements FlagProvider { static AUTO_CLOSE_PERIOD_MILLIS = 1; flagIdsToFlags: Map = new Map(); create(options: FlagCreationOptions): Flag { if (options.actions) { options.actions.forEach((action) => { if (!action.executeAction) { throw new Error('Flag action does not have the executeAction property defined.'); } }); } const flag = new FlagImpl(options, () => { this.flagIdsToFlags.delete(flag.id); }); this.flagIdsToFlags.set(flag.id, flag); if (options.close === 'auto') { setTimeout(() => { this.flagIdsToFlags.delete(flag.id); }, FlagProviderImpl.AUTO_CLOSE_PERIOD_MILLIS); } return flag; } closeFlagById(flagId: string): void { this.flagIdsToFlags.delete(flagId); } isFlagOpen(flagId: string): boolean { return this.flagIdsToFlags.get(flagId) !== undefined; } }