Skip to content

Commit

Permalink
refactor(core): transform configManager to lazy init style
Browse files Browse the repository at this point in the history
  • Loading branch information
tadayosi committed Dec 20, 2023
1 parent 0808b2b commit ede1bde
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 36 deletions.
16 changes: 5 additions & 11 deletions packages/hawtio/src/core/config-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import fetchMock from 'jest-fetch-mock'
import { configManager, __testing__ } from './config-manager'
import { configManager } from './config-manager'

describe('ConfigManager', () => {
beforeEach(() => {
fetchMock.resetMocks()
configManager.reset()
})

test('configManager exists', () => {
Expand All @@ -23,7 +24,6 @@ describe('ConfigManager', () => {
}),
)

const configManager = new __testing__.ConfigManager()
const config = await configManager.getHawtconfig()
expect(config.branding?.appName).toEqual('Test App')
expect(config.online?.projectSelector).toEqual('environment=dev,networkzone=internal')
Expand All @@ -46,8 +46,7 @@ describe('ConfigManager', () => {
}),
)

const configManager = new __testing__.ConfigManager()
const brandingApplied = await configManager.isBrandingApplied()
const brandingApplied = await configManager.applyBranding()
expect(brandingApplied).toBe(true)
const head = document.head.innerHTML
expect(head).toContain('<title>Test App</title>')
Expand All @@ -64,8 +63,7 @@ describe('ConfigManager', () => {
// response for fetching hawtconfig.json
fetchMock.mockResponse(JSON.stringify({}))

const configManager = new __testing__.ConfigManager()
const brandingApplied = await configManager.isBrandingApplied()
const brandingApplied = await configManager.applyBranding()
expect(brandingApplied).toBe(false)
const head = document.head.innerHTML
expect(head).toContain('<title>Hawtio</title>')
Expand All @@ -81,7 +79,6 @@ describe('ConfigManager', () => {
}),
)

const configManager = new __testing__.ConfigManager()
await expect(configManager.isRouteEnabled('route1')).resolves.toBe(false)
await expect(configManager.isRouteEnabled('route2')).resolves.toBe(true)
})
Expand All @@ -90,7 +87,6 @@ describe('ConfigManager', () => {
// response for fetching hawtconfig.json
fetchMock.mockResponse('{}')

const configManager = new __testing__.ConfigManager()
const routeEnabled = await configManager.isRouteEnabled('route1')
expect(routeEnabled).toEqual(true)
})
Expand All @@ -102,15 +98,14 @@ describe('ConfigManager', () => {
disabledRoutes: ['route1', 'route3'],
}),
)
const plugins = [...Array(5).keys()].map(i => ({
const plugins = Array.from(Array(5).keys()).map(i => ({
id: `route${i}`,
title: `Route ${i}`,
path: `route${i}`,
component: () => null,
isActive: () => Promise.resolve(true),
}))

const configManager = new __testing__.ConfigManager()
const enabledPlugins = await configManager.filterEnabledPlugins(plugins)
expect(enabledPlugins.map(p => p.path)).toEqual(['route0', 'route2', 'route4'])
})
Expand Down Expand Up @@ -138,7 +133,6 @@ describe('ConfigManager', () => {
}),
)

const configManager = new __testing__.ConfigManager()
let config = await configManager.getHawtconfig()
expect(config.about?.title).toEqual('Test App')
expect(config.about?.description).toEqual('This is a test.')
Expand Down
44 changes: 19 additions & 25 deletions packages/hawtio/src/core/config-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,19 @@ export type OnlineConfig = {
export const HAWTCONFIG_JSON = 'hawtconfig.json'

class ConfigManager {
private config: Promise<Hawtconfig>
private brandingApplied: Promise<boolean>
private config?: Promise<Hawtconfig>

reset() {
this.config = undefined
}

getHawtconfig(): Promise<Hawtconfig> {
if (this.config) {
return this.config
}

constructor() {
this.config = this.loadConfig()
this.brandingApplied = this.applyBranding()
return this.config
}

private async loadConfig(): Promise<Hawtconfig> {
Expand All @@ -149,13 +156,13 @@ class ConfigManager {
}
}

private async applyBranding(): Promise<boolean> {
const config = await this.config

const branding = config.branding
async applyBranding(): Promise<boolean> {
const { branding } = await this.getHawtconfig()
if (!branding) {
return false
}

log.info('Apply branding', branding)
let applied = false
if (branding.appName) {
log.info('Updating title -', branding.appName)
Expand All @@ -181,17 +188,9 @@ class ConfigManager {
elm.prop('disabled', false)
}

getHawtconfig(): Promise<Hawtconfig> {
return this.config
}

isBrandingApplied(): Promise<boolean> {
return this.brandingApplied
}

async isRouteEnabled(path: string): Promise<boolean> {
const config = await this.config
return !config.disabledRoutes || !config.disabledRoutes.includes(path)
const { disabledRoutes } = await this.getHawtconfig()
return !disabledRoutes || !disabledRoutes.includes(path)
}

async filterEnabledPlugins(plugins: Plugin[]): Promise<Plugin[]> {
Expand All @@ -207,14 +206,9 @@ class ConfigManager {
}

async addProductInfo(name: string, value: string) {
const config = await this.config
config.about?.productInfo?.push({ name, value })
const { about } = await this.getHawtconfig()
about?.productInfo?.push({ name, value })
}
}

export const configManager = new ConfigManager()

// Export non-exported definitions for testing
export const __testing__ = {
ConfigManager,
}
8 changes: 8 additions & 0 deletions packages/hawtio/src/core/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { userService } from '@hawtiosrc/auth'
import { importRemote, ImportRemoteOptions } from '@module-federation/utilities'
import $ from 'jquery'
import { configManager } from './config-manager'
import { eventService } from './event-service'
import { log } from './globals'

Expand Down Expand Up @@ -179,7 +180,14 @@ class HawtioCore {
*/
async bootstrap() {
log.info('Bootstrapping Hawtio...')

// Apply branding
const brandingApplied = await configManager.applyBranding()
log.info('Branding applied:', brandingApplied)

// Load plugins
await this.loadPlugins()

log.info('Bootstrapped Hawtio')
}

Expand Down

0 comments on commit ede1bde

Please sign in to comment.