overleaf/services/web/app/coffee/Features/SystemMessages/SystemMessageManager.coffee
Henry Oswald 1a9c035647 bring system message checks down to 1 min.
can take ages when at 5 mins
2017-02-28 18:53:34 +00:00

29 lines
No EOL
856 B
CoffeeScript

SystemMessage = require("../../models/SystemMessage").SystemMessage
module.exports = SystemMessageManager =
getMessages: (callback = (error, messages) ->) ->
if @_cachedMessages?
return callback null, @_cachedMessages
else
@getMessagesFromDB (error, messages) =>
return callback(error) if error?
@_cachedMessages = messages
return callback null, messages
getMessagesFromDB: (callback = (error, messages) ->) ->
SystemMessage.find {}, callback
clearMessages: (callback = (error) ->) ->
SystemMessage.remove {}, callback
createMessage: (content, callback = (error) ->) ->
message = new SystemMessage { content: content }
message.save callback
clearCache: () ->
delete @_cachedMessages
CACHE_TIMEOUT = 1 * 60 * 1000 # 1 minutes
setInterval () ->
SystemMessageManager.clearCache()
, CACHE_TIMEOUT