12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- module PushMessages
- def self.enabled?
- return true if Thread.current[:push_messages].class == Array
- false
- end
- def self.init
- return true if enabled?
- 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
- message = { type: 'broadcast', data: data }
- Thread.current[:push_messages].push message
- end
- def self.send_to(user_id, data)
- if !PushMessages.enabled?
- Sessions.send_to(user_id, data)
- return true
- end
- message = { type: 'send_to', user_id: user_id, data: data }
- Thread.current[:push_messages].push message
- end
- def self.finish
- return false if !enabled?
- Thread.current[:push_messages].each { |message|
- if message[:type] == 'send_to'
- Sessions.send_to(message[:user_id], message[:data])
- else
- Sessions.broadcast(
- message[:data][:message],
- message[:data][:type],
- message[:data][:current_user_id],
- )
- end
- }
- Thread.current[:push_messages] = nil
- true
- end
- end
|