Bootstrapping
AppBuilder builds an app from its modules, start() starts it, and dispose() disposes it.
Build an app
AppBuilder.create(appName) returns a builder. appName must be a non-empty string.
These methods add to the app:
withContainerConfiguration(fn):fngets the root container and registers app services in it. See services.withModule(moduleOrFactory): adds a module thatModuleBuilderbuilt, or a function that returns a promise of one. See lazy modules.withInitialisation(fn):fngets the app and runs duringstart(), before the modules initialise.withStart(fn):fngets the app and runs at the end ofstart().
You can call each of these methods more than once. The functions run in the order you add them.
The hooks are typed to return a promise, so write them async. start() awaits whatever a hook returns.
build() returns the app.
Calling build() a second time, or a with method after build(), throws AppBuilder has already been built.
What exists when
- After
build(),app.eventBusandapp.containerexist.app.modulesis[]. - During
start(), the app registers its bus in the root container as'bus', and loads the modules intoapp.modules.
Render the app with React after start(): see EspApp.
Start the app
start() returns a promise. It runs these steps in order:
- Register the bus as
'bus'in the root container. - Run the app’s
withContainerConfigurationfunctions. - Load the modules. The lazy module functions run in parallel.
- Give each module its own child container, and run the module’s
withContainerConfigurationfunctions on it. - Run the app’s
withInitialisationhooks, one after the other. - Run each module’s
withInitialisationhooks. The modules run in parallel. - Run each module’s
withStarthooks. The modules run in parallel. - Run the app’s
withStarthooks, one after the other.
Step 7 waits until every module has finished step 6. This sample logs steps 2 to 8:
import { AppBuilder, ModuleBuilder } from 'esp-js-ui';
import type { Module } from 'esp-js-ui';
const createModule = (name: string): Module => ModuleBuilder
.create(name)
.withContainerConfiguration(() => console.log(`${name}: module container configuration`))
.withInitialisation(async () => console.log(`${name}: module initialise`))
.withStart(async () => console.log(`${name}: module start`))
.build();
const app = AppBuilder
.create('lifecycle-app')
.withContainerConfiguration(() => console.log('app: container configuration'))
.withModule(createModule('todo'))
.withModule(async () => {
console.log('weather: lazy module loaded');
return createModule('weather');
})
.withInitialisation(async () => console.log('app: initialise'))
.withStart(async () => console.log('app: start'))
.build();
await app.start();
Output, without the lines esp-js-ui writes through the esp-js logger:
app: container configuration
weather: lazy module loaded
todo: module container configuration
weather: module container configuration
app: initialise
todo: module initialise
weather: module initialise
todo: module start
weather: module start
app: start
Errors during start
- If a lazy module function rejects, the app logs the error and skips that module.
start()carries on, and the module isn’t inapp.modules. - If an app
withInitialisationorwithStarthook throws,start()rejects. - If a module’s
withInitialisationorwithStarthook throws, the app logs the error and continues. A module whosewithInitialisationfailed doesn’t run itswithStarthooks.
Call start() again
- A second call returns at once. It doesn’t wait for the first call to finish.
- After a failed start,
start()resolves and does nothing. - After
dispose(),start()rejects.
Lazy modules
Pass withModule a function that returns a promise of a module, and start() calls it in step 3.
With a dynamic import(), the bundler can put the module’s code in its own file:
import { AppBuilder } from 'esp-js-ui';
import { todoModule } from './modules/todo/todoModule';
const app = AppBuilder
.create('todo-app')
.withModule(todoModule)
.withModule(() => import('./modules/weather/weatherModule').then(m => m.weatherModule))
.build();
await app.start();
console.log(app.modules.map(m => m.moduleId));
Output:
[ 'todo', 'weather' ]
app.modules keeps the withModule order, whichever module loads first.
Dispose the app
app.dispose() disposes, in this order:
- The modules, last module first. Each module disposes its child container.
- The bus, which disposes every store. See dispose the bus.
- The root container.
dispose() catches and logs any error.
You can call it at any time, including before start(). A second call does nothing.
If dispose() runs while start() loads or initialises the modules, start() logs a warning, stops, and resolves.