Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
Adds support for orderly destruction of all managed objects (i.e. the…
Browse files Browse the repository at this point in the history
… objects created by or injected into by the injector) using Injector#teardown()
  • Loading branch information
tschneidereit committed Jun 1, 2012
1 parent 425cd95 commit 4f3ed47
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/org/swiftsuspenders/injection/Injector.as
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -183,6 +184,7 @@ package org.swiftsuspenders.injection
{
_mappings = new Dictionary();
_mappingsInProcess = new Dictionary();
_managedObjects = new Dictionary();
try
{
_reflector = DescribeTypeJSON.available
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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);
}
Expand Down Expand Up @@ -401,6 +400,10 @@ package org.swiftsuspenders.injection
{
mapping.getProvider().destroy();
}
for each (var instance : Object in _managedObjects)
{
destroyInstance(instance);
}
_mappings = null;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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));
}
Expand Down
15 changes: 15 additions & 0 deletions test/org/swiftsuspenders/InjectorTests.as
Original file line number Diff line number Diff line change
Expand Up @@ -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.