Skip to main content

Execution Model

The execution model is how Cradle handles the execution of services, controllers and components. This is important to understand as it can help you understand how Cradle works. This guide will go over the execution model in detail.

Lifecycle

The lifecycle of a Cradle is as follows:

  1. Require Cradle
  2. Call Cradle:Load()
    • All Services or Controllers will be created
    • All Components will be created
  3. Call Cradle:Start()
    • All :Init() methods will run at the same time and will yield until all are finished
    • All :Start() methods will run at the same time and will yield until all are finished
    • Components will start after all Services or Controllers have started

Lifecycle

Bootstrap

The bootstrap is the first thing that runs when Cradle is required. This is where cradle is loaded and the lifecycle begins. The bootstrap is where you will load your services, controllers and components.

local Cradle = require(game:GetService("ReplicatedStorage").Cradle)
Cradle:Load()
-- Global Middlewares Goes Here (Server Only)
Cradle:Start()
danger

You should only use Cradle:Load() and Cradle:Start() in the bootstrap once on the client and once on the server. If you call these methods more than once, you will get unexpected results.

Best Practices

  • Only one Script on the server should manage loading services and starting Cradle
  • Only one LocalScript on the client should manage loading controllers and starting Cradle
  • Code within :Init() and within the root scope of the Service/Controller should try to finish ASAP, and should avoid yielding if possible
  • RemoteComm objects and methods should never be added to a service's Client table after Cradle:Start() has been called