Sometimes you want to have a global state of your app, accross all your widgets. Although it is not recommended if you can use per-widget state management. In certain cases, like to know if the user is logged in or not, it becomes very useful.
Creating a Store
A Store is basically an object that contains state variables and functions to update it.
Create a new ModuleScript, and import the base Store class:
local Store =require(game.ReplicatedStorage.Common.Enoria.packages.store.Store)localMyStore=function()-- TODOendreturn MyStore
The module is basically a function that will return our store object. We now need to create a new store object:
local Store =require(game.ReplicatedStorage.Common.Enoria.packages.store.Store)localMyStore=function()local store = Store.new({ LoggedIn =false })return storeendreturn MyStore
The Store constructor takes a dictionnary with state variables and their initial value. Then, you can add getters and setters:
local Store =require(game.ReplicatedStorage.Common.Enoria.packages.store.Store)localMyStore=function()local store = Store.new({ LoggedIn =false }) store:AddGetter("getLoggedIn", function()return store.State.LoggedInend) store:AddSetter("setLoggedIn", function(value) store.State.LoggedIn = valueend)return storeendreturn MyStore
Now, we have to create an action: something that will change a/multiple state variable(s) and rebuild the UI. It takes a dictonnary as parameter. In this example, we call a Remote Function to validate if the username and password are matching. If so, we call the setLoggedIn setter:
local Store =require(game.ReplicatedStorage.Common.Enoria.packages.store.Store)local login = game.ReplicatedStorage.loginlocalMyStore=function()local store = Store.new({ LoggedIn =false }) store:AddGetter("getLoggedIn", function()return store.State.LoggedInend) store:AddSetter("setLoggedIn", function(value) store.State.LoggedIn = valueend) store:AddAction("login", function(args)local success = login:InvokeServer(args.username, args.password)if success then store.Setters.setLoggedIn(true)else store.Setters.setLoggedIn(false)endend)return storeendreturn MyStore
We can now import it and tell Enoria to use it in our main client script: