Lifecycle

This document describes the lifecycle of the rhinoartisan-web-3d web component, from initialization to cleanup.

📊 Lifecycle Overview

The web component follows a predictable lifecycle with several stages and corresponding events:

┌─────────────────────────────────────────────────────────────┐
│                    Component Created                         │
│                   (Custom Element Defined)                   │
└───────────────────────────┬─────────────────────────────────┘


┌─────────────────────────────────────────────────────────────┐
│                  Component Mounted to DOM                    │
│              (connectedCallback triggered)                   │
└───────────────────────────┬─────────────────────────────────┘


┌─────────────────────────────────────────────────────────────┐
│                 React Component Initializes                  │
│            - State setup                                     │
│            - Options parsing                                 │
│            - Refs initialization                             │
└───────────────────────────┬─────────────────────────────────┘


┌─────────────────────────────────────────────────────────────┐
│                   Canvas & Scene Setup                       │
│            - Three.js renderer                               │
│            - Camera initialization                           │
│            - Controls setup                                  │
└───────────────────────────┬─────────────────────────────────┘


┌─────────────────────────────────────────────────────────────┐
│                    Model Loading Phase                       │
│            - Fetch GLTF/GLB file                            │
│            - Parse geometry                                  │
│            - Apply materials                                 │
│            - Compute bounding box                            │
│                                                              │
│            Progress: 0% → 100%                               │
│            Event: 'model-loaded' (on success)                │
│            Event: 'model-load-error' (on failure)            │
└───────────────────────────┬─────────────────────────────────┘


┌─────────────────────────────────────────────────────────────┐
│                   Viewer Ready State                         │
│            - All methods exposed                             │
│            - Interactive controls enabled                    │
│            - Event: 'ready' dispatched                       │
│                                                              │
│            ✅ FULLY OPERATIONAL                              │
└───────────────────────────┬─────────────────────────────────┘


┌─────────────────────────────────────────────────────────────┐
│                    Active/Interactive                        │
│            - User interactions                               │
│            - Material changes → 'material-changed'           │
│            - Selection changes → 'selection-changed'         │
│            - Method calls                                    │
└───────────────────────────┬─────────────────────────────────┘


┌─────────────────────────────────────────────────────────────┐
│                  Component Unmounted                         │
│            (disconnectedCallback triggered)                  │
│            - Cleanup resources                               │
│            - Dispose geometries                              │
│            - Clear caches                                    │
└─────────────────────────────────────────────────────────────┘

🔄 Lifecycle Stages

1. Component Creation

When the custom element is defined in the browser.

What happens:

  • Custom element registered with customElements.define()

  • Default styles injected into document head

  • Component class ready for instantiation

Code:


2. Component Mount

When the element is added to the DOM.

What happens:

  • React root created

  • Web component connected to DOM

  • Attribute parsing begins

Code:

Timing: Immediate when element appears in DOM


3. Initialization

Internal setup phase after mount.

What happens:

  • Options parsed from attributes

  • State initialized

  • Refs created (camera, controls, experience, etc.)

  • Version logged to console

Console Output:

Timing: ~10-50ms after mount


4. Scene Setup

Three.js scene and renderer initialization.

What happens:

  • Canvas element created

  • WebGL context initialized

  • Camera positioned

  • Controls attached

  • Environment loaded

  • Lights created

Timing: ~50-200ms after mount


5. Model Loading

The 3D model is fetched and processed.

What happens:

  • GLTF/GLB file fetched from URL

  • Geometry parsed and optimized

  • Materials applied to meshes

  • Bounding box computed

  • Textures loaded (if needed)

Events:

  • Success: model-loaded event fires

  • Failure: model-load-error event fires

Timing: 200ms - 5s (depends on model size and network)

Example:


6. Ready State

Viewer is fully operational and ready for interaction.

What happens:

  • All methods exposed on the element

  • Interactive controls enabled

  • ready event dispatched

Event: ready

Timing: ~500ms after model loading completes

Example:


7. Active State

Normal operation - user interactions and API calls.

What happens:

  • User can interact with the model

  • Click to select items

  • Methods can be called

  • Materials can be changed

  • Camera can be controlled

Events:

  • selection-changed - When items are selected/deselected

  • material-changed - When materials are updated

  • texture-load-error - If texture loading fails

Duration: Until component unmounts or model changes


8. Model Change

When a new model URL is set.

What happens:

  • Loading state reactivated

  • Previous model cleared from memory

  • New model loading begins

  • Returns to Stage 5 (Model Loading)

Trigger:


9. Component Unmount

When the element is removed from the DOM.

What happens:

  • React cleanup effects run

  • Three.js resources disposed

  • Event listeners removed

  • Memory freed

Timing: When element removed from DOM


📍 Key Lifecycle Events

Event Timeline


🎯 Best Practices

1. Wait for Ready State

Always wait for the ready event before calling methods:


2. Handle Loading States

Show appropriate UI during loading:


3. Cleanup on Unmount

If you're dynamically creating/destroying the component:


4. Preload for Better UX

Preload models before showing them:


⚠️ Common Pitfalls

Calling Methods Too Early


Not Handling Model Load Errors


Memory Leaks


  • Events - All available lifecycle and interaction events

  • Methods - Available methods (ready after ready event)

  • Configuration - Initial configuration options

  • Examples - Complete usage examples


📊 Lifecycle State Diagram

Last updated

Was this helpful?