Skip to content

Commit

Permalink
Prevent stale messages on page refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisNT committed Apr 25, 2015
1 parent a63e288 commit 53788ef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
5 changes: 3 additions & 2 deletions mopidy_alarmclock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from mopidy import config, ext

from http import factory_decorator
from http import factory_decorator, MessageStore
from alarm_manager import AlarmManager


Expand All @@ -29,7 +29,8 @@ def get_config_schema(self):

def setup(self, registry):
alarm_manager = AlarmManager()
msg_store = MessageStore()
registry.add('http:app', {
'name': self.ext_name,
'factory': factory_decorator(alarm_manager),
'factory': factory_decorator(alarm_manager, msg_store),
})
19 changes: 12 additions & 7 deletions mopidy_alarmclock/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@
}

class BaseRequestHandler(tornado.web.RequestHandler):
def initialize(self, core, alarm_manager):
def initialize(self, core, alarm_manager, msg_store):
self.core = core
self.alarm_manager = alarm_manager
self.msg_store = msg_store

def send_message(self, code):
self.redirect('/alarmclock/?msg=%s' % code)
self.msg_store.msg_code = code
self.redirect('/alarmclock/')

class MainRequestHandler(BaseRequestHandler):
def get(self):
message = None
msg_code = self.get_argument('msg',None)
if msg_code and msg_code in MESSAGES:
message = MESSAGES[msg_code]
if self.msg_store.msg_code and self.msg_store.msg_code in MESSAGES:
message = MESSAGES[self.msg_store.msg_code]
self.msg_store.msg_code = None

playlists = self.core.playlists.playlists.get()

Expand Down Expand Up @@ -74,12 +76,15 @@ def get(self):
self.alarm_manager.cancel()
self.send_message('cancel')

class MessageStore(object):
msg_code = None #Message to be stored

#little hack to pass a persistent instance (alarm_manager) to the handler
#and pass the instance of mopidy.Core to the AlarmManager (via get_core)
def factory_decorator(alarm_manager):
def factory_decorator(alarm_manager, msg_store):
def app_factory(config, core):
#since all the RequestHandler-classes get the same arguments ...
bind = lambda url, klass : (url, klass, {'core': core, 'alarm_manager':alarm_manager.get_core(core)})
bind = lambda url, klass : (url, klass, {'core': core, 'alarm_manager':alarm_manager.get_core(core), 'msg_store':msg_store})

return [
(r'/static/(.*)', tornado.web.StaticFileHandler, {'path': os.path.join(os.path.dirname(__file__), 'static')}),
Expand Down

0 comments on commit 53788ef

Please sign in to comment.