@spirex/js-boot
v1.0.0
Published
Efficient JavaScript app initialization library with staged execution and dependency management
Downloads
8
Maintainers
Readme
SpireX's JS Application Bootstrapper
Description
This small library helps solve the problem of initializing JavaScript applications. It can be used in any JS project.
The library provides an initializer class that allows you to break the initialization process into multiple stages and execute them depending on their interdependencies with other stages.
If any stage of the initialization is paused due to an asynchronous task, the initializer does not wait for its completion but proceeds to the next stage, provided that the tasks related to this stage have been completed.
Installing
You can get the latest release and the type definitions using your preferred package manager:
$ npm install @spirex/js-boot
# OR
$ yarn add @spirex/js-boot
# OR
$ bun i @spirex/js-boot
Usage
1. Import the initializer class
Almost all operations are performed using the AppBoot class. Therefore, it needs to be imported into all files where initialization tasks are defined.
import { AppBoot } from "@spirex/js-boot";
2. Create initialization tasks
The AppBoot.task()
method is used to create initialization tasks.
It accepts a function containing the executable code for the task.
The function can be synchronous or asynchronous.
The task name is optional, but helps with logging tasks to the console.
After the function, you can pass an array of dependencies. This ensures that the task will start execution as soon as all dependencies are completed.
const taskA = AppBoot.task('A', async () => {
console.log('Run task A');
await someAsyncFunction();
});
const taskB = AppBoot.task('B', () => {
console.log('Run task B');
}, [taskA]); // Depends on 'A'
3. Create the initializer and add tasks
Tasks are added to the created initializer using the add(..)
method.
You can add tasks one by one or multiple at once as an array.
const boot = new AppBoot()
.add([taskA, taskB]);
4. Run the Initialization
Once the initializer is created and all necessary tasks
for the application are added, you can start the initialization process.
The runAsync()
method will return a Promise
that resolves when all tasks are completed.
await boot.runAsync();
console.log('Done!');
By default, all tasks are mandatory. Therefore, if any task fails, the initialization process will be aborted, and an exception will be thrown.