Move pusher config to main config

This commit is contained in:
Piotr Sarnacki 2015-01-30 11:25:52 +01:00
parent f94ff75a13
commit 7650520beb
3 changed files with 94 additions and 94 deletions

View File

@ -10,12 +10,25 @@ loadConfig = ->
# to allow more granular config later
pro = $('meta[name="travis.pro"]').attr('value') == 'true' || enterprise
if config.pro
pusher =
channels: []
channel_prefix: 'private-'
encrypted: true
key: ''
else
pusher =
channels: ['common']
channel_prefix: ''
encrypted: false
return {
syncingPageRedirectionTime: 5000
api_endpoint: $('meta[rel="travis.api_endpoint"]').attr('href')
source_endpoint: $('meta[rel="travis.source_endpoint"]').attr('href')
pusher_key: $('meta[name="travis.pusher_key"]').attr('value')
pusher_host: $('meta[name="travis.pusher_host"]').attr('value')
pusher_path: $('meta[name="travis.pusher_path"]').attr('value')
ga_code: $('meta[name="travis.ga_code"]').attr('value')
code_climate: $('meta[name="travis.code_climate"]').attr('value')
ssh_key_enabled: $('meta[name="travis.ssh_key_enabled"]').attr('value') == 'true'
@ -38,15 +51,19 @@ loadConfig = ->
customer_io_site_id: customer_io_site_id
intervals: { times: -1, updateTimes: 1000 }
pusher: pusher
}
initialize = (container, application) ->
application.register 'config:main', application.config, { instantiate: false }
config = application.config
application.register 'config:main', config, { instantiate: false }
application.inject('controller', 'config', 'config:main')
application.inject('route', 'config', 'config:main')
application.inject('auth', 'config', 'config:main')
application.pusher.config = config
ConfigInitializer =
name: 'config'
initialize: initialize

View File

@ -1,7 +1,6 @@
require 'routes/route'
TravisRoute = Travis.Route
channels = Travis.Pusher.CHANNELS
Route = TravisRoute.extend
renderTemplate: ->
@ -18,7 +17,7 @@ Route = TravisRoute.extend
activate: ->
# subscribe to pusher only if we're at a main route
if channels
@get('pusher').subscribeAll(channels)
if @config.pusher.channels
@get('pusher').subscribeAll(@config.pusher.channels)
Travis.MainRoute = Route

View File

@ -1,26 +1,13 @@
Travis.Pusher = (config) ->
TravisPusher = (config) ->
@init(config)
this
if Travis.config.pro
$.extend Travis.Pusher,
CHANNELS: []
CHANNEL_PREFIX: 'private-'
ENCRYPTED: true
KEY: ''
else
$.extend Travis.Pusher,
CHANNELS: ['common']
CHANNEL_PREFIX: ''
ENCRYPTED: false
TravisPusher.prototype.active_channels = []
$.extend Travis.Pusher.prototype,
active_channels: []
init: (config) ->
TravisPusher.prototype.init = (config) ->
Pusher.warn = @warn.bind(this)
Pusher.host = config.host if config.host
@pusher = new Pusher(config.key, encrypted: Travis.Pusher.ENCRYPTED, disableStats: true)
@pusher = new Pusher(config.key, encrypted: @config.pusher.encrypted, disableStats: true)
@callbacksToProcess = []
@ -29,35 +16,35 @@ $.extend Travis.Pusher.prototype,
setInterval @processSavedCallbacks.bind(this), @processingIntervalWhenHidden
subscribeAll: (channels) ->
TravisPusher.prototype.subscribeAll = (channels) ->
@subscribe(channel) for channel in channels
unsubscribeAll: (channels) ->
TravisPusher.prototype.unsubscribeAll = (channels) ->
@unsubscribe(channel) for channel in channels
subscribe: (channel) ->
TravisPusher.prototype.subscribe = (channel) ->
return unless channel
channel = @prefix(channel)
console.log("subscribing to #{channel}")
unless @pusher?.channel(channel)
@pusher.subscribe(channel).bind_all((event, data) => @receive(event, data))
unsubscribe: (channel) ->
TravisPusher.prototype.unsubscribe = (channel) ->
return unless channel
channel = @prefix(channel)
console.log("unsubscribing from #{channel}")
@pusher.unsubscribe(channel) if @pusher?.channel(channel)
prefix: (channel) ->
if channel.indexOf(Travis.Pusher.CHANNEL_PREFIX) != 0
"#{Travis.Pusher.CHANNEL_PREFIX}#{channel}"
TravisPusher.prototype.prefix = (channel) ->
if channel.indexOf(@config.pusher.channel_prefix) != 0
"#{@config.pusher.channel_prefix}#{channel}"
else
channel
# process pusher messages in batches every 5 minutes when the page is hidden
processingIntervalWhenHidden: 1000 * 60 * 5
TravisPusher.prototype.processingIntervalWhenHidden = 1000 * 60 * 5
receive: (event, data) ->
TravisPusher.prototype.receive = (event, data) ->
return if event.substr(0, 6) == 'pusher'
data = @normalize(event, data) if data.id
@ -71,20 +58,20 @@ $.extend Travis.Pusher.prototype,
Ember.run.next ->
Travis.receive(event, data)
processSavedCallbacks: ->
TravisPusher.prototype.processSavedCallbacks = ->
while callback = @callbacksToProcess.shiftObject()
callback.call(this)
processLater: (callback) ->
TravisPusher.prototype.processLater = (callback) ->
@callbacksToProcess.pushObject(callback)
processWhenVisible: (callback) ->
TravisPusher.prototype.processWhenVisible = (callback) ->
if Visibility.hidden() && Visibility.isSupported()
@processLater(callback)
else
callback.call(this)
normalize: (event, data) ->
TravisPusher.prototype.normalize = (event, data) ->
switch event
when 'build:started', 'build:finished'
data
@ -96,23 +83,20 @@ $.extend Travis.Pusher.prototype,
when 'annotation:created', 'annotation:updated'
{ annotation: data }
warn: (type, object) ->
TravisPusher.prototype.warn = (type, object) ->
console.warn(type, object.error) unless @ignoreWarning(type, object.error)
ignoreWarning: (type, error) ->
TravisPusher.prototype.ignoreWarning = (type, error) ->
code = error?.data?.code || 0
message = error?.data?.message || ''
@ignoreCode(code) || @ignoreMessage(message)
ignoreCode: (code) ->
TravisPusher.prototype.ignoreCode = (code) ->
code == 1006
ignoreMessage: (message) ->
TravisPusher.prototype.ignoreMessage = (message) ->
message.indexOf('Existing subscription') == 0 or message.indexOf('No current subscription') == 0
pusher_host = $('meta[name="travis.pusher_host"]').attr('value')
pusher_path = $('meta[name="travis.pusher_path"]').attr('value')
Pusher.SockJSTransport.isSupported = -> false if pusher_host != 'ws.pusherapp.com'
if Travis.config.pro