advanced
Review: the error that is not the error
This is recall. The code looks finished and is wrong. Find the defect and fix it. You are graded on tests you cannot see.
This one is real. It cost most of a day.
export interface Plugin {
name: string
setup?: (app: App) => void
hooks?: Record<string, (app: App) => void>
}
export interface App {
provided: Record<string, unknown>
hooks: Record<string, Array<(app: App) => void>>
provide: (key: string, value: unknown) => void
callHook: (name: string) => void
}
export function createApp(plugins: Plugin[]): App {
const app: App = {
provided: {},
hooks: {},
provide(key, value) {
app.provided[key] = value
},
callHook(name) {
for (const hook of app.hooks[name] ?? []) hook(app)
},
}
applyPlugins(app, plugins)
return app
}
function applyPlugins(app: App, plugins: Plugin[]): void {
for (const plugin of plugins) {
for (const [name, hook] of Object.entries(plugin.hooks ?? {})) {
app.hooks[name] ??= []
app.hooks[name].push(hook)
}
}
for (const plugin of plugins) {
plugin.setup?.(app)
}
}review — find the defectCtrl+↵ submits · Ctrl+Shift+↵ runsstarter code