fix remove listener (#3196)

This commit is contained in:
Paulus Schoutsen 2016-09-07 06:59:59 -07:00 committed by GitHub
parent 35b388edce
commit 5995f2438e
6 changed files with 54 additions and 44 deletions

View file

@ -98,31 +98,32 @@ class APIEventStream(HomeAssistantView):
def stream():
"""Stream events to response."""
self.hass.bus.listen(MATCH_ALL, forward_events)
unsub_stream = self.hass.bus.listen(MATCH_ALL, forward_events)
_LOGGER.debug('STREAM %s ATTACHED', id(stop_obj))
try:
_LOGGER.debug('STREAM %s ATTACHED', id(stop_obj))
# Fire off one message right away to have browsers fire open event
to_write.put(STREAM_PING_PAYLOAD)
# Fire off one message so browsers fire open event right away
to_write.put(STREAM_PING_PAYLOAD)
while True:
try:
payload = to_write.get(timeout=STREAM_PING_INTERVAL)
while True:
try:
payload = to_write.get(timeout=STREAM_PING_INTERVAL)
if payload is stop_obj:
if payload is stop_obj:
break
msg = "data: {}\n\n".format(payload)
_LOGGER.debug('STREAM %s WRITING %s', id(stop_obj),
msg.strip())
yield msg.encode("UTF-8")
except queue.Empty:
to_write.put(STREAM_PING_PAYLOAD)
except GeneratorExit:
break
msg = "data: {}\n\n".format(payload)
_LOGGER.debug('STREAM %s WRITING %s', id(stop_obj),
msg.strip())
yield msg.encode("UTF-8")
except queue.Empty:
to_write.put(STREAM_PING_PAYLOAD)
except GeneratorExit:
break
_LOGGER.debug('STREAM %s RESPONSE CLOSED', id(stop_obj))
self.hass.bus.remove_listener(MATCH_ALL, forward_events)
finally:
_LOGGER.debug('STREAM %s RESPONSE CLOSED', id(stop_obj))
unsub_stream()
return self.Response(stream(), mimetype='text/event-stream')