Sha256: f9573138711d8b1d73602ed8c72facce4a501151552dd34772a510c0e32f287c

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

import Channels from '../channels'
import Env from '../env'
import {Deps} from '../deps'

class Line
  constructor: (opts = {}) ->
    @connected = false

  connect: ->
    Channels.Loco.NotificationCenter = Deps.cable.subscriptions.create
      channel: "Loco::NotificationCenterChannel"
    ,
      connected: =>
        console.log 'ws connected'
        @connected = true
        wire = Env.loco.getWire()
        if wire?
          wire.setDelayedDisconnection()
        this._sendNotification loco: 'connected'
      disconnected: =>
        console.log 'ws disconnected'
        @connected = false
        wire = Env.loco.getWire()
        if wire?
          wire.setUuid null
          wire.fetchSyncTime after: 'connect'
        this._sendNotification loco: 'disconnected'
      rejected: =>
        console.log 'ws rejected'
        this._sendNotification loco: 'rejected'
      received: (data) =>
        if data.loco?
          this._processSystemNotification data.loco
          delete data.loco
        return if Object.keys(data).length is 0
        this._sendNotification data

  isWireAllowed: -> not @connected

  send: (data) -> Channels.Loco.NotificationCenter.send data

  _processSystemNotification: (data) ->
    if data.connection_check?
      this.send loco: {connection_check: true}
    wire = Env.loco.getWire()
    return if not wire?
    if data.sync_time?
      wire.setSyncTime data.sync_time
    if data.uuid?
      console.log "uuid: #{data.uuid}"
      wire.setUuid data.uuid
    if data.notification?
      wire.processNotification data.notification
    if data.xhr_notifications?
      wire.check()
    if data.start_ajax_polling
      console.log "wire connected"
      @connected = null
      wire.setUuid null
      wire.fetchSyncTime after: 'connect'

  _sendNotification: (data) ->
    if Deps.NotificationCenter['receivedSignal']?
      Deps.NotificationCenter.receivedSignal data
    else
      (new Deps.NotificationCenter).receivedSignal data

export default Line

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
loco-rails-3.0.3 test/dummy/node_modules/loco-js/src/base/line.coffee
loco-rails-3.0.2 test/dummy/node_modules/loco-js/src/base/line.coffee
loco-rails-3.0.1 test/dummy/node_modules/loco-js/src/base/line.coffee