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::
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()
assert foo.closed == True
.. _asyncio:
.. _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,27 +57,24 @@ 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())
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())
if not reactions:
self.registry.pop(obj, None)
self.registry.pop(obj, None)
def __call__(self):
for obj, reactions in self.registry.items():
for reaction in reactions:
if asyncio.iscoroutinefunction(reaction):
yield from reaction(obj)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment