diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ffe194..de1944e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.2.2 +- There is now a Promise version of some Functions. + ## 1.4 - Now Converting rbxl to rbxm diff --git a/src/CrystalClient.lua b/src/CrystalClient.lua index 21f2ac5..851f230 100644 --- a/src/CrystalClient.lua +++ b/src/CrystalClient.lua @@ -188,6 +188,40 @@ function CrystalClient.CreateController(controllerDef: ControllerDef): Controlle return controller end +--[=[ + Creates a new controller. (PROMISE) + + :::caution + Controllers must be created _before_ calling `Crystal.Start()`. + ::: + ```lua + -- Create a controller + local MyController = Crystal.CreateControllerPromise { + Name = "MyController", + } + + function MyController:CrystalStart() + print("MyController started") + end + + function MyController:CrystalInit() + print("MyController initialized") + end + ``` +]=] +function CrystalClient.CreateControllerPromise(controllerDef: ControllerDef): Controller + return Promise.new(function(resolve) + assert(type(controllerDef) == "table", "Controller must be a table; got " .. type(controllerDef)) + assert(type(controllerDef.Name) == "string", "Controller.Name must be a string; got " .. type(controllerDef.Name)) + assert(#controllerDef.Name > 0, "Controller.Name must be a non-empty string") + assert(not DoesControllerExist(controllerDef.Name), "Controller \"" .. controllerDef.Name .. "\" already exists") + local controller = controllerDef :: Controller + controllers[controller.Name] = controller + resolve(controller) + end) +end + + --[=[ Requires all the modules that are children of the given parent with an optional affix. This is an easy diff --git a/src/CrystalServer.lua b/src/CrystalServer.lua index 6b8c6e2..914cc03 100644 --- a/src/CrystalServer.lua +++ b/src/CrystalServer.lua @@ -195,6 +195,58 @@ function CrystalServer.CreateService(serviceDef: ServiceDef): Service return service end +--[=[ + Constructs a new service. (PROMISE) + + :::caution + Services must be created _before_ calling `Crystal.Start()`. + ::: + ```lua + -- Create a service + local MyService = Crystal.CreateServicePromise { + Name = "MyService", + Client = {}, + } + + -- Expose a ToAllCaps remote function to the clients + function MyService.Client:ToAllCaps(player, msg) + return msg:upper() + end + + -- Crystal will call CrystalStart after all services have been initialized + function MyService:CrystalStart() + print("MyService started") + end + + -- Crystal will call CrystalInit when Crystal is first started + function MyService:CrystalInit() + print("MyService initialize") + end + ``` +]=] +function CrystalServer.CreateServicePromise(serviceDef: ServiceDef): Service + return Promise.new(function(resolve) + assert(type(serviceDef) == "table", "Service must be a table; got " .. type(serviceDef)) + assert(type(serviceDef.Name) == "string", "Service.Name must be a string; got " .. type(serviceDef.Name)) + assert(#serviceDef.Name > 0, "Service.Name must be a non-empty string") + assert(not DoesServiceExist(serviceDef.Name), "Service \"" .. serviceDef.Name .. "\" already exists") + local service = serviceDef + service.CrystalComm = ServerComm.new(CrystalRepServiceFolder, serviceDef.Name) + if type(service.Client) ~= "table" then + service.Client = {Server = service} + else + if service.Client.Server ~= service then + service.Client.Server = service + end + end + services[service.Name] = service + resolve(service) + end) +end + + + + --[=[ Requires all the modules that are children of the given parent with an optional affix. This is an easy