2016-09-04 21:24:19 +00:00
|
|
|
module PushMessages
|
|
|
|
|
|
|
|
def self.enabled?
|
|
|
|
return true if Thread.current[:push_messages].class == Array
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.init
|
2016-09-06 05:51:12 +00:00
|
|
|
return true if enabled?
|
2016-09-04 21:24:19 +00:00
|
|
|
Thread.current[:push_messages] = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.send(data)
|
|
|
|
if !PushMessages.enabled?
|
|
|
|
Sessions.broadcast(
|
|
|
|
data[:message],
|
|
|
|
data[:type],
|
|
|
|
data[:current_user_id],
|
|
|
|
)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
Thread.current[:push_messages].push data
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.finish
|
2016-09-06 05:51:12 +00:00
|
|
|
return false if !enabled?
|
2016-09-04 21:24:19 +00:00
|
|
|
Thread.current[:push_messages].each { |data|
|
|
|
|
Sessions.broadcast(
|
|
|
|
data[:message],
|
|
|
|
data[:type],
|
|
|
|
data[:current_user_id],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Thread.current[:push_messages] = nil
|
2016-09-06 05:51:12 +00:00
|
|
|
true
|
2016-09-04 21:24:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|