<commit_before>PushtView = require './pusht-view'

require './pusher'

_ = require 'underscore'

JSDiff = require 'diff'

{CompositeDisposable} = require 'atom'

module.exports = Pusht =
  pushtView: null
  modalPanel: null
  subscriptions: null

  activate: (state) ->

    @pushtView = new PushtView(state.pushtViewState)
    @modalPanel = atom.workspace.addModalPanel(item: @pushtView.getElement(), visible: false)

    # Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
    @subscriptions = new CompositeDisposable

    # Register command that toggles this view
    @subscriptions.add atom.commands.add 'atom-workspace', 'pusht:toggle': => @toggle()

  deactivate: ->
    @modalPanel.destroy()
    @subscriptions.dispose()
    @pushtView.destroy()

  serialize: ->
    pushtViewState: @pushtView.serialize()

  toggle: ->

    editor = atom.workspace.getActiveEditor()

    buffer = editor.buffer

    pusher = new Pusher 'd<PASSWORD>', { authEndpoint: 'http://localhost:3000/presence/auth' }

    channel = pusher.subscribe('presence-pairing')

    me = null

    lastEvent = null

    channel.bind 'pusher:subscription_succeeded', (members) ->
      me =  members.me.id

    triggerPush = true

    channel.bind 'client-change', (data) ->
      if data.deletion
        buffer.delete data.oldRange
      else
        # unless !lastEvent or ((data.newText is lastEvent.newText) and (_.isEqual data.newRange, lastEvent.newRange))
        if !lastEvent or !((data.newText is lastEvent.newText) and (_.isEqual data.newRange, lastEvent.newRange))
          # console.log data.newText
          # console.log lastEvent.newText
          # console.log (data.newText is lastEvent.newText)
          #
          # console.log "-------------"
          #
          # console.log data.newRange
          # console.log lastEvent.newRange
          # console.log (_.isEqual data.newRange, lastEvent.newRange)

          triggerPush = false
          buffer.setTextInRange data.newRange, data.newText
      triggerPush = true

    buffer.onDidChange (event) ->
      console.log "BUG!!"
      lastEvent = event
      deletion = (event.newText.length is 0)
      if triggerPush then channel.trigger 'client-change', {deletion: deletion, oldRange: event.oldRange, newRange: event.newRange, oldText: event.oldText, newText: event.newText}<commit_msg>Backspaces now works, inserting lines doesn't<commit_after>PushtView = require './pusht-view'

require './pusher'

_ = require 'underscore'

JSDiff = require 'diff'

{CompositeDisposable} = require 'atom'

module.exports = Pusht =
  pushtView: null
  modalPanel: null
  subscriptions: null

  activate: (state) ->

    @pushtView = new PushtView(state.pushtViewState)
    @modalPanel = atom.workspace.addModalPanel(item: @pushtView.getElement(), visible: false)

    # Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
    @subscriptions = new CompositeDisposable

    # Register command that toggles this view
    @subscriptions.add atom.commands.add 'atom-workspace', 'pusht:toggle': => @toggle()

  deactivate: ->
    @modalPanel.destroy()
    @subscriptions.dispose()
    @pushtView.destroy()

  serialize: ->
    pushtViewState: @pushtView.serialize()

  toggle: ->

    editor = atom.workspace.getActiveEditor()

    buffer = editor.buffer

    pusher = new Pusher 'd41a439c438a100756f5', { authEndpoint: 'http://localhost:3000/presence/auth' }

    channel = pusher.subscribe('presence-pairing')

    me = null

    lastEvent = {}

    channel.bind 'pusher:subscription_succeeded', (members) ->
      me =  members.me.id

    triggerPush = true

    channel.bind 'client-change', (data) ->
      console.log data
      triggerPush = false
      if data.deletion
        buffer.delete data.oldRange
      else
        if !lastEvent or !((data.newText is lastEvent.newText) and (_.isEqual data.newRange, lastEvent.newRange))
          buffer.setTextInRange data.newRange, data.newText
      triggerPush = true

    buffer.onDidChange (event) ->
      console.log event
      lastEvent = {newRange: event.newRange, newText: event.newText}

      insertNewLine = (event.newText is "\n")
      deletion = !insertNewLine and (event.newText.length is 0)


      if triggerPush then channel.trigger 'client-change',
        insertNewLine: insertNewLine
        deletion: deletion
        oldRange: event.oldRange
        newRange: event.newRange
        oldText: event.oldText
        newText: event.newText
{'fraction_non_alphanumeric': 0.1100309323906319, 'fraction_numerical': 0.00552364118426867, 'mean_word_length': 3.031166518254675, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7611965', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1372, 'n_words': 323}