<commit_before>{CompositeDisposable} = require 'atom'
url = require 'url'
ConsoleRuntimeObserver = require './console-runtime-observer'
ConsoleRuntimeView = require './console-runtime-view'

module.exports = SampleScriptConsumer =

  # subscriptions: null

  config:
    evaluateOnSave:
      type        : 'boolean'
      description : 'Re-evaluate on save'
      default     : true

  activate: (state) ->
    @subscriptions = new CompositeDisposable
    @subscriptions.add atom.commands.add 'atom-workspace', 'magic-console:toggle':
      => @toggle()
    @views = []
    @blankRuntime = null
    atom.workspace.addOpener (uriToOpen) =>
      try
        {protocol, host, pathname} = url.parse(uriToOpen)
      catch error
        return
      return unless protocol is 'hyper-console:'
      try
        pathname = decodeURI(pathname) if pathname
      catch error
        return
      if host is 'editor'
        editorId = pathname.substring(1)
        view = new ConsoleRuntimeView({editorId})
        changeCount = view.editor.buffer.changeCount.valueOf()
        @views = [@views..., view]
        @subscriptions.add view.onDidDestroy =>
          @blankRuntime.stop()
          @views = @views.filter (view) -> view.editorId != editorId
        @subscriptions.add view.onDidSave (ev) =>
          if atom.config.get 'magic-console.evaluateOnSave'
            if view.editor.buffer.changeCount > changeCount
              console.log 'onDidSave', @blankRuntime.observers[0].view.editor.buffer.changeCount, changeCount
              changeCount = @blankRuntime.observers[0].view.editor.buffer.changeCount
              @blankRuntime.stop()
              @runBlank(view)
        view
      else
        filePath = pathname
        view = new ConsoleRuntimeView({filePath})
        changeCount = view.editor.buffer.changeCount.valueOf()
        @views = [@views..., view]
        @subscriptions.add view.onDidDestroy =>
          @blankRuntime.stop()
          @views = @views.filter (view) -> view.filePath != filePath
        @subscriptions.add view.onDidSave (ev) =>
          if atom.config.get 'magic-console.evaluateOnSave'
            if view.editor.buffer.changeCount > changeCount
              console.log 'onDidSave', @blankRuntime.observers[0].view.editor.buffer.changeCount, changeCount
              changeCount = @blankRuntime.observers[0].view.editor.buffer.changeCount
              @blankRuntime.stop()
              @runBlank(view)
        view

  deactivate: ->
    @subscriptions.dispose()
    @view.destroy()

  # serialize: ->
  #   view: @views.forEach (view) -> view.serialize()

  # deserialize: ->
  #   view: @view.deserialize()

  consumeBlankRuntime: (runtime) ->
    @blankRuntime = runtime

  consumeDefaultRuntime: (runtime) ->
    @defaultRuntime = runtime

  activatePackage: (packageName) ->
    return if atom.packages.activePackages[packageName]?
    atomPackage = atom.packages.loadPackage(packageName)
    throw new Error("Packaged not installed: '#{packageName}'") unless atomPackage?
    atomPackage.activateNow()

  runBlank: (view) ->
    # @activatePackage('script')
    @blankRuntime?.observers.forEach (observer) -> observer.destroy()
    @blankRuntime.observers = []
    @blankRuntime.addObserver(new ConsoleRuntimeObserver(view))
    @blankRuntime.execute()

  runDefault: ->
    @activatePackage('script')
    @defaultRuntime.execute()

  toggle: ->
    editor = atom.workspace.getActiveTextEditor()
    return unless editor?
    uri = "hyper-console://editor/#{editor.id}"
    previewPane = atom.workspace.paneForURI(uri)
    if previewPane
      # previewPane.destroyItem(previewPane.itemForURI(uri))
      previousActivePane = atom.workspace.getActivePane()
      previewPane.activateItemForURI(uri)
      @blankRuntime.stop()<commit_msg>Enforce one observer at a time<commit_after>{CompositeDisposable} = require 'atom'
fs = require 'fs'
url = require 'url'
path = require 'path'
ConsoleRuntimeObserver = require './console-runtime-observer'
ConsoleRuntimeView = require './console-runtime-view'

module.exports = SampleScriptConsumer =

  config:
    evaluateOnSave:
      type        : 'boolean'
      description : 'Re-evaluate on save'
      default     : true

  activate: (state) ->
    @subscriptions = new CompositeDisposable
    @subscriptions.add atom.commands.add 'atom-workspace', 'magic-console:toggle':
      => @toggle()
    @views = []
    @blankRuntime = null
    atom.workspace.addOpener (uriToOpen) =>
      try
        {protocol, host, pathname} = url.parse(uriToOpen)
      catch error
        return
      return unless protocol is 'magic-console:'
      try
        pathname = decodeURI(pathname) if pathname
      catch error
        return
      if host is 'editor'
        editorId = pathname.substring(1)
        view = new ConsoleRuntimeView({editorId})
        @views = [@views..., view]
        @subscriptions.add view.onDidDestroy =>
          @blankRuntime.stop()
          @views = @views.filter (view) -> view.editorId != editorId
        @subscriptions.add view.onDidSave (ev) =>
          if atom.config.get 'magic-console.evaluateOnSave'
            @toggle() unless @blankRuntime.observers?.length > 0
        view
      else
        filePath = pathname
        view = new ConsoleRuntimeView({filePath})
        @views = [@views..., view]
        @subscriptions.add view.onDidDestroy =>
          @blankRuntime.stop()
          @views = @views.filter (view) -> view.filePath != filePath
        @subscriptions.add view.onDidSave (ev) =>
          if atom.config.get 'magic-console.evaluateOnSave'
            @toggle() unless @blankRuntime.observers?.length > 0
        view

  deactivate: ->
    @subscriptions.dispose()

  # serialize: ->
  #   view: @views.forEach (view) -> view.serialize()

  # deserialize: ->
  #   view: @view.deserialize()

  consumeBlankRuntime: (runtime) ->
    @blankRuntime = runtime

  consumeDefaultRuntime: (runtime) ->
    @defaultRuntime = runtime

  activatePackage: (packageName) ->
    return if atom.packages.activePackages[packageName]?
    atomPackage = atom.packages.loadPackage(packageName)
    throw new Error("Packaged not installed: '#{packageName}'") unless atomPackage?
    atomPackage.activateNow()

  runBlank: (view) ->
    @activatePackage('script')
    @blankRuntime.addObserver(new ConsoleRuntimeObserver(view))
    @blankRuntime.execute()

  runDefault: ->
    # @activatePackage('script')
    @defaultRuntime.execute()

  toggle: ->
    editor = atom.workspace.getActiveTextEditor()
    return unless editor?
    uri = "magic-console://editor/#{editor.id}"
    previewPane = atom.workspace.paneForURI(uri)
    if previewPane
      # previewPane.destroyItem(previewPane.itemForURI(uri))
      previousActivePane = atom.workspace.getActivePane()
      previewPane.activateItemForURI(uri)
      if @blankRuntime.observers?.length > 0
        return @blankRuntime.stop()
{'fraction_non_alphanumeric': 0.1180806474924122, 'fraction_numerical': 0.0013007660066483597, 'mean_word_length': 2.8508625486922647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, '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': '6839274', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1943, 'n_words': 397}