Skip to main content

ServiceBag

Service bags handle recursive initialization of services, and the retrieval of services from a given source. This allows the composition of services without the initialization of those services becoming a pain, which makes refactoring downstream services very easy.

This also allows multiple copies of a service to exist at once, although many services right now are not designed for this.

local serviceBag = ServiceBag.new()

serviceBag:GetService({
	Init = function(self)
		print("Service initialized")
	end;
})
serviceBag:Init()
serviceBag:Start()

Types​

Service​

interface Service {
Init:: function?
Start:: function?
Destroy:: function?
}

ServiceType​

type ServiceType = Service | ModuleScript

Functions​

new​

ServiceBag.new(
parentProvider: ServiceBag?--

Optional parent provider to find services in

) → ServiceBag

Constructs a new ServiceBag

isServiceBag​

ServiceBag.isServiceBag(value: ServiceBag?) → boolean

Returns whether the value is a serviceBag

GetService​

ServiceBag:GetService(serviceType: ServiceType) → any

Retrieves the service, ensuring initialization if we are in the initialization phase.

HasService​

ServiceBag:HasService(serviceType: ServiceType) → boolean

Returns whether the service bag has the service.

Init​

ServiceBag:Init() → ()

Initializes the service bag and ensures recursive initialization can occur

Start​

ServiceBag:Start() → ()

Starts the service bag and all services

IsStarted​

ServiceBag:IsStarted() → boolean

Returns whether the service bag has fully started or not.

CreateScope​

ServiceBag:CreateScope() → ServiceBag

Creates a scoped service bag, where services within the scope will not be accessible outside of the scope.

Destroy​

ServiceBag:Destroy() → ()

Cleans up the service bag and all services that have been initialized in the service bag.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Constructs a new ServiceBag",
            "params": [
                {
                    "name": "parentProvider",
                    "desc": "Optional parent provider to find services in",
                    "lua_type": "ServiceBag?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "ServiceBag"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 53,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        },
        {
            "name": "isServiceBag",
            "desc": "Returns whether the value is a serviceBag",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "ServiceBag?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 77,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        },
        {
            "name": "GetService",
            "desc": "Retrieves the service, ensuring initialization if we are in\nthe initialization phase.",
            "params": [
                {
                    "name": "serviceType",
                    "desc": "",
                    "lua_type": "ServiceType"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 89,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        },
        {
            "name": "HasService",
            "desc": "Returns whether the service bag has the service.",
            "params": [
                {
                    "name": "serviceType",
                    "desc": "",
                    "lua_type": "ServiceType"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 117,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        },
        {
            "name": "Init",
            "desc": "Initializes the service bag and ensures recursive initialization\ncan occur",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 133,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        },
        {
            "name": "Start",
            "desc": "Starts the service bag and all services",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 152,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        },
        {
            "name": "IsStarted",
            "desc": "Returns whether the service bag has fully started or not.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 195,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        },
        {
            "name": "CreateScope",
            "desc": "Creates a scoped service bag, where services within the scope will not\nbe accessible outside of the scope.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "ServiceBag"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 205,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        },
        {
            "name": "Destroy",
            "desc": "Cleans up the service bag and all services that have been\ninitialized in the service bag.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 280,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "Service",
            "desc": "",
            "fields": [
                {
                    "name": "Init:",
                    "lua_type": "function?",
                    "desc": ""
                },
                {
                    "name": "Start:",
                    "lua_type": "function?",
                    "desc": ""
                },
                {
                    "name": "Destroy:",
                    "lua_type": "function?",
                    "desc": ""
                }
            ],
            "source": {
                "line": 37,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        },
        {
            "name": "ServiceType",
            "desc": "",
            "lua_type": "Service | ModuleScript",
            "source": {
                "line": 42,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        }
    ],
    "name": "ServiceBag",
    "desc": "Service bags handle recursive initialization of services, and the\nretrieval of services from a given source. This allows the composition\nof services without the initialization of those services becoming a pain,\nwhich makes refactoring downstream services very easy.\n\nThis also allows multiple copies of a service to exist at once, although\nmany services right now are not designed for this.\n\n```lua\nlocal serviceBag = ServiceBag.new()\n\nserviceBag:GetService({\n\tInit = function(self)\n\t\tprint(\"Service initialized\")\n\tend;\n})\nserviceBag:Init()\nserviceBag:Start()\n```",
    "source": {
        "line": 24,
        "path": "src/servicebag/src/Shared/ServiceBag.lua"
    }
}