Commit 71552ca5 authored by Xavier Barbosa's avatar Xavier Barbosa

fix close()

parent 3f7da6f5
Pipeline #196 failed with stage
......@@ -57,5 +57,26 @@ close().
``Injector.close()`` is called.
Factories don't need to be fully qualified. For example::
@services.factory('prefix')
def foo_factory(bar):
return 'I am foo and ' + bar
assert (yield from services.get('prefix:baz')) == 'I am foo and baz'
assert (yield from services.get('prefix:qux')) == 'I am foo and qux'
Closing callback can be registered::
class Foo:
def close(self):
self.closed = True
foo = Foo()
services.close.register(foo)
services.close()
assert foo.closed == True
.. _asyncio: https://pypi.python.org/pypi/asyncio
.. _jeni: https://pypi.python.org/pypi/jeni
......@@ -3,7 +3,7 @@ import logging
from abc import ABCMeta
from collections import defaultdict, namedtuple, OrderedDict
from itertools import chain
from weakref import WeakKeyDictionary, WeakSet
from weakref import WeakKeyDictionary
from functools import wraps
logger = logging.getLogger(__name__)
......@@ -57,28 +57,25 @@ class CloseHandler:
"""Register callbacks that should be thrown on close.
"""
reaction = reaction or close_reaction
reactions = self.registry.setdefault(obj, WeakSet())
reactions = self.registry.setdefault(obj, set())
reactions.add(reaction)
print('reactions are', obj, reactions)
def unregister(self, obj, reaction=None):
"""Unregister callbacks that should not be thrown on close.
"""
if reaction:
reactions = self.registry.setdefault(obj, WeakSet())
reactions = self.registry.setdefault(obj, set())
reactions.remove(reaction)
if not reactions:
self.registry.pop(obj, None)
else:
self.registry.pop(obj, None)
@asyncio.coroutine
def __call__(self):
for obj, reactions in self.registry.items():
for reaction in reactions:
if asyncio.iscoroutinefunction(reaction):
yield from reaction(obj)
else:
reaction(obj)
reaction(obj)
self.injector.services.clear()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment