Skip to content

Singletons are not initialized #4

Description

@IcedQuinn

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions