# 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)


from twisted.python import log, failure
from twisted.internet import reactor, defer, protocol
from twisted.application import service
from twisted.protocols import basic
from twisted.web.client import Agent, ResponseDone
from twisted.web.http_headers import Headers
from .eventual import eventually
        self.active = False
        return self._maybeStop()

    def _maybeStart(self):
        if not (self.active and self.running):
            return
        self.continueTrying = True
        url = self.connection_starting()
        self.es = EventSource(url, self.handler, self.resetDelay)
        d = self.es.start()
        d.addBoth(self._stopped)

    def _stopped(self, res):
        self.es = None
        # we might have stopped because of a connection error, or because of
        # an intentional shutdown.
        if self.active and self.running:
            # we still want to be connected, so schedule a reconnection
            if isinstance(res, failure.Failure):
                log.err(res)
            self.retry() # will eventually call _maybeStart
            return
        # intentional shutdown
        self.stopTrying()
        for d in self.when_stopped:
            eventually(d.callback, None)
        self.when_stopped = []

    def _stop_eventsource(self):
        if self.es:

# 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/eventsource.py
from twisted.python import log, failure
from twisted.internet import reactor, defer, protocol
from twisted.application import service
from twisted.protocols import basic
from twisted.web.client import Agent, ResponseDone
from twisted.web.http_headers import Headers
from .eventual import eventually

        self.active = False
        return self._maybeStop()

    def _maybeStart(self):
        if not (self.active and self.running):
            return
        self.continueTrying = True
        url = self.connection_starting()
        self.es = EventSource(url, self.handler, self.resetDelay)
        d = self.es.start()
        d.addBoth(self._stopped)

    def _stopped(self, res):
        self.es = None
        # we might have stopped because of a connection error, or because of
        # an intentional shutdown.
        if self.active and self.running:
            # we still want to be connected, so schedule a reconnection
            if isinstance(res, failure.Failure):
                log.err(res)
            self.retry() # will eventually call _maybeStart
            return
        # intentional shutdown
        self.stopTrying()
        for d in self.when_stopped:
            eventually(d.callback, None)
        self.when_stopped = []

    def _stop_eventsource(self):
        if self.es:

            eventually(self.es.cancel)