diff --git a/src/main.ts b/src/main.ts index 0b3bb78..13c85b9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,7 +12,7 @@ import { VerseOfDayModal } from './suggesetor/VerseOfDayModal' import { getVod } from './provider/VODProvider' import { splitBibleReference } from './utils/splitBibleReference' import { VerseOfDaySuggesting } from './verse/VerseOfDaySuggesting' -import getFlags from './provider/FeatureFlag' +import { FlagService } from './provider/FeatureFlag' import { EventStats } from './provider/EventStats' export default class BibleReferencePlugin extends Plugin { @@ -37,9 +37,9 @@ export default class BibleReferencePlugin extends Plugin { this.addVerseLookupCommand() this.verseOfDayModal = new VerseOfDayModal(this, this.settings) - const flags = await getFlags() - console.debug(flags, flags.isFeatureEnabled('vod')) - if (flags.isFeatureEnabled('vod')) { + const flagService = FlagService.getInstace() + await flagService.init() + if (FlagService.instance.isFeatureEnabled('vod')) { console.debug('vod feature flag enabled') this.registerEditorSuggest( new VerseOfDayEditorSuggester(this, this.settings) diff --git a/src/provider/EventStats.ts b/src/provider/EventStats.ts index 237c5d2..dcfc2c7 100644 --- a/src/provider/EventStats.ts +++ b/src/provider/EventStats.ts @@ -63,38 +63,38 @@ class Logger { }) } - logRecord(optOut?: boolean) { + async logRecord(optOut?: boolean) { if (!optOut) { this.record = Logger.instance.ackeeTracker.record(this.domainId) this.record.stop() // to not over record } } - logSettingChange = ( + async logSettingChange( eventName: SettingChange, actionAttributes: ActionAttributes, optOut?: boolean - ): void => { + ) { if (!optOut) { this.fireEvent(this.getEventId(eventName), actionAttributes) } } - logLookup = ( + async logLookup( eventName: VerseLookUp, actionAttributes: ActionAttributes, optOut?: boolean - ): void => { + ) { if (!optOut) { this.fireEvent(this.getEventId(eventName), actionAttributes) } } - logUIOpen = ( + async logUIOpen( eventName: UIOpen, actionAttributes: ActionAttributes, optOut?: boolean - ): void => { + ) { if (!optOut) { this.fireEvent(this.getEventId(eventName), actionAttributes) } diff --git a/src/provider/FeatureFlag.ts b/src/provider/FeatureFlag.ts index 578ff80..7d3625b 100644 --- a/src/provider/FeatureFlag.ts +++ b/src/provider/FeatureFlag.ts @@ -4,8 +4,22 @@ const flagsmith = new Flagsmith({ environmentKey: 'NJTKgnNToZxbe6TCksAcmD', }) -const getFlags = async (): Promise => { - return flagsmith.getEnvironmentFlags() -} +export class FlagService { + public static instance: FlagService + private flags: Flags + + public static getInstace(): FlagService { + if (!FlagService.instance) { + FlagService.instance = new FlagService() + } + return FlagService.instance + } -export default getFlags + public async init() { + this.flags = await flagsmith.getEnvironmentFlags() + } + + public isFeatureEnabled(feature: string): boolean { + return this.flags.isFeatureEnabled(feature) + } +} diff --git a/src/ui/BibleReferenceSettingTab.ts b/src/ui/BibleReferenceSettingTab.ts index 6f005cd..f7a20e6 100644 --- a/src/ui/BibleReferenceSettingTab.ts +++ b/src/ui/BibleReferenceSettingTab.ts @@ -23,7 +23,7 @@ import { BibleVerseNumberFormat, BibleVerseNumberFormatCollection, } from '../data/BibleVerseNumberFormat' -import getFlags from '../provider/FeatureFlag' +import { FlagService } from '../provider/FeatureFlag' import { BibleAPISourceCollection } from '../data/BibleApiSourceCollection' import { EventStats } from '../provider/EventStats' @@ -51,12 +51,11 @@ export class BibleReferenceSettingTab extends PluginSettingTab { return BibleVersionCollection } - setUpVersionSettingsAndVersionOptions = ( - containerEl: HTMLElement, - disableBibleAPI?: boolean - ): void => { + setUpVersionSettingsAndVersionOptions = (containerEl: HTMLElement): void => { let allAvailableVersionOptions = this.getAllBibleVersionsWithLanguageNameAlphabetically() + const disableBibleAPI = + FlagService.instance.isFeatureEnabled('disable-bible-api') if (disableBibleAPI) { allAvailableVersionOptions = allAvailableVersionOptions.filter((v) => { return v.apiSource.name !== BibleAPISourceCollection.bibleApi.name @@ -272,24 +271,14 @@ export class BibleReferenceSettingTab extends PluginSettingTab { async display(): Promise { const { containerEl } = this - EventStats.logUIOpen( - 'settingsOpen', - { key: 'open', value: 1 }, - this.plugin.settings.optOutToEvents - ) containerEl.empty() const headingSection = containerEl.createDiv() headingSection.innerHTML = `