Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singletons are not initialized #4

Open
IcedQuinn opened this issue Jan 6, 2023 · 0 comments
Open

Singletons are not initialized #4

IcedQuinn opened this issue Jan 6, 2023 · 0 comments

Comments

@IcedQuinn
Copy link

There is a bit of a goof. If singletons are enabled but have not yet been initialized then they are assigned to instance which is still nil. The code path to initialize the object is ignored.

I've fixed it with this patch.

diff --git a/lua-di.lua b/lua-di.lua
index bfa4a10..b606d5a 100644
--- a/lua-di.lua
+++ b/lua-di.lua
@@ -166,19 +166,20 @@ local dependency_injection_module = function(configure)
             instance = providers[module_name] 
         end
 
+        local module_handle = dynamic_require(module_name)
+        local module_constructor = get_constructor(module_name, module_handle)
+
+        -- new instance
+        instance = build_instance(module_constructor)
         if singletons[module_name] then
             -- initialise singleton instance if needed
             singletons[module_name].instance = instance
 
             -- singleton instance
             return singletons[module_name].instance
+        else
+            return instance
         end
-
-        local module_handle = dynamic_require(module_name)
-        local module_constructor = get_constructor(module_name, module_handle)
-
-        -- new instance
-        return build_instance(module_constructor)
     end
 
     return self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant