summaryrefslogtreecommitdiff
path: root/mail
diff options
context:
space:
mode:
authorKali Kaneko <kali@leap.se>2015-03-05 12:18:11 -0400
committerKali Kaneko <kali@leap.se>2015-03-05 15:18:19 -0400
commit5c2a6487a8a291e4b0f54c62d19f3ad3a07e7991 (patch)
tree90dae9991ab9560d02b95ff53c03a7a601ebaa2b /mail
parenta3976983d40dc4e84ef98b30275ec3fb844b462e (diff)
[feature] Keep mapping of collections
it is a weakref dictionary so that the collections can be garbage collected when out of scope. Releases: 0.4.0
Diffstat (limited to 'mail')
-rw-r--r--mail/src/leap/mail/mail.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/mail/src/leap/mail/mail.py b/mail/src/leap/mail/mail.py
index ef9a0d9f..57d96efa 100644
--- a/mail/src/leap/mail/mail.py
+++ b/mail/src/leap/mail/mail.py
@@ -20,6 +20,8 @@ Generic Access to Mail objects: Public LEAP Mail API.
import uuid
import logging
import StringIO
+import weakref
+
from twisted.internet import defer
from leap.common.check import leap_assert_type
@@ -732,6 +734,14 @@ class Account(object):
adaptor_class = SoledadMailAdaptor
+ # This is a mapping to collection instances so that we always
+ # return a reference to them instead of creating new ones. However, being a
+ # dictionary of weakrefs values, they automagically vanish from the dict
+ # when no hard refs is left to them (so they can be garbage collected)
+ # This is important because the different wrappers rely on several
+ # kinds of deferredLocks that are kept as class or instance variables
+ _collection_mapping = weakref.WeakValueDictionary()
+
def __init__(self, store, ready_cb=None):
self.store = store
self.adaptor = self.adaptor_class()
@@ -835,12 +845,19 @@ class Account(object):
def get_collection_by_mailbox(self, name):
"""
- :rtype: MessageCollection
+ :rtype: deferred
+ :return: a deferred that will fire with a MessageCollection
"""
+ collection = self._collection_mapping.get(name, None)
+ if collection:
+ return defer.succeed(collection)
+
# imap select will use this, passing the collection to SoledadMailbox
def get_collection_for_mailbox(mbox_wrapper):
- return MessageCollection(
+ collection = MessageCollection(
self.adaptor, self.store, self.mbox_indexer, mbox_wrapper)
+ self._collection_mapping[name] = collection
+ return collection
d = self.adaptor.get_or_create_mbox(self.store, name)
d.addCallback(get_collection_for_mailbox)