Skip to content

Utilities

Vanguard ships a dependency-free utility layer under Vanguard.Util. Utilities can be used inside or outside services, controllers, and components.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Vanguard = require(ReplicatedStorage.Packages.Vanguard)

local Promise = require(Vanguard.Util.Promise)

Utility Index

Utility Primary use Guide
Cache TTL storage, LRU capacity, lazy values Cache
Class Constructors and inheritance Classes
Cleaner Deterministic resource cleanup Cleaner
Component Tagged Instance behavior Components
Error Stable error codes and documentation links Errors
Logger Scoped level-based output Logger
Math Interpolation, remapping, easing, wrapping, and quantization Math
NetworkGuard Standalone request guard pipeline NetworkGuard
PluginHost Runtime plugin registry and hook dispatcher Plugins
Promise Asynchronous composition Promise
RateLimiter Per-key rolling-window limits RateLimiter
Replicator Server-owned replicated state trees Replicators
RemoteProperty Server-owned replicated state Networking
RemoteSignal Bidirectional remote events Networking
Signal Local event dispatch Signal
Switch Ordered case dispatch without fall-through Switch
Validator Runtime payload and data schemas Validator

Framework Shortcuts

The main module provides shortcuts for common context-aware utilities:

local cache = Vanguard.CreateCache(options)
local limiter = Vanguard.CreateRateLimiter(options)
local logger = Vanguard.CreateLogger("Scope")
local plugin = Vanguard.CreatePlugin(definition)
local class = Vanguard.CreateClass(definition)
local component = Vanguard.CreateComponent(definition)
local replicatorMarker = Vanguard.CreateReplicator(initialState)

The first three create utility objects. CreatePlugin, CreateClass, and CreateComponent also register their results. CreateReplicator returns a server remote marker for service Client tables.

Error, Math, and Switch are also exposed directly on the main module:

local message = Vanguard.Error.format("VG-CORE-001", "Example failure")
local alpha = Vanguard.Math.inverseLerp(0, 100, 25)
local result = Vanguard.Switch.match(state, { Ready = "Start" }, "Wait")

Type Exports

Each utility exports its detailed types:

local Cache = require(Vanguard.Util.Cache)
type Cache = Cache.Cache
type CacheOptions = Cache.Options

The main Vanguard module exports shared versions of the most common types. See Type System.

Cleanup Compatibility

Objects with Destroy, Cleanup, or Disconnect methods can be given to Cleaner. Cache and RateLimiter expose Destroy aliases that clear their contents. Signal destroys its BindableEvent. Components stop and detach their instances.

Clock Injection

Cache and RateLimiter accept a custom Clock function. This is useful for deterministic tests:

local now = 0
local cache = Vanguard.CreateCache({
    Clock = function()
        return now
    end,
})

Production code normally uses the default monotonic os.clock.