My First Feature » The Boot Order & App's Lifecycle

My First Feature » The Boot Order & App's Lifecycle


The Boot Order & App's Lifecycle

Features and Services manifest's functions are executed synchronously and in the same order as they are declared in the App's manifest.

The Single Responsibility of the Feature Manifest's function is to register Actions into existing Extensions, extending the App's capabilities with custom logic.

A ForrestJS App ships a list of Lifecycle Extensions that can be used to manage an orchestated the booting sequence of your application:

const feature1 = {
  target: '$INIT_FEATURE',
  handler: () => console.log('> feature 1'),
};

const feature2 = {
  target: '$INIT_FEATURE',
  handler: () => console.log('> feature 2'),
};

In the example above, both features register an action into the same hook. In this case, the order of the features will determine which one comes first:

forrest.run({ features: [feature1, feature2] });

// > feature 1
// > feature 2

forrest.run({ features: [feature2, feature1] });

// > feature 2
// > feature 1

But the order of the features doesn't affect the execution order of different lifecycle hooks:

👉 INIT comes before START

const feature1 = {
  target: '$START_FEATURE',
  handler: () => console.log('> feature 1'),
};

const feature2 = {
  target: '$INIT_FEATURE',
  handler: () => console.log('> feature 2'),
};

forrest.run({ features: [feature1, feature2] });

// > feature 2
// > feature 1

This happens because the $INIT_FEATURE Lifecycle Extension is always fired before the $START_FEATURE one.

Use the Lifecycle Extensions if you want to guarantee proper timing in the execution of your logic.


💻 Live on CodeSandbox:
https://codesandbox.io/s/forrestjs-app-lifecycle-qkxep?file=/src/index.js


ForrestJS Lifecyle Extensions

👉 Here is a comprehensive list of the existing Lifecyle Extensions 👈

results matching ""

    No results matching ""