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()
tip

ServiceBag does not allow services to yield on :Init() or :Start(), nor does it allow you to add services after :Init() or :Start()

Types

Service

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

ServiceType

type ServiceType = Service | ModuleScript

Functions

new

ServiceBag.new(
parentProviderServiceBag?--

Optional parent provider to find services in

) → ServiceBag

Constructs a new ServiceBag

isServiceBag

ServiceBag.isServiceBag(valueServiceBag?) → boolean

Returns whether the value is a serviceBag

GetService

ServiceBag:GetService(serviceTypeServiceType) → any

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

HasService

ServiceBag:HasService(serviceTypeServiceType) → 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": 59,
                "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": 86,
                "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": 103,
                "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": 133,
                "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": 149,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        },
        {
            "name": "Start",
            "desc": "Starts the service bag and all services",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 167,
                "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": 216,
                "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": 226,
                "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": 322,
                "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": 43,
                "path": "src/servicebag/src/Shared/ServiceBag.lua"
            }
        },
        {
            "name": "ServiceType",
            "desc": "",
            "lua_type": "Service | ModuleScript",
            "source": {
                "line": 48,
                "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```\n\n:::tip\nServiceBag does not allow services to yield on :Init() or :Start(), nor\ndoes it allow you to add services after :Init() or :Start()\n:::",
    "source": {
        "line": 29,
        "path": "src/servicebag/src/Shared/ServiceBag.lua"
    }
}