robotlegs / robotlegs-framework

An ActionScript 3 application framework for Flash and Flex
https://robotlegs.tenderapp.com/
MIT License
967 stars 261 forks source link

Dead loop when 2 singleton injectee reference each other #173

Open 7ujian opened 9 years ago

7ujian commented 9 years ago

When 2 singleton injectee reference each other. A dead loop is caused during injection.

Here is an example:

class A { [Inject] B; }

class B { [Inject] A; } class Foo { public function Foo() { var injector:Injector = new Injector(); injector.map(A).asSingleton(); injector.map(B).asSingleton(); injector.getInstance(A); }

}

A fixed is adding a "postInstantiate" callback in Injector before "applyInjectionPoints" and assign the instance to _response in the callback.

SingletonProvider.as

    private function createResponse(injector : Injector) : Object
    {
        if (_destroyed)
        {
            throw new InjectorError("Forbidden usage of unmapped singleton provider for type "
                + getQualifiedClassName(_responseType));
        }
        return injector.instantiateUnmapped(_responseType, postInstantiate);
    }

    private function postInstantiate(instance : Object)
    {
        _response = instance;
    }