Need help with bad script design!

function DataService.init(self: DataService)
    ReplicaClient.OnNew(Globals.REPLICA_PLAYER_DATA_TOKEN, function(...: any)
        onNew(self, ...)
    end)
    
    ReplicaClient.RequestData()
end

function DataService.awaitData(self: DataService): any
    if not self._data then
        self._dataReady:Wait()
    end
    
    return self._data
end

return DataService

I know this is a little dumb, but I’m making a client data module. I load all my modules in a random order, so if I have another module that loads before this and calls :awaitData(), it will cause the script to yield infinitely without it even initializing first.

I would just do something that make sure its initialized when calling that function, but I feel like that’s just a sign of bad design (In case I have other functions that yield, or other modules that have similiar behavior)

1 Like

So apparently the way im initializing my modules can be improved by adding a :init method which doesn’t yield and a :start method which is done after all modules are initialized (for safely yielding)

1 Like

This is why frameworks like Knit have both init() and start(), and I’d say its not bad design.

init() would be for setting up the module, but no communication with other modules is allowed. This is where you should set up data replicating.

Other modules can then call :awaitData() in their start().

1 Like