Skip to content

Finalization Layer

Maksym Uimanov edited this page Jan 30, 2026 · 2 revisions

Finalization Layer

Overview

The Finalization Layer is the cleanup and teardown phase of the TemporalEngine.

Its main goal is to:

  • Release resources allocated during previous layers
  • Clear object pools
  • Ensure no leftover state persists between mod reloads or engine runs

It operates after all initialization, registration, metadata, configuration, and event handling are complete.


Core Responsibility

The Finalization Layer:

  • Invokes all registered ObjectPoolCleaner instances
  • Cleans both default and dynamic object pools
  • Guarantees that the system is left in a stable and clean state

This prevents memory leaks, dangling references, and inconsistent object states.


FinalizationLayer

Definition

public class FinalizationLayer implements EngineLayer

processAllTasks()

@Override
public void processAllTasks()

Step-by-step:

  1. Run default ObjectPoolCleaners:
objectPoolCleaners.forEach(ObjectPoolCleaner::clear);
  1. Access the dynamic object pool:
ObjectPool objectPool = InjectionPool.getInstance();
  1. Run all dynamic cleaners registered in the pool:
objectPool.getAll(ObjectPoolCleaner.class)
          .forEach(ObjectPoolCleaner::clear);

All cleanup logic is centralized here, ensuring predictable finalization.


FinalizationLayerBuilder

Purpose

public class FinalizationLayerBuilder

The builder:

  • Attaches the FinalizationLayer to the engine
  • Sets up default or custom cleaners
  • Processes the layer immediately via .and()
this.engineBuilder.processLayer(this.finalizationLayer);

This guarantees that all resources are cleared once the engine is done.


Default Cleaners

private static final List<ObjectPoolCleaner> DEFAULT_CLEANERS = List.of();
  • Empty by default
  • Can be extended by mods to clear specific pools or objects

Custom Cleaners

Custom cleanup tasks are implemented via the ObjectPoolCleaner functional interface:

@FunctionalInterface
public interface ObjectPoolCleaner {
    void clear();
}

Example:

ObjectPoolCleaner cacheCleaner = () -> someCache.clear();
ObjectPoolCleaner dbCleaner = () -> databaseConnectionPool.closeAll();

These can be passed to the builder:

new FinalizationLayerBuilder(engineBuilder)
    .cleaners(List.of(cacheCleaner, dbCleaner))
    .and();

Summary

The Finalization Layer:

  • Runs after Initialization, Registry, Metadata, Config, and Event layers
  • Clears default and dynamic object pools
  • Uses ObjectPoolCleaner interface to define cleanup logic
  • Ensures consistent and leak-free runtime behavior

Clone this wiki locally