This repository was archived by the owner on Jul 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Map Singleton bug #48
Copy link
Copy link
Open
Description
Hi,
I think I found a bug in a very specific case. The getInstance will create a new instance in a PostConstruct handler, even if it is mapped as singleton.
My problem was in a more complicated scenario of an command that is dispatched from a PostConstruct in ClassA, and this ClassA was injected in the command class for monitoring purpose, throwing an error.
I minimized the code the following 3 classes to show the bug:
import org.swiftsuspenders.Injector;
import flash.display.Sprite;
public class Main extends Sprite {
public static var injector:Injector;
public function Main() {
injector = new Injector();
injector.mapSingleton(ClassA);
injector.mapSingleton(ClassB);
injector.getInstance(ClassA);
}
}
public class ClassA {
[Inject]
public var classB:ClassB;
public function ClassA() {
trace("Constructor", this);
}
[PostConstruct]
public function initialize():void {
trace("Initialize", this, ", classB instance:", classB);
Main.injector.getInstance(ClassA); // should not create a new ClassA (will throw an error)
}
}
public class ClassB {
public function ClassB() {
trace("Constructor", this);
}
[PostConstruct]
public function initialize():void {
trace("Initialize", this);
}
}
The error looks like:
Constructor [object ClassA]
Constructor [object ClassB]
Initialize [object ClassB]
Initialize [object ClassA] , classB instance: [object ClassB]
Constructor [object ClassA]
Initialize [object ClassA] , classB instance: [object ClassB]
Constructor [object ClassA]
Initialize [object ClassA] , classB instance: [object ClassB]
Constructor [object ClassA]
Initialize [object ClassA] , classB instance: [object ClassB]
Let me know what you think.
Romu
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels