# Path: src/petmail/eventual.py
# def eventually(cb, *args, **kwargs):
#     """This is the eventual-send operation, used as a plan-coordination
#     primitive. The callable will be invoked (with args and kwargs) in a later
#     reactor turn. Doing 'eventually(a); eventually(b)' guarantees that a will
#     be called before b.
# 
#     Any exceptions that occur in the callable will be logged with log.err().
#     If you really want to ignore them, be sure to provide a callable that
#     catches those exceptions.
# 
#     This function returns None. If you care to know when the callable was
#     run, be sure to provide a callable that notifies somebody.
#     """
#     _theSimpleQueue.append(cb, args, kwargs)


import weakref
from wormhole.twisted.transcribe import Wormhole
from twisted.internet import defer
from .eventual import eventually
        wormhole_data = row["wormhole"]
        payload_for_them = row["wormhole_payload"].decode("hex")
        self.code = row["invitation_code"]
        if wormhole_data is None:
            self.w = Wormhole(self.appid, self.relay)
            if self.code:
                self.w.set_code(str(self.code))
                d = defer.succeed(str(self.code))
            else:
                d = self.w.get_code()
            d.addCallback(self._got_code) # saves to DB
        else:
            assert self.code is not None
            self.w = Wormhole.from_serialized(wormhole_data)
            d = defer.succeed(None)
        d.addCallback(lambda _: self.w.get_data(payload_for_them))
        return d

    def _got_code(self, code):
        self.code = code
        wormhole_data = self.w.serialize()
        self.db.update("UPDATE addressbook SET"
                       " invitation_state=?,"
                       " invitation_code=?, wormhole=?"
                       " WHERE id=?", (INVITE_WAITING_TO_COMPLETE,
                                       code, wormhole_data,
                                       self.channel_id),
                       "addressbook", self.channel_id)
        self.db.commit()
        if self._debug_when_got_code:

# Path: src/petmail/eventual.py
# def eventually(cb, *args, **kwargs):
#     """This is the eventual-send operation, used as a plan-coordination
#     primitive. The callable will be invoked (with args and kwargs) in a later
#     reactor turn. Doing 'eventually(a); eventually(b)' guarantees that a will
#     be called before b.
# 
#     Any exceptions that occur in the callable will be logged with log.err().
#     If you really want to ignore them, be sure to provide a callable that
#     catches those exceptions.
# 
#     This function returns None. If you care to know when the callable was
#     run, be sure to provide a callable that notifies somebody.
#     """
#     _theSimpleQueue.append(cb, args, kwargs)


# Path: src/petmail/invitation.py
import weakref
from wormhole.twisted.transcribe import Wormhole
from twisted.internet import defer
from .eventual import eventually

        wormhole_data = row["wormhole"]
        payload_for_them = row["wormhole_payload"].decode("hex")
        self.code = row["invitation_code"]
        if wormhole_data is None:
            self.w = Wormhole(self.appid, self.relay)
            if self.code:
                self.w.set_code(str(self.code))
                d = defer.succeed(str(self.code))
            else:
                d = self.w.get_code()
            d.addCallback(self._got_code) # saves to DB
        else:
            assert self.code is not None
            self.w = Wormhole.from_serialized(wormhole_data)
            d = defer.succeed(None)
        d.addCallback(lambda _: self.w.get_data(payload_for_them))
        return d

    def _got_code(self, code):
        self.code = code
        wormhole_data = self.w.serialize()
        self.db.update("UPDATE addressbook SET"
                       " invitation_state=?,"
                       " invitation_code=?, wormhole=?"
                       " WHERE id=?", (INVITE_WAITING_TO_COMPLETE,
                                       code, wormhole_data,
                                       self.channel_id),
                       "addressbook", self.channel_id)
        self.db.commit()
        if self._debug_when_got_code:

            eventually(self._debug_when_got_code.callback, code)