Skip to content

Commit

Permalink
fix slowness of flag service
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-hub committed Sep 4, 2023
1 parent 27602d4 commit 638efc9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions src/provider/EventStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
22 changes: 18 additions & 4 deletions src/provider/FeatureFlag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@ const flagsmith = new Flagsmith({
environmentKey: 'NJTKgnNToZxbe6TCksAcmD',
})

const getFlags = async (): Promise<Flags> => {
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)
}
}
28 changes: 11 additions & 17 deletions src/ui/BibleReferenceSettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -272,24 +271,14 @@ export class BibleReferenceSettingTab extends PluginSettingTab {

async display(): Promise<void> {
const { containerEl } = this
EventStats.logUIOpen(
'settingsOpen',
{ key: 'open', value: 1 },
this.plugin.settings.optOutToEvents
)
containerEl.empty()
const headingSection = containerEl.createDiv()
headingSection.innerHTML = `
<iframe src="https://github.com/sponsors/tim-hub/button" title="Sponsor Obsidian Bible Reference" width="116" height="32px" style="margin-right: 2em"/>
`

const flags = await getFlags()

containerEl.createEl('h2', { text: 'General Settings' })
this.setUpVersionSettingsAndVersionOptions(
containerEl,
flags.isFeatureEnabled('disable-bible-api')
)
this.setUpVersionSettingsAndVersionOptions(containerEl)
containerEl.createEl('h2', { text: 'Verses Rendering' })
this.setUpReferenceLinkPositionOptions(containerEl)
this.setUpVerseFormatOptions(containerEl)
Expand All @@ -304,7 +293,7 @@ export class BibleReferenceSettingTab extends PluginSettingTab {
this.setUpBookTagging(containerEl)
this.setUpChapterTagging(containerEl)

if (flags.isFeatureEnabled('vod')) {
if (FlagService.instance.isFeatureEnabled('vod')) {
// todo add vod settings and reflect feature flags
}

Expand All @@ -318,5 +307,10 @@ export class BibleReferenceSettingTab extends PluginSettingTab {
<a href="https://github.com/tim-hub/obsidian-bible-reference/blob/master/docs/privacy.md">Privacy Policy</a>
`
})
EventStats.logUIOpen(
'settingsOpen',
{ key: 'open', value: 1 },
this.plugin.settings.optOutToEvents
)
}
}

0 comments on commit 638efc9

Please sign in to comment.