Permalink
Browse files

Adds support for orderly destruction of all managed objects (i.e. the…

… objects created by or injected into by the injector) using Injector#teardown()
  • Loading branch information...
1 parent 425cd95 commit 4f3ed475d14a9b8bc6108f37ad7f5edcc8a9e8cd @tschneidereit committed Jun 1, 2012
Showing with 29 additions and 6 deletions.
  1. +14 −6 src/org/swiftsuspenders/injection/Injector.as
  2. +15 −0 test/org/swiftsuspenders/InjectorTests.as
View
20 src/org/swiftsuspenders/injection/Injector.as
@@ -171,6 +171,7 @@ package org.swiftsuspenders.injection
private var _classDescriptor : TypeDescriptor;
private var _mappings : Dictionary;
private var _mappingsInProcess : Dictionary;
+ private var _managedObjects : Dictionary;
private var _reflector : Reflector;
@@ -183,6 +184,7 @@ package org.swiftsuspenders.injection
{
_mappings = new Dictionary();
_mappingsInProcess = new Dictionary();
+ _managedObjects = new Dictionary();
try
{
_reflector = DescribeTypeJSON.available
@@ -321,9 +323,7 @@ package org.swiftsuspenders.injection
public function injectInto(target : Object) : void
{
const type : Class = _reflector.getClass(target);
- var description : TypeDescription = _classDescriptor.getDescription(type);
-
- applyInjectionPoints(target, type, description.injectionPoints);
+ applyInjectionPoints(target, type, _classDescriptor.getDescription(type));
}
/**
@@ -349,7 +349,6 @@ package org.swiftsuspenders.injection
const provider : DependencyProvider = getProvider(mappingId);
if (provider)
{
-
const ctor : ConstructorInjectionPoint = _classDescriptor.getDescription(type).ctor;
return provider.apply(targetType, this, ctor ? ctor.injectParameters : null);
}
@@ -401,6 +400,10 @@ package org.swiftsuspenders.injection
{
mapping.getProvider().destroy();
}
+ for each (var instance : Object in _managedObjects)
+ {
+ destroyInstance(instance);
+ }
_mappings = null;
}
@@ -500,7 +503,7 @@ package org.swiftsuspenders.injection
const instance : * = description.ctor.createInstance(type, this);
hasEventListener(InjectionEvent.POST_INSTANTIATE) && dispatchEvent(
new InjectionEvent(InjectionEvent.POST_INSTANTIATE, instance, type));
- applyInjectionPoints(instance, type, description.injectionPoints);
+ applyInjectionPoints(instance, type, description);
return instance;
}
@@ -559,15 +562,20 @@ package org.swiftsuspenders.injection
}
private function applyInjectionPoints(
- target : Object, targetType : Class, injectionPoint : InjectionPoint) : void
+ target : Object, targetType : Class, description : TypeDescription) : void
{
+ var injectionPoint : InjectionPoint = description.injectionPoints;
hasEventListener(InjectionEvent.PRE_CONSTRUCT) && dispatchEvent(
new InjectionEvent(InjectionEvent.PRE_CONSTRUCT, target, targetType));
while (injectionPoint)
{
injectionPoint.applyInjection(target, targetType, this);
injectionPoint = injectionPoint.next;
}
+ if (description.preDestroyMethods)
+ {
+ _managedObjects[target] = target;
+ }
hasEventListener(InjectionEvent.POST_CONSTRUCT) && dispatchEvent(
new InjectionEvent(InjectionEvent.POST_CONSTRUCT, target, targetType));
}
View
15 test/org/swiftsuspenders/InjectorTests.as
@@ -933,7 +933,22 @@ package org.swiftsuspenders
assertThat(singleton1, hasPropertyWithValue("preDestroyCalled", false));
assertThat(singleton2, hasPropertyWithValue("preDestroyCalled", false));
injector.teardown();
+ assertThat(singleton1, hasPropertyWithValue("preDestroyCalled", true));
assertThat(singleton2, hasPropertyWithValue("preDestroyCalled", true));
}
+
+ [Test]
+ public function injectorTeardownDestroysAllInstancesItInjectedInto() : void
+ {
+ const target1 : Clazz = new Clazz();
+ injector.injectInto(target1);
+ injector.map(Clazz);
+ const target2 : Clazz = injector.getInstance(Clazz);
+ assertThat(target1, hasPropertyWithValue("preDestroyCalled", false));
+ assertThat(target2, hasPropertyWithValue("preDestroyCalled", false));
+ injector.teardown();
+ assertThat(target1, hasPropertyWithValue("preDestroyCalled", true));
+ assertThat(target2, hasPropertyWithValue("preDestroyCalled", true));
+ }
}
}

0 comments on commit 4f3ed47

Please sign in to comment.