Dependency Injection Demo

A 4-level deep component hierarchy demonstrating Supplier's powerful context switching capabilities. Watch how configs flow and transform through the component tree.

๐Ÿš€ Live Demo

Supplier Dependency Injection Example

A 4-level deep component hierarchy with dependency injection using Supplier

Level 1 Component

Welcome to the Supplier dependency injection example! This is the top-level component that starts our 4-level deep component hierarchy.

Level 2 Component - Config Context Switcher

๐Ÿ“ฅ Original Config (received from Level 1)

Basic Info:

App Name: Supplier Example App
Version: 1.0.0
API URL: https://api.example.com

Theme:

Primary:
#3b82f6
Secondary:
#ef4444

Features:

Dark Mode:โœ…Enabled
Notifications:โœ…Enabled

๐Ÿ“ค Modified Config (sent to Level 3 & 4)

Basic Info:

App Name: ๐Ÿš€ Modified by Level 2
Version: 2.0.0-modified
API URL: https://modified-api.example.com

Theme:

Primary:
#10b981
Secondary:
#f59e0b

Features:

Dark Mode:โŒDisabled
Notifications:โŒDisabled

Context Switch: Level 2 resupplies Level 3 and 4 with a modified config. Notice how Level 4 will display different values than what Level 2 received!

Level 3 Component

This component renders Level 4, which displays the config information.

Level 4 Component (Deepest)

๐ŸŽฏ Final Config (received from Level 3)

Basic Info:

App Name: ๐Ÿš€ Modified by Level 2
Version: 2.0.0-modified
API URL: https://modified-api.example.com

Theme:

Primary:
#10b981
Secondary:
#f59e0b

Features:

Dark Mode:โŒDisabled
Notifications:โŒDisabled

How it works

Level 1 (Root): The top-level component that starts the hierarchy

Level 2 (Context Switcher): Displays the original config AND resupplies Level 3 and 4 with a modified config using resupply()

Level 3: Passes through the modified config from Level 2

Level 4 (Deepest): Displays the modified config (different from what Level 2 shows!)

๐Ÿš€ Context Switching Demo: Notice how Level 2 shows the original config, but Level 4 shows a completely different config! This demonstrates Supplier's powerful context switching capability with resupply().

Config Supply: The configuration is supplied at this page (entrypoint), flows to Level 2, gets modified, and the modified version flows to Level 4.

How It Works

Level 1 (Root)

The top-level component that starts the hierarchy and passes the original config down.

Level 2 (Context Switcher)

โญ The Magic Happens Here! Level 2 displays the original config AND creates a modified version that it supplies to Level 3 & 4 using resupply().

Level 3 (Pass-through)

Receives the modified config from Level 2 and passes it to Level 4.

Level 4 (Consumer)

Displays the final modified config - completely different from what Level 2 originally received!

๐Ÿ”ฅ Key Insight

Notice how Level 2 shows the original blue theme, but Level 4 shows a completely different emerald theme! This demonstrates Supplier's powerful context switching - the same component tree can work with different configurations at different levels.

Code Structure

1. Entry Point

typescript
// Supply config at the root
const result = Level1Agent.supply(
  parcel(ConfigResource.supply(defaultConfig))
);

2. Context Switch (Level 2)

typescript
// Create modified config
const modifiedConfig = {
  ...currentConfig,
  appName: "๐Ÿš€ Modified by Level 2",
  theme: { primaryColor: "#10b981" }
};

// Resupply with new context
const Level3Component = level3Agent.resupply(
  parcel(ConfigResource.supply(modifiedConfig))
);

3. Component Definition

typescript
const Level2Agent = register("level-2").asAgent({
  team: [Level3Agent, ConfigRendererAgent],
  factory: ($) => {
    const config = $(ConfigResource.id);
    const ConfigRenderer = $(ConfigRendererAgent.id);
    // ... render logic
  }
});

Why This Matters

๐Ÿงช Testing

Easily inject mock configurations for different test scenarios without changing your components.

๐ŸŽ›๏ธ Configuration

Switch between development, staging, and production configs dynamically at runtime.

๐Ÿ”ง Flexibility

Override dependencies at any level of your component tree without affecting parent components.